From 4af4b05e338d295d7cb060491ae3a832d0b05eb1 Mon Sep 17 00:00:00 2001 From: Taeik Lim Date: Thu, 11 Mar 2021 02:32:56 +0900 Subject: [PATCH] Fix NPE in JobParameters.toProperties on null parameter value Issue #834 --- .../springframework/batch/core/JobParameters.java | 8 +++++--- .../batch/core/JobParametersTests.java | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/JobParameters.java b/spring-batch-core/src/main/java/org/springframework/batch/core/JobParameters.java index 74502ef669..94a7973971 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/JobParameters.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/JobParameters.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2018 the original author or authors. + * Copyright 2006-2021 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.Date; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Objects; import java.util.Properties; import org.springframework.lang.Nullable; @@ -38,6 +39,7 @@ * @author Lucas Ward * @author Michael Minella * @author Mahmoud Ben Hassine + * @author Taeik Lim * @since 1.0 */ @SuppressWarnings("serial") @@ -269,8 +271,8 @@ public Properties toProperties() { Properties props = new Properties(); for (Map.Entry param : parameters.entrySet()) { - if(param.getValue() != null) { - props.put(param.getKey(), param.getValue().toString()); + if (param.getValue() != null) { + props.put(param.getKey(), Objects.toString(param.getValue().toString(), "")); } } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/JobParametersTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/JobParametersTests.java index 68fa481fc4..3eb34a5cd2 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/JobParametersTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/JobParametersTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2018 the original author or authors. + * Copyright 2008-2021 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. @@ -25,6 +25,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; +import java.util.Properties; import org.junit.Before; import org.junit.Test; @@ -35,6 +36,7 @@ * @author Dave Syer * @author Michael Minella * @author Mahmoud Ben Hassine + * @author Taeik Lim * */ public class JobParametersTests { @@ -228,4 +230,15 @@ public void testDoubleReturnsNullWhenKeyDoesntExit(){ public void testDateReturnsNullWhenKeyDoesntExit(){ assertNull(new JobParameters().getDate("keythatdoesntexist")); } + + @Test + public void testToPropertiesWithNullValue() { + Map parameterMap = new HashMap<>(); + Long value = null; + parameterMap.put("nullkey", new JobParameter(value)); + JobParameters jobParameters = new JobParameters(parameterMap); + + Properties properties = jobParameters.toProperties(); + assertEquals("", properties.get("nullkey")); + } }