Skip to content

Commit 955f9a4

Browse files
jwillebrandsfmbenhassine
authored andcommitted
Copy missing Context keys from JobParameters
Fix DefaultJobParametersExtractor to copy values from JobParameters when they are missing from ExecutionContext. Javadoc states this class is supposed to do just that, but seems to have broken in the 5.x API revamp. Resolves #4458 (cherry picked from commit b2a287d)
1 parent 566b680 commit 955f9a4

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/step/job/DefaultJobParametersExtractor.java

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ public JobParameters getJobParameters(Job job, StepExecution stepExecution) {
7979
if (executionContext.containsKey(key)) {
8080
properties.setProperty(key, executionContext.getString(key));
8181
}
82+
else if (jobParameters.containsKey(key)) {
83+
builder.addJobParameter(key, jobParameters.get(key));
84+
}
8285
}
8386
builder.addJobParameters(this.jobParametersConverter.getJobParameters(properties));
8487
return builder.toJobParameters();

spring-batch-core/src/test/java/org/springframework/batch/core/step/job/DefaultJobParametersExtractorTests.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,10 +18,12 @@
1818
import org.junit.jupiter.api.Test;
1919

2020
import org.springframework.batch.core.JobExecution;
21+
import org.springframework.batch.core.JobParameter;
2122
import org.springframework.batch.core.JobParameters;
2223
import org.springframework.batch.core.JobParametersBuilder;
2324
import org.springframework.batch.core.StepExecution;
2425

26+
import static org.assertj.core.api.Assertions.assertThat;
2527
import static org.junit.jupiter.api.Assertions.assertEquals;
2628
import static org.junit.jupiter.api.Assertions.assertNotNull;
2729
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -109,4 +111,25 @@ void testDontUseParentParameters() {
109111
assertNotNull(jobParameters.getParameter("foo").getValue());
110112
}
111113

114+
@Test
115+
public void testGetKeysFromParentParametersWhenNotInExecutionContext() {
116+
DefaultJobParametersExtractor extractor = new DefaultJobParametersExtractor();
117+
extractor.setUseAllParentParameters(false);
118+
119+
JobExecution jobExecution = new JobExecution(0L,
120+
new JobParametersBuilder().addString("parentParam", "val").addDouble("foo", 22.2).toJobParameters());
121+
122+
StepExecution stepExecution = new StepExecution("step", jobExecution);
123+
124+
stepExecution.getExecutionContext().put("foo", "11.1,java.lang.Double");
125+
extractor.setKeys(new String[] { "foo", "parentParam" });
126+
127+
JobParameters jobParameters = extractor.getJobParameters(null, stepExecution);
128+
129+
assertThat(jobParameters.getParameter("parentParam")).isNotNull()
130+
.extracting(JobParameter::getValue)
131+
.isEqualTo("val");
132+
assertEquals(11.1, jobParameters.getDouble("foo"));
133+
}
134+
112135
}

0 commit comments

Comments
 (0)