From 955f9a48ead342bdf129256497eb245fa912ee22 Mon Sep 17 00:00:00 2001 From: Jan-Willem Willebrands Date: Tue, 3 Oct 2023 14:39:34 +0200 Subject: [PATCH] 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 b2a287da93982461fcfa484d9b5443f46f44fa8c) --- .../job/DefaultJobParametersExtractor.java | 3 +++ .../DefaultJobParametersExtractorTests.java | 25 ++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) 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 948b14471..f37de435a 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 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(); 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 1f124694d..c94a6d2de 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 @@ 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")); + } + }