Skip to content

Commit

Permalink
Add warning for missing optional keys in DefaultJobParametersValidator
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fmbenhassine committed Sep 18, 2023
1 parent 69c6839 commit 6eeaf4d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -20,6 +20,9 @@
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;
Expand All @@ -36,6 +39,8 @@
*/
public class DefaultJobParametersValidator implements JobParametersValidator, InitializingBean {

private static final Log logger = LogFactory.getLog(DefaultJobParametersValidator.class);

private Collection<String> requiredKeys;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -20,6 +20,7 @@
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 {
Expand Down Expand Up @@ -59,7 +60,7 @@ void testValidateOptionalValues() throws Exception {
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
Expand Down

0 comments on commit 6eeaf4d

Please sign in to comment.