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
This commit is contained in:
Mahmoud Ben Hassine
2023-09-18 17:25:12 +02:00
parent 69c6839415
commit 6eeaf4d256
2 changed files with 10 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-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.
@@ -20,6 +20,9 @@ import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.JobParametersValidator;
@@ -36,6 +39,8 @@ import org.springframework.util.Assert;
*/
public class DefaultJobParametersValidator implements JobParametersValidator, InitializingBean {
private static final Log logger = LogFactory.getLog(DefaultJobParametersValidator.class);
private Collection<String> requiredKeys;
private Collection<String> optionalKeys;
@@ -100,7 +105,7 @@ public class DefaultJobParametersValidator implements JobParametersValidator, In
}
}
if (!missingKeys.isEmpty()) {
throw new JobParametersInvalidException(
logger.warn(
"The JobParameters contains keys that are not explicitly optional or required: " + missingKeys);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2009-2022 the original author or authors.
* Copyright 2009-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.
@@ -20,6 +20,7 @@ import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
class DefaultJobParametersValidatorTests {
@@ -59,7 +60,7 @@ class DefaultJobParametersValidatorTests {
void testValidateOptionalWithImplicitRequiredKey() {
validator.setOptionalKeys(new String[] { "name", "value" });
JobParameters jobParameters = new JobParametersBuilder().addString("foo", "bar").toJobParameters();
assertThrows(JobParametersInvalidException.class, () -> validator.validate(jobParameters));
assertDoesNotThrow(() -> validator.validate(jobParameters));
}
@Test