Skip to content

Commit 4af4b05

Browse files
acktsapfmbenhassine
authored andcommitted
Fix NPE in JobParameters.toProperties on null parameter value
Issue #834
1 parent 1beb07e commit 4af4b05

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/JobParameters.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2018 the original author or authors.
2+
* Copyright 2006-2021 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
import java.util.Date;
2121
import java.util.LinkedHashMap;
2222
import java.util.Map;
23+
import java.util.Objects;
2324
import java.util.Properties;
2425

2526
import org.springframework.lang.Nullable;
@@ -38,6 +39,7 @@
3839
* @author Lucas Ward
3940
* @author Michael Minella
4041
* @author Mahmoud Ben Hassine
42+
* @author Taeik Lim
4143
* @since 1.0
4244
*/
4345
@SuppressWarnings("serial")
@@ -269,8 +271,8 @@ public Properties toProperties() {
269271
Properties props = new Properties();
270272

271273
for (Map.Entry<String, JobParameter> param : parameters.entrySet()) {
272-
if(param.getValue() != null) {
273-
props.put(param.getKey(), param.getValue().toString());
274+
if (param.getValue() != null) {
275+
props.put(param.getKey(), Objects.toString(param.getValue().toString(), ""));
274276
}
275277
}
276278

spring-batch-core/src/test/java/org/springframework/batch/core/JobParametersTests.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2018 the original author or authors.
2+
* Copyright 2008-2021 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.
@@ -25,6 +25,7 @@
2525
import java.util.HashMap;
2626
import java.util.Map;
2727
import java.util.Map.Entry;
28+
import java.util.Properties;
2829

2930
import org.junit.Before;
3031
import org.junit.Test;
@@ -35,6 +36,7 @@
3536
* @author Dave Syer
3637
* @author Michael Minella
3738
* @author Mahmoud Ben Hassine
39+
* @author Taeik Lim
3840
*
3941
*/
4042
public class JobParametersTests {
@@ -228,4 +230,15 @@ public void testDoubleReturnsNullWhenKeyDoesntExit(){
228230
public void testDateReturnsNullWhenKeyDoesntExit(){
229231
assertNull(new JobParameters().getDate("keythatdoesntexist"));
230232
}
233+
234+
@Test
235+
public void testToPropertiesWithNullValue() {
236+
Map<String, JobParameter> parameterMap = new HashMap<>();
237+
Long value = null;
238+
parameterMap.put("nullkey", new JobParameter(value));
239+
JobParameters jobParameters = new JobParameters(parameterMap);
240+
241+
Properties properties = jobParameters.toProperties();
242+
assertEquals("", properties.get("nullkey"));
243+
}
231244
}

0 commit comments

Comments
 (0)