Skip to content

Commit 0093e44

Browse files
committed
Fix exception when parsing empty values in DefaultJobParametersConverter
Resolves #4505
1 parent 2fbdcd4 commit 0093e44

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/converter/DefaultJobParametersConverter.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2024 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.
@@ -168,7 +168,11 @@ protected JobParameter<?> decode(String encodedJobParameter) {
168168
}
169169

170170
private String parseValue(String encodedJobParameter) {
171-
return StringUtils.commaDelimitedListToStringArray(encodedJobParameter)[0];
171+
String[] tokens = StringUtils.commaDelimitedListToStringArray(encodedJobParameter);
172+
if (tokens.length == 0) {
173+
return "";
174+
}
175+
return tokens[0];
172176
}
173177

174178
private Class<?> parseType(String encodedJobParameter) {

Diff for: spring-batch-core/src/test/java/org/springframework/batch/core/converter/DefaultJobParametersConverterTests.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2024 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

2121
import org.junit.jupiter.api.Test;
2222

23+
import org.springframework.batch.core.JobParameter;
2324
import org.springframework.batch.core.JobParameters;
2425
import org.springframework.batch.core.JobParametersBuilder;
2526
import org.springframework.util.StringUtils;
@@ -129,6 +130,22 @@ void testGetParametersWithBogusLong() {
129130
}
130131
}
131132

133+
@Test
134+
void testGetParametersWithEmptyValue() {
135+
// given
136+
String[] args = new String[] { "parameter=" };
137+
138+
// when
139+
JobParameters jobParameters = factory.getJobParameters(StringUtils.splitArrayElementsIntoProperties(args, "="));
140+
141+
// then
142+
assertEquals(1, jobParameters.getParameters().size());
143+
JobParameter<?> parameter = jobParameters.getParameters().get("parameter");
144+
assertEquals("", parameter.getValue());
145+
assertEquals(String.class, parameter.getType());
146+
assertTrue(parameter.isIdentifying());
147+
}
148+
132149
@Test
133150
void testGetParametersWithDoubleValueDeclaredAsLong() {
134151

0 commit comments

Comments
 (0)