Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Parser Contained Resource ID fix #6518

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1379,6 +1380,124 @@ 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");
//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"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"resourceType": "Observation",
"id": "O1",
"contained": [
{
"resourceType": "Specimen",
"id": "#contained-id"
}
],
"status": "final",
"specimen": {
"reference": "#contained-id"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"resourceType": "Observation",
"id": "O1",
"contained": [
{
"resourceType": "Specimen",
"id": "contained-id"
}
],
"status": "final",
"specimen": {
"reference": "#contained-id"
}
}
Loading