Fix exception when parsing empty values in DefaultJobParametersConverter

Resolves #4505
This commit is contained in:
Mahmoud Ben Hassine
2024-02-14 15:01:59 +01:00
parent 85a84cb30f
commit 964df6d643
2 changed files with 24 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2023 the original author or authors.
* Copyright 2006-2024 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.
@@ -169,7 +169,11 @@ public class DefaultJobParametersConverter implements JobParametersConverter {
}
private String parseValue(String encodedJobParameter) {
return StringUtils.commaDelimitedListToStringArray(encodedJobParameter)[0];
String[] tokens = StringUtils.commaDelimitedListToStringArray(encodedJobParameter);
if (tokens.length == 0) {
return "";
}
return tokens[0];
}
private Class<?> parseType(String encodedJobParameter) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2023 the original author or authors.
* Copyright 2006-2024 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 java.util.Properties;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.util.StringUtils;
@@ -129,6 +130,22 @@ class DefaultJobParametersConverterTests {
}
}
@Test
void testGetParametersWithEmptyValue() {
// given
String[] args = new String[] { "parameter=" };
// when
JobParameters jobParameters = factory.getJobParameters(StringUtils.splitArrayElementsIntoProperties(args, "="));
// then
assertEquals(1, jobParameters.getParameters().size());
JobParameter<?> parameter = jobParameters.getParameters().get("parameter");
assertEquals("", parameter.getValue());
assertEquals(String.class, parameter.getType());
assertTrue(parameter.isIdentifying());
}
@Test
void testGetParametersWithDoubleValueDeclaredAsLong() {