hapifhir / hapi-fhir

🔥 HAPI FHIR - Java API for HL7 FHIR Clients and Servers
http://hapifhir.io
Apache License 2.0
2k stars 1.31k forks source link

ID is always added to output even input message doesn't have id #1342

Open kthaya opened 5 years ago

kthaya commented 5 years ago

Describe the bug When a message string converted from JSON to XML or XML to JSON, ID is always added to output even input message doesn't have id. I don't see anywhere in FHIR specification to mention that ID is mandatory.

To Reproduce

  1. Create a JSON/XML message string without ID string
  2. Parse the message with hapi library
  3. Output contains ID tag

Expected behavior Output should not contain ID tag

Environment (please complete the following information):

jamesagnew commented 5 years ago

I'm not sure I follow- Could you please provide a code snippet which demonstrates this issue?

kthaya commented 5 years ago
import odl.ca.uhn.fhir_2_5.context.FhirContext;
import odl.org.hl7.fhir_2_5.dstu3.model.Bundle;
import odl.ca.uhn.fhir_2_5.parser.IParser;

public class HapiSetIdTest {

    public static void main(String[] args) throws Exception {
        FhirContext ctxDstu2 = FhirContext.forDstu3();

        String input = "<Bundle xmlns=\"http://hl7.org/fhir\"><id value=\"bundle-example\" /><entry><fullUrl value=\"https://example.com/base/MedicationRequest/3123\"/>"
                + "<resource><MedicationRequest></MedicationRequest></resource></entry></Bundle>";
        IParser parser = ctxDstu2.newXmlParser();

        Bundle output = (Bundle) parser.parseResource(input);

        System.out.println(parser.encodeResourceToString(output));

    }

}

Question is why output has id tag \\ while input doesn't have?

Output

<Bundle xmlns="http://hl7.org/fhir"><id value="bundle-example"></id><entry><fullUrl value="https://example.com/base/MedicationRequest/3123"></fullUrl><resource><MedicationRequest xmlns="http://hl7.org/fhir"><id value="3123"></id></MedicationRequest></resource></entry></Bundle>

Input

<Bundle xmlns="http://hl7.org/fhir"><id value="bundle-example"/><entry><fullUrl value="https://example.com/base/MedicationRequest/3123"/><resource><MedicationRequest></MedicationRequest></resource></entry></Bundle>
jamesagnew commented 5 years ago

That 3123 comes from the fullUrl tag. It is by design, since that is the purpose of that field.

kthaya commented 5 years ago

Thanks, when I look at FHIR spec identity is still optional (0..1) - https://www.hl7.org/fhir/medicationrequest-definitions.html#MedicationRequest.identifier

jamesagnew commented 5 years ago

FYI: MedicationRequest.identifier is different from MedicationRequest.id

The id field is optional too, but certainly in this specific use case, if you're parsing a Bundle that provides an ID via the fullUrl field, I believe it's the right behaviour to respect it. Can you not just clear it if manually after parsing you need different behaviour?