diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/step/job/DefaultJobParametersExtractor.java b/spring-batch-core/src/main/java/org/springframework/batch/core/step/job/DefaultJobParametersExtractor.java index 948b144712..f37de435a5 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/step/job/DefaultJobParametersExtractor.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/step/job/DefaultJobParametersExtractor.java @@ -79,6 +79,9 @@ public JobParameters getJobParameters(Job job, StepExecution stepExecution) { if (executionContext.containsKey(key)) { properties.setProperty(key, executionContext.getString(key)); } + else if (jobParameters.containsKey(key)) { + builder.addJobParameter(key, jobParameters.get(key)); + } } builder.addJobParameters(this.jobParametersConverter.getJobParameters(properties)); return builder.toJobParameters(); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/job/DefaultJobParametersExtractorTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/job/DefaultJobParametersExtractorTests.java index 1f124694d1..c94a6d2de5 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/job/DefaultJobParametersExtractorTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/job/DefaultJobParametersExtractorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2022 the original author or authors. + * Copyright 2006-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,10 +18,12 @@ import org.junit.jupiter.api.Test; import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.JobParameter; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParametersBuilder; import org.springframework.batch.core.StepExecution; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; @@ -109,4 +111,25 @@ void testDontUseParentParameters() { assertNotNull(jobParameters.getParameter("foo").getValue()); } + @Test + public void testGetKeysFromParentParametersWhenNotInExecutionContext() { + DefaultJobParametersExtractor extractor = new DefaultJobParametersExtractor(); + extractor.setUseAllParentParameters(false); + + JobExecution jobExecution = new JobExecution(0L, + new JobParametersBuilder().addString("parentParam", "val").addDouble("foo", 22.2).toJobParameters()); + + StepExecution stepExecution = new StepExecution("step", jobExecution); + + stepExecution.getExecutionContext().put("foo", "11.1,java.lang.Double"); + extractor.setKeys(new String[] { "foo", "parentParam" }); + + JobParameters jobParameters = extractor.getJobParameters(null, stepExecution); + + assertThat(jobParameters.getParameter("parentParam")).isNotNull() + .extracting(JobParameter::getValue) + .isEqualTo("val"); + assertEquals(11.1, jobParameters.getDouble("foo")); + } + }