Skip to content

Commit

Permalink
Copy missing Context keys from JobParameters
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
jwillebrands authored and fmbenhassine committed Oct 18, 2023
1 parent 566b680 commit 955f9a4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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"));
}

}

0 comments on commit 955f9a4

Please sign in to comment.