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
This commit is contained in:
Jan-Willem Willebrands
2023-10-03 14:39:34 +02:00
committed by Mahmoud Ben Hassine
parent 5598e180a2
commit b2a287da93
2 changed files with 27 additions and 1 deletions

View File

@@ -79,6 +79,9 @@ public class DefaultJobParametersExtractor implements JobParametersExtractor {
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();

View File

@@ -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 @@ package org.springframework.batch.core.step.job;
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 @@ class DefaultJobParametersExtractorTests {
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"));
}
}