Skip to content

Commit

Permalink
Fix NPE in JobParameters.toProperties on null parameter value
Browse files Browse the repository at this point in the history
Issue #834
  • Loading branch information
acktsap authored and fmbenhassine committed May 18, 2021
1 parent 1beb07e commit 4af4b05
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
@@ -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.
Expand All @@ -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;
Expand All @@ -38,6 +39,7 @@
* @author Lucas Ward
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @author Taeik Lim
* @since 1.0
*/
@SuppressWarnings("serial")
Expand Down Expand Up @@ -269,8 +271,8 @@ public Properties toProperties() {
Properties props = new Properties();

for (Map.Entry<String, JobParameter> 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(), ""));
}
}

Expand Down
@@ -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.
Expand All @@ -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;
Expand All @@ -35,6 +36,7 @@
* @author Dave Syer
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @author Taeik Lim
*
*/
public class JobParametersTests {
Expand Down Expand Up @@ -228,4 +230,15 @@ public void testDoubleReturnsNullWhenKeyDoesntExit(){
public void testDateReturnsNullWhenKeyDoesntExit(){
assertNull(new JobParameters().getDate("keythatdoesntexist"));
}

@Test
public void testToPropertiesWithNullValue() {
Map<String, JobParameter> 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"));
}
}

0 comments on commit 4af4b05

Please sign in to comment.