Skip to content

Commit 6eeaf4d

Browse files
committed
Add warning for missing optional keys in DefaultJobParametersValidator
Before this commit, the DefaultJobParametersValidator was failing when an optional key is missing. This commit changes the default validator to only log a warning in this case. Resolves #4073
1 parent 69c6839 commit 6eeaf4d

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-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.
@@ -20,6 +20,9 @@
2020
import java.util.HashSet;
2121
import java.util.Set;
2222

23+
import org.apache.commons.logging.Log;
24+
import org.apache.commons.logging.LogFactory;
25+
2326
import org.springframework.batch.core.JobParameters;
2427
import org.springframework.batch.core.JobParametersInvalidException;
2528
import org.springframework.batch.core.JobParametersValidator;
@@ -36,6 +39,8 @@
3639
*/
3740
public class DefaultJobParametersValidator implements JobParametersValidator, InitializingBean {
3841

42+
private static final Log logger = LogFactory.getLog(DefaultJobParametersValidator.class);
43+
3944
private Collection<String> requiredKeys;
4045

4146
private Collection<String> optionalKeys;
@@ -100,7 +105,7 @@ public void validate(@Nullable JobParameters parameters) throws JobParametersInv
100105
}
101106
}
102107
if (!missingKeys.isEmpty()) {
103-
throw new JobParametersInvalidException(
108+
logger.warn(
104109
"The JobParameters contains keys that are not explicitly optional or required: " + missingKeys);
105110
}
106111

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2022 the original author or authors.
2+
* Copyright 2009-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.
@@ -20,6 +20,7 @@
2020
import org.springframework.batch.core.JobParametersBuilder;
2121
import org.springframework.batch.core.JobParametersInvalidException;
2222

23+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2324
import static org.junit.jupiter.api.Assertions.assertThrows;
2425

2526
class DefaultJobParametersValidatorTests {
@@ -59,7 +60,7 @@ void testValidateOptionalValues() throws Exception {
5960
void testValidateOptionalWithImplicitRequiredKey() {
6061
validator.setOptionalKeys(new String[] { "name", "value" });
6162
JobParameters jobParameters = new JobParametersBuilder().addString("foo", "bar").toJobParameters();
62-
assertThrows(JobParametersInvalidException.class, () -> validator.validate(jobParameters));
63+
assertDoesNotThrow(() -> validator.validate(jobParameters));
6364
}
6465

6566
@Test

0 commit comments

Comments
 (0)