From 9e9f1e65e2534a7ece1574fa0529d813fbc6392b Mon Sep 17 00:00:00 2001 From: dotasek Date: Tue, 26 Nov 2024 10:33:37 -0500 Subject: [PATCH 1/2] Breaking test --- .../java/ca/uhn/fhir/parser/JsonParserR4Test.java | 9 +++++++++ .../observation-with-contained-specimen.json | 14 ++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 hapi-fhir-structures-r4/src/test/resources/observation-with-contained-specimen.json diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/JsonParserR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/JsonParserR4Test.java index b80251d29108..dc4f6a96e635 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/JsonParserR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/JsonParserR4Test.java @@ -1379,6 +1379,15 @@ public void testPreCommentsToFhirComments() { assertThat(patientString).doesNotContain("fhir_comment"); } + @Test + public void testContainedResourceIdIsReadWithoutAddingHash() throws IOException { + String text = loadResource("/observation-with-contained-specimen.json"); + + Observation observation = ourCtx.newJsonParser().parseResource(Observation.class, text); + assertThat(observation.getSpecimen().getReference()).isEqualTo("#contained-id"); + assertThat(observation.getContained().get(0).getId()).isEqualTo("contained-id"); + } + @DatatypeDef( name = "UnknownPrimitiveType" ) diff --git a/hapi-fhir-structures-r4/src/test/resources/observation-with-contained-specimen.json b/hapi-fhir-structures-r4/src/test/resources/observation-with-contained-specimen.json new file mode 100644 index 000000000000..49a8153cfc71 --- /dev/null +++ b/hapi-fhir-structures-r4/src/test/resources/observation-with-contained-specimen.json @@ -0,0 +1,14 @@ +{ + "resourceType": "Observation", + "id": "O1", + "contained": [ + { + "resourceType": "Specimen", + "id": "contained-id" + } + ], + "status": "final", + "specimen": { + "reference": "#contained-id" + } +} From 7a35d49980b8668f3dd9edcd1deec55b978b2e8f Mon Sep 17 00:00:00 2001 From: dotasek Date: Tue, 26 Nov 2024 18:01:42 -0500 Subject: [PATCH 2/2] Additional testing to demonstrate mutations --- .../ca/uhn/fhir/parser/JsonParserR4Test.java | 110 ++++++++++++++++++ ...ation-with-contained-specimen-hash-id.json | 14 +++ 2 files changed, 124 insertions(+) create mode 100644 hapi-fhir-structures-r4/src/test/resources/observation-with-contained-specimen-hash-id.json diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/JsonParserR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/JsonParserR4Test.java index dc4f6a96e635..3bbf5bd1ea7c 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/JsonParserR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/JsonParserR4Test.java @@ -46,6 +46,7 @@ import org.hl7.fhir.r4.model.Quantity; import org.hl7.fhir.r4.model.QuestionnaireResponse; import org.hl7.fhir.r4.model.Reference; +import org.hl7.fhir.r4.model.Resource; import org.hl7.fhir.r4.model.Specimen; import org.hl7.fhir.r4.model.StringType; import org.hl7.fhir.r4.model.Type; @@ -1385,9 +1386,118 @@ public void testContainedResourceIdIsReadWithoutAddingHash() throws IOException Observation observation = ourCtx.newJsonParser().parseResource(Observation.class, text); assertThat(observation.getSpecimen().getReference()).isEqualTo("#contained-id"); + //FIXME the parsed resource has an added `#` in the contained resource id. assertThat(observation.getContained().get(0).getId()).isEqualTo("contained-id"); } + @Test + public void testContainedResourceIdIsReadWithoutAddingHashThatAlreadyExists() throws IOException { + String text = loadResource("/observation-with-contained-specimen-hash-id.json"); + + Observation observation = ourCtx.newJsonParser().parseResource(Observation.class, text); + // FIXME? This shouldn't be a valid resource, but it's loaded and made into `##contained-id` anyway. + assertThat(observation.getSpecimen().getReference()).isEqualTo("#contained-id"); + assertThat(observation.getContained().get(0).getId()).isEqualTo("#contained-id"); + } + + @Test + public void testReferenceCreatedByStringDoesntMutateContained() { + Observation observation = new Observation(); + observation.setId("123"); + Specimen specimen = new Specimen(); + specimen.setId("contained-id"); + observation.getContained().add(specimen); + observation.setSpecimen(new Reference("#contained-id")); + + String text = ourCtx.newJsonParser().encodeResourceToString(observation); + + assertThat(text).contains("\"reference\":\"#contained-id\""); + assertThat(text).contains("\"id\":\"contained-id\""); + + assertThat(observation.getContained().size()).isEqualTo(1); + /*FIXME despite correctly encoding the contained resource, the original contained resource is mutated and given + an id prefixed with a hash. + */ + assertThat(observation.getContained().get(0).getId()).isEqualTo("contained-id"); + } + + @Test + public void testReferenceCreatedByResourceDoesntMutateContained() { + + Specimen specimen = new Specimen(); + specimen.setId("contained-id"); + + Observation observation = new Observation(); + observation.setId("123"); + observation.getContained().add(specimen); + observation.setSpecimen(new Reference(specimen)); + + String text = ourCtx.newJsonParser().encodeResourceToString(observation); + + assertThat(text).contains("\"reference\":\"#contained-id\""); + assertThat(text).contains("\"id\":\"contained-id\""); + + assertThat(observation.getContained().size()).isEqualTo(1); + /*FIXME despite correctly encoding the contained resource, the original contained resource is mutated and given + an id prefixed with a hash. + */ + assertThat(observation.getContained().get(0).getId()).isEqualTo("contained-id"); + } + + @Test + public void testReferenceCreatedByResourceDoesntMutateEmptyContained() { + Specimen specimen = new Specimen(); + specimen.setReceivedTimeElement(new DateTimeType("2011-01-01")); + + Observation observation = new Observation(); + observation.setId("O1"); + observation.setStatus(Observation.ObservationStatus.FINAL); + observation.setSpecimen(new Reference(specimen)); + + String text = ourCtx.newJsonParser().encodeResourceToString(observation); + + // When encoding, if the reference does not have an id, it is generated in the FhirTerser + String specimenReferenceId = observation.getSpecimen().getResource().getIdElement().getValue(); + assertThat(specimenReferenceId).startsWith("#"); + + // the terser doesn't add a new contained element to the observation + assertThat(observation.getContained()).isEmpty(); + + // However the encoded text contains both a single contained resource, as well as the reference to it. + assertThat(text).contains("\"reference\":\""+specimenReferenceId+"\""); + assertThat(text).contains("\"id\":\""+specimenReferenceId.substring(1)+"\""); + + } + + + @Test + public void testReferenceCreatedByIdTypeDoesntMutateContained() { + Specimen specimen = new Specimen(); + specimen.setId("contained-id"); + specimen.setReceivedTimeElement(new DateTimeType("2011-01-01")); + + Observation observation = new Observation(); + observation.setId("O1"); + observation.setStatus(Observation.ObservationStatus.FINAL); + observation.getContained().add(specimen); + observation.setSpecimen(new Reference(new IdType("#contained-id"))); + + String text = ourCtx.newJsonParser().encodeResourceToString(observation); + + // When encoding, + String specimenReferenceId = observation.getSpecimen().getReference(); + assertThat(specimenReferenceId).startsWith("#"); + + // FIXME why does the terser mutate the contained element of the original resource? + String specimenContainedId = observation.getContained().get(0).getIdElement().getValue(); + assertThat(specimenContainedId).isEqualTo("contained-id"); + + // However the encoded text contains both a single contained resource, as well as the reference to it. + assertThat(text).contains("\"reference\":\""+specimenReferenceId+"\""); + assertThat(text).contains("\"id\":\""+specimenReferenceId.substring(1)+"\""); + + } + @DatatypeDef( name = "UnknownPrimitiveType" ) diff --git a/hapi-fhir-structures-r4/src/test/resources/observation-with-contained-specimen-hash-id.json b/hapi-fhir-structures-r4/src/test/resources/observation-with-contained-specimen-hash-id.json new file mode 100644 index 000000000000..350ef5606790 --- /dev/null +++ b/hapi-fhir-structures-r4/src/test/resources/observation-with-contained-specimen-hash-id.json @@ -0,0 +1,14 @@ +{ + "resourceType": "Observation", + "id": "O1", + "contained": [ + { + "resourceType": "Specimen", + "id": "#contained-id" + } + ], + "status": "final", + "specimen": { + "reference": "#contained-id" + } +}