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

UndeclaredExtension (added by addExtension method) does not serialize to JSON format properly #326

Closed
fwang0 opened this issue Apr 5, 2016 · 4 comments

Comments

@fwang0
Copy link

fwang0 commented Apr 5, 2016

This bug is found in version 1.4.

The following test code in PatientProvider would generate correct XML document, but wrong JSON document. Note the valueReference in JSON format contains not reference with id.

@READ()
public Patient read(@IdParam IdType theId) {
Patient patient = new Patient();
patient.setBirthDate(new Date());
patient.addExtension().setUrl("test").setValue(new Reference(new Condition()));
return patient;
}

XML:



    </valueReference>

</extension>
<contained>
    <Condition xmlns="http://hl7.org/fhir">
        <id value="1"/>

    </Condition>

</contained>
<birthDate value="2016-03-30"/>

JSON:

{
  "resourceType": "Patient",
  "extension": [
    {
      "url": "test",
      "valueReference": {}
    }
  ],
  "contained": [
    {
      "resourceType": "Condition",
      "id": "1"
    }
  ],
  "birthDate": "2016-03-30"
}
@fwang0
Copy link
Author

fwang0 commented Apr 5, 2016

The following change in JsonParser.java solves the problem. Can you verify if the fix is correct?

diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java
index a3c8a9a..f69ccb6 100644
--- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java
+++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java
@@ -1509,7 +1509,7 @@ public class JsonParser extends BaseParser implements IParser {
                                if (childName == null) {
                                        childName = "value" + myContext.getElementDefinition(value.getClass()).getName();
                                }
-                               BaseRuntimeElementDefinition<?> childDef = myContext.getElementDefinition(value.getClass());
+                               BaseRuntimeElementDefinition<?> childDef = extDef.getChildElementDefinitionByDatatype(value.getClass());
                                if (childDef == null) {
                                        throw new ConfigurationException("Unable to encode extension, unregognized child element type: " + value.getClass().getCanonicalName());
                                }

The result is:
{ "resourceType": "Patient", "extension": [ { "url": "test", "valueReference": { "reference": "#1" } } ], "contained": [ { "resourceType": "Condition", "id": "1" } ], "birthDate": "2016-04-05" }

@jamesagnew
Copy link
Collaborator

I applied the patch, and it works perfectly. Awesome!

I'll commit this shortly, and make sure you get the credit. :)

@jamesagnew
Copy link
Collaborator

Ps- I always try to make sure that any who contributes patches gets credit in our changelog. If you want anything other than your github handle listed please feel free to let me know.

@fwang0
Copy link
Author

fwang0 commented Apr 5, 2016

My github handle works great :) Thanks James!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants