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

java.lang.ClassCastException: java.lang.String cannot be cast to org.hl7.fhir.instance.model.api.IBase #304

Closed
hugosoares opened this Issue Feb 26, 2016 · 5 comments

Comments

Projects
None yet
2 participants
@hugosoares
Contributor

hugosoares commented Feb 26, 2016

Hi James,
we are experiencing this error while serializing to json or xml:

java.lang.ClassCastException: java.lang.String cannot be cast to org.hl7.fhir.instance.model.api.IBase

the stack trace points at:
ca.uhn.fhir.parser.BaseParser.encodeResourceToString(BaseParser.java:351)

Looks like somehow the parser is expecting an IBase but is getting a String. It should be no problem with the resource that I am serializing becuase everything was fine during the instantiation process.

In this case we are serializing a Bundle with a MessageHeader and Encounter resources.

Tested on HAPI versions 1.2, 1.3 and 1.4

@jamesagnew

This comment has been minimized.

Owner

jamesagnew commented Feb 28, 2016

Hi Hugo,

Would you be able to provide a code snippet illustrating what you're seeing?

@hugosoares

This comment has been minimized.

Contributor

hugosoares commented Feb 29, 2016

Meanwhile I found the reason for this error was because of the name property of the patient resource.
This code is HAPI Fhir running on Javascript (Rhino, Mirth Connect).

var patient = new Packages.ca.uhn.fhir.model.dstu2.resource.Patient();

patient.setName(['Hugo']); //public Patient setName(List<HumanNameDt> theValue)
...
var outcomeString = parser.setPrettyPrint(true).encodeResourceToString(patient);

It is strange that this does not give an error while setting the name, only while encoding to string...

@jamesagnew

This comment has been minimized.

Owner

jamesagnew commented Feb 29, 2016

Hi Hugo,

patient.setName(['Hugo']); looks wrong to me. That method accepts a list of HumanNameDt objects, but you're passing in a list of strings. Java's generics don't enforce their types at runtime, so the error isn't caught until the parser tries to actually use the value later on.

I don't know exactly the right syntax you'd need, but presumably it would be something like

var name = new Packages.ca.uhn.fhir.model.dstu2.composite.HumanNameDt();
name.addGiven('Hugo');
patient.setName([name]); 
@hugosoares

This comment has been minimized.

Contributor

hugosoares commented Mar 1, 2016

I understand the issue with generics... Has an improvement on the parser I think you could control these encoding errors and produce a detailed error message showing exactly wich property is failling during the encoding process.
I see this happening a lot of times during resource instantiation and it will be a pain to check each property set by hand to find the one that is causing the problem...

Keep up the great work.

@jamesagnew

This comment has been minimized.

Owner

jamesagnew commented Mar 2, 2016

Thanks for the suggestion! Checking in a fix now.

@jamesagnew jamesagnew closed this in 94f5068 Mar 2, 2016

jamesagnew added a commit that referenced this issue Mar 2, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment