Skip to content

Commit d8d76ab

Browse files
committed
Fix exception when parsing empty values in DefaultJobParametersConverter
Resolves #4505
1 parent c369190 commit d8d76ab

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

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.
@@ -169,7 +169,11 @@ protected JobParameter<?> decode(String encodedJobParameter) {
169169
}
170170

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

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

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)