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 java.math.RoundingMode;
import ca.uhn.fhir.model.api.annotation.DatatypeDef; import ca.uhn.fhir.model.api.annotation.DatatypeDef;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype; import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
/** /**
@ -78,7 +79,7 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
* Constructor * Constructor
*/ */
public DecimalType(String theValue) { public DecimalType(String theValue) {
setValue(new BigDecimal(theValue)); setValueAsString(theValue);
} }
@Override @Override
@ -148,6 +149,14 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
setValue(new BigDecimal(theValue)); setValue(new BigDecimal(theValue));
} }
public void setValueAsString(String theString) {
if (StringUtils.isBlank(theString)) {
setValue((BigDecimal) null);
} else {
setValue(new BigDecimal(theString));
}
}
@Override @Override
public DecimalType copy() { public DecimalType copy() {
return new DecimalType(getValue()); 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.Assertions;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; 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 { class DecimalTypeNullTest {
@ -44,4 +49,27 @@ class DecimalTypeNullTest {
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy(); DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
Assertions.assertNull(copyDecimal.getValue()); 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.MathContext;
import java.math.RoundingMode; import java.math.RoundingMode;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype; import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
import ca.uhn.fhir.model.api.annotation.DatatypeDef; import ca.uhn.fhir.model.api.annotation.DatatypeDef;
@ -79,7 +80,7 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
* Constructor * Constructor
*/ */
public DecimalType(String theValue) { public DecimalType(String theValue) {
setValue(new BigDecimal(theValue)); setValueAsString(theValue);
} }
@Override @Override
@ -149,6 +150,14 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
setValue(new BigDecimal(theValue)); 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 * 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

@ -35,6 +35,7 @@ import java.math.BigDecimal;
import java.math.MathContext; import java.math.MathContext;
import java.math.RoundingMode; import java.math.RoundingMode;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype; import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
import ca.uhn.fhir.model.api.annotation.DatatypeDef; import ca.uhn.fhir.model.api.annotation.DatatypeDef;
@ -81,7 +82,7 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
* Constructor * Constructor
*/ */
public DecimalType(String theValue) { public DecimalType(String theValue) {
setValue(new BigDecimal(theValue)); setValueAsString(theValue);
} }
@Override @Override
@ -151,6 +152,14 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
setValue(BigDecimal.valueOf(theValue)); 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 * 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.Assertions;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; 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 { class DecimalTypeNullTest {
@ -44,4 +49,27 @@ class DecimalTypeNullTest {
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy(); DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
Assertions.assertNull(copyDecimal.getValue()); 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.MathContext;
import java.math.RoundingMode; import java.math.RoundingMode;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype; import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
import ca.uhn.fhir.model.api.annotation.DatatypeDef; import ca.uhn.fhir.model.api.annotation.DatatypeDef;
@ -150,9 +151,14 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
} }
public void setValueAsString(String theString) { public void setValueAsString(String theString) {
if (StringUtils.isBlank(theString)) {
setValue((BigDecimal) null);
setRepresentation(null);
} else {
setValue(new BigDecimal(theString)); setValue(new BigDecimal(theString));
setRepresentation(theString); setRepresentation(theString);
} }
}
/** /**
* Sets a new value using a long * Sets a new value using a long

View File

@ -3,6 +3,11 @@ package org.hl7.fhir.r4.model;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; 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 { class DecimalTypeNullTest {
@ -44,4 +49,27 @@ class DecimalTypeNullTest {
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy(); DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
Assertions.assertNull(copyDecimal.getValue()); 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.MathContext;
import java.math.RoundingMode; import java.math.RoundingMode;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype; import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
import ca.uhn.fhir.model.api.annotation.DatatypeDef; import ca.uhn.fhir.model.api.annotation.DatatypeDef;
@ -150,9 +151,14 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
} }
public void setValueAsString(String theString) { public void setValueAsString(String theString) {
if (StringUtils.isBlank(theString)) {
setValue((BigDecimal) null);
setRepresentation(null);
} else {
setValue(new BigDecimal(theString)); setValue(new BigDecimal(theString));
setRepresentation(theString); setRepresentation(theString);
} }
}
/** /**
* Sets a new value using a long * Sets a new value using a long

View File

@ -1,9 +1,13 @@
package org.hl7.fhir.r4b.model; package org.hl7.fhir.r4b.model;
import org.hl7.fhir.r4b.model.DecimalType;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; 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 { class DecimalTypeNullTest {
@ -45,4 +49,27 @@ class DecimalTypeNullTest {
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy(); DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
Assertions.assertNull(copyDecimal.getValue()); 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.MathContext;
import java.math.RoundingMode; import java.math.RoundingMode;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype; import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
import ca.uhn.fhir.model.api.annotation.DatatypeDef; 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) { public void setValueAsString(String theString) {
if (StringUtils.isBlank(theString)) {
setValue((BigDecimal) null);
setRepresentation(null);
} else {
setValue(new BigDecimal(theString)); setValue(new BigDecimal(theString));
setRepresentation(theString); setRepresentation(theString);
}
} }
@Override @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 * A parser can provide a literal representation for the decimal value that preserves
* the presented form. * the presented form.
* * <p/>
* All sorts of bad things can happen if this method is used to set the string representation * 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 * 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.Assertions;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; 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 { class DecimalTypeNullTest {
@ -44,4 +50,27 @@ class DecimalTypeNullTest {
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy(); DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
Assertions.assertNull(copyDecimal.getValue()); 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));
}
} }