diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java index b87a3408961..f0d4f6a1276 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java @@ -58,6 +58,24 @@ public class JsonParserDstu3Test { ourCtx.setNarrativeGenerator(null); } + @Test + public void testEncodedResourceWithIncorrectRepresentationOfDecimalTypeToJson() { + DecimalType decimalType = new DecimalType(); + decimalType.setValueAsString(".5"); + MedicationRequest mr = new MedicationRequest(); + Dosage dosage = new Dosage(); + dosage.setDose(new SimpleQuantity() + .setValue(decimalType.getValue()) + .setUnit("{tablet}") + .setSystem("http://unitsofmeasure.org") + .setCode("{tablet}")); + mr.addDosageInstruction(dosage); + String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(mr); + ourLog.info(encoded); + mr = ourCtx.newJsonParser().parseResource(MedicationRequest.class, encoded); + assertEquals(BigDecimal.valueOf(0.5), mr.getDosageInstructionFirstRep().getDoseSimpleQuantity().getValue()); + assertTrue(encoded.contains("0.5")); + } /** * See #563 diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java index 98fdae848b5..6d4f0fb10d6 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java @@ -45,6 +45,7 @@ import org.xmlunit.diff.ElementSelectors; import java.io.IOException; import java.io.StringReader; +import java.math.BigDecimal; import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.util.*; @@ -70,6 +71,25 @@ public class XmlParserDstu3Test { ourCtx.setNarrativeGenerator(null); } + @Test + public void testEncodedResourceWithIncorrectRepresentationOfDecimalTypeToXml() { + DecimalType decimalType = new DecimalType(); + decimalType.setValueAsString(".5"); + MedicationRequest mr = new MedicationRequest(); + Dosage dosage = new Dosage(); + dosage.setDose(new SimpleQuantity() + .setValue(decimalType.getValue()) + .setUnit("{tablet}") + .setSystem("http://unitsofmeasure.org") + .setCode("{tablet}")); + mr.addDosageInstruction(dosage); + String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(mr); + ourLog.info(encoded); + mr = ourCtx.newXmlParser().parseResource(MedicationRequest.class, encoded); + assertEquals(BigDecimal.valueOf(0.5), mr.getDosageInstructionFirstRep().getDoseSimpleQuantity().getValue()); + assertTrue(encoded.contains("0.5")); + } + /** * We specifically include extensions on CapabilityStatment even in * summary mode, since this is behaviour that people depend on