From 6eeaf4d256c26165710e79a8ea79d6874e1e76bd Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Mon, 18 Sep 2023 17:25:12 +0200 Subject: [PATCH] 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 --- .../batch/core/job/DefaultJobParametersValidator.java | 9 +++++++-- .../core/job/DefaultJobParametersValidatorTests.java | 5 +++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/job/DefaultJobParametersValidator.java b/spring-batch-core/src/main/java/org/springframework/batch/core/job/DefaultJobParametersValidator.java index c20410ae99..114670c294 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/job/DefaultJobParametersValidator.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/job/DefaultJobParametersValidator.java @@ -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.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 @@ */ public class DefaultJobParametersValidator implements JobParametersValidator, InitializingBean { + private static final Log logger = LogFactory.getLog(DefaultJobParametersValidator.class); + private Collection requiredKeys; private Collection optionalKeys; @@ -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); } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/job/DefaultJobParametersValidatorTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/job/DefaultJobParametersValidatorTests.java index 03bd8a7c98..ccd88adb99 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/job/DefaultJobParametersValidatorTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/job/DefaultJobParametersValidatorTests.java @@ -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.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 @@ 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