Merge pull request #1845 from hapifhir/do-20241204-npe-in-decimaltype

Fix NPE in DecimalType
This commit is contained in:
Grahame Grieve 2024-12-08 05:41:12 +03:00 committed by GitHub
commit 34efd6aa1d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 299 additions and 39 deletions

View File

@ -34,6 +34,7 @@ import java.math.MathContext;
import java.math.RoundingMode;
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
/**
@ -78,7 +79,7 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
* Constructor
*/
public DecimalType(String theValue) {
setValue(new BigDecimal(theValue));
setValueAsString(theValue);
}
@Override
@ -148,6 +149,14 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
setValue(new BigDecimal(theValue));
}
public void setValueAsString(String theString) {
if (StringUtils.isBlank(theString)) {
setValue((BigDecimal) null);
} else {
setValue(new BigDecimal(theString));
}
}
@Override
public DecimalType copy() {
return new DecimalType(getValue());

View File

@ -3,6 +3,11 @@ package org.hl7.fhir.dstu2.model;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.stream.Stream;
class DecimalTypeNullTest {
@ -44,4 +49,27 @@ class DecimalTypeNullTest {
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
Assertions.assertNull(copyDecimal.getValue());
}
public static Stream<Arguments> provideEmptyOrNullStringsConstructor() {
return Stream.of(
Arguments.of((String)null),
Arguments.of(""),
Arguments.of(" ")
);
}
@ParameterizedTest
@MethodSource("provideEmptyOrNullStringsConstructor")
void testNullValue(String theStringValue) {
DecimalType nullDecimal = new DecimalType();
Assertions.assertNull(nullDecimal.getValue());
Assertions.assertNull(nullDecimal.asStringValue());
DecimalType anotherNullDecimal = new DecimalType(theStringValue);
Assertions.assertNull(anotherNullDecimal.getValue());
Assertions.assertNull(anotherNullDecimal.asStringValue());
Assertions.assertTrue(nullDecimal.equalsDeep(anotherNullDecimal));
Assertions.assertTrue(nullDecimal.equalsShallow(anotherNullDecimal));
}
}

View File

@ -33,6 +33,7 @@ import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
@ -79,7 +80,7 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
* Constructor
*/
public DecimalType(String theValue) {
setValue(new BigDecimal(theValue));
setValueAsString(theValue);
}
@Override
@ -149,6 +150,14 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
setValue(new BigDecimal(theValue));
}
public void setValueAsString(String theString) {
if (StringUtils.isBlank(theString)) {
setValue((BigDecimal) null);
} else {
setValue(new BigDecimal(theString));
}
}
/**
* Sets a new value using a long
*/

View File

@ -0,0 +1,75 @@
package org.hl7.fhir.dstu2016may.model;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.stream.Stream;
class DecimalTypeNullTest {
@Test
@DisplayName("Test null value toString()")
void testToString() {
DecimalType nullDecimal = new DecimalType();
System.out.println("Value -> " + nullDecimal);
}
@Test
@DisplayName("Test null value equalsDeep()")
void equalsDeep() {
DecimalType nullDecimal = new DecimalType();
DecimalType validDecimal = new DecimalType("3.14");
Assertions.assertFalse(nullDecimal.equalsDeep(validDecimal));
}
@Test
@DisplayName("Test null value equalsShallow()")
void equalsShallow() {
DecimalType nullDecimal = new DecimalType();
DecimalType validDecimal = new DecimalType("3.14");
Assertions.assertFalse(nullDecimal.equalsShallow(validDecimal));
}
@Test
@DisplayName("Test null value copy()")
void copy() {
DecimalType nullDecimal = new DecimalType();
DecimalType copyDecimal = nullDecimal.copy();
Assertions.assertNull(copyDecimal.getValue());
}
@Test
@DisplayName("Test null value typedCopy()")
void typedCopy() {
DecimalType nullDecimal = new DecimalType();
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
Assertions.assertNull(copyDecimal.getValue());
}
public static Stream<Arguments> provideEmptyOrNullStringsConstructor() {
return Stream.of(
Arguments.of((String)null),
Arguments.of(""),
Arguments.of(" ")
);
}
@ParameterizedTest
@MethodSource("provideEmptyOrNullStringsConstructor")
void testNullValue(String theStringValue) {
DecimalType nullDecimal = new DecimalType();
Assertions.assertNull(nullDecimal.getValue());
Assertions.assertNull(nullDecimal.asStringValue());
DecimalType anotherNullDecimal = new DecimalType(theStringValue);
Assertions.assertNull(anotherNullDecimal.getValue());
Assertions.assertNull(anotherNullDecimal.asStringValue());
Assertions.assertTrue(nullDecimal.equalsDeep(anotherNullDecimal));
Assertions.assertTrue(nullDecimal.equalsShallow(anotherNullDecimal));
}
}

View File

@ -1,33 +1,33 @@
package org.hl7.fhir.dstu3.model;
/*
Copyright (c) 2011+, HL7, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of HL7 nor the names of its contributors may be used to
endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/*
Copyright (c) 2011+, HL7, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of HL7 nor the names of its contributors may be used to
endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
@ -35,6 +35,7 @@ import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
@ -81,7 +82,7 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
* Constructor
*/
public DecimalType(String theValue) {
setValue(new BigDecimal(theValue));
setValueAsString(theValue);
}
@Override
@ -151,6 +152,14 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
setValue(BigDecimal.valueOf(theValue));
}
public void setValueAsString(String theString) {
if (StringUtils.isBlank(theString)) {
setValue((BigDecimal) null);
} else {
setValue(new BigDecimal(theString));
}
}
/**
* Sets a new value using a long
*/

View File

@ -3,6 +3,11 @@ package org.hl7.fhir.dstu3.model;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.stream.Stream;
class DecimalTypeNullTest {
@ -44,4 +49,27 @@ class DecimalTypeNullTest {
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
Assertions.assertNull(copyDecimal.getValue());
}
public static Stream<Arguments> provideEmptyOrNullStringsConstructor() {
return Stream.of(
Arguments.of((String)null),
Arguments.of(""),
Arguments.of(" ")
);
}
@ParameterizedTest
@MethodSource("provideEmptyOrNullStringsConstructor")
void testNullValue(String theStringValue) {
DecimalType nullDecimal = new DecimalType();
Assertions.assertNull(nullDecimal.getValue());
Assertions.assertNull(nullDecimal.asStringValue());
DecimalType anotherNullDecimal = new DecimalType(theStringValue);
Assertions.assertNull(anotherNullDecimal.getValue());
Assertions.assertNull(anotherNullDecimal.asStringValue());
Assertions.assertTrue(nullDecimal.equalsDeep(anotherNullDecimal));
Assertions.assertTrue(nullDecimal.equalsShallow(anotherNullDecimal));
}
}

View File

@ -33,6 +33,7 @@ import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
@ -150,8 +151,13 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
}
public void setValueAsString(String theString) {
setValue(new BigDecimal(theString));
setRepresentation(theString);
if (StringUtils.isBlank(theString)) {
setValue((BigDecimal) null);
setRepresentation(null);
} else {
setValue(new BigDecimal(theString));
setRepresentation(theString);
}
}
/**

View File

@ -3,6 +3,11 @@ package org.hl7.fhir.r4.model;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.stream.Stream;
class DecimalTypeNullTest {
@ -44,4 +49,27 @@ class DecimalTypeNullTest {
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
Assertions.assertNull(copyDecimal.getValue());
}
public static Stream<Arguments> provideEmptyOrNullStringsConstructor() {
return Stream.of(
Arguments.of((String)null),
Arguments.of(""),
Arguments.of(" ")
);
}
@ParameterizedTest
@MethodSource("provideEmptyOrNullStringsConstructor")
void testNullValue(String theStringValue) {
DecimalType nullDecimal = new DecimalType();
Assertions.assertNull(nullDecimal.getValue());
Assertions.assertNull(nullDecimal.asStringValue());
DecimalType anotherNullDecimal = new DecimalType(theStringValue);
Assertions.assertNull(anotherNullDecimal.getValue());
Assertions.assertNull(anotherNullDecimal.asStringValue());
Assertions.assertTrue(nullDecimal.equalsDeep(anotherNullDecimal));
Assertions.assertTrue(nullDecimal.equalsShallow(anotherNullDecimal));
}
}

View File

@ -33,6 +33,7 @@ import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
@ -150,8 +151,13 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
}
public void setValueAsString(String theString) {
setValue(new BigDecimal(theString));
setRepresentation(theString);
if (StringUtils.isBlank(theString)) {
setValue((BigDecimal) null);
setRepresentation(null);
} else {
setValue(new BigDecimal(theString));
setRepresentation(theString);
}
}
/**

View File

@ -1,9 +1,13 @@
package org.hl7.fhir.r4b.model;
import org.hl7.fhir.r4b.model.DecimalType;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.stream.Stream;
class DecimalTypeNullTest {
@ -45,4 +49,27 @@ class DecimalTypeNullTest {
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
Assertions.assertNull(copyDecimal.getValue());
}
public static Stream<Arguments> provideEmptyOrNullStringsConstructor() {
return Stream.of(
Arguments.of((String)null),
Arguments.of(""),
Arguments.of(" ")
);
}
@ParameterizedTest
@MethodSource("provideEmptyOrNullStringsConstructor")
void testNullValue(String theStringValue) {
DecimalType nullDecimal = new DecimalType();
Assertions.assertNull(nullDecimal.getValue());
Assertions.assertNull(nullDecimal.asStringValue());
DecimalType anotherNullDecimal = new DecimalType(theStringValue);
Assertions.assertNull(anotherNullDecimal.getValue());
Assertions.assertNull(anotherNullDecimal.asStringValue());
Assertions.assertTrue(nullDecimal.equalsDeep(anotherNullDecimal));
Assertions.assertTrue(nullDecimal.equalsShallow(anotherNullDecimal));
}
}

View File

@ -35,6 +35,7 @@ import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
@ -166,8 +167,13 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
}
public void setValueAsString(String theString) {
setValue(new BigDecimal(theString));
setRepresentation(theString);
if (StringUtils.isBlank(theString)) {
setValue((BigDecimal) null);
setRepresentation(null);
} else {
setValue(new BigDecimal(theString));
setRepresentation(theString);
}
}
@Override
@ -184,7 +190,7 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
/**
* A parser can provide a literal representation for the decimal value that preserves
* the presented form.
*
* <p/>
* All sorts of bad things can happen if this method is used to set the string representation
* to anything other than what was parsed into the actual value. Don't do that
*

View File

@ -3,6 +3,12 @@ package org.hl7.fhir.r5.model;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.List;
import java.util.stream.Stream;
class DecimalTypeNullTest {
@ -44,4 +50,27 @@ class DecimalTypeNullTest {
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
Assertions.assertNull(copyDecimal.getValue());
}
public static Stream<Arguments> provideEmptyOrNullStringsConstructor() {
return Stream.of(
Arguments.of((String)null),
Arguments.of(""),
Arguments.of(" ")
);
}
@ParameterizedTest
@MethodSource("provideEmptyOrNullStringsConstructor")
void testNullValue(String theStringValue) {
DecimalType nullDecimal = new DecimalType();
Assertions.assertNull(nullDecimal.getValue());
Assertions.assertNull(nullDecimal.asStringValue());
DecimalType anotherNullDecimal = new DecimalType(theStringValue);
Assertions.assertNull(anotherNullDecimal.getValue());
Assertions.assertNull(anotherNullDecimal.asStringValue());
Assertions.assertTrue(nullDecimal.equalsDeep(anotherNullDecimal));
Assertions.assertTrue(nullDecimal.equalsShallow(anotherNullDecimal));
}
}