-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
encodeResourceToString not encoding contained resources #1186
Comments
Hi Michael, Can you please provide a complete code sample and/or unit test showing this issue? |
Hi James, Something like this Unit test should do the trick
|
this issue explains: #1184 |
PS: log outout from the above test is:
|
Oops, just investigated a bit further. Turns out it all comes down to me forgetting a "#" character in the reference to the contained resource. This means the BaseParser.containResourcesForEncoding will not pick up the reference and thus will never call ContainedResources.addContained. This in turn results in ContainedResources.myResourceList remaining empty. Not sure if this is by design: That an unreferenced contained resource doesn't get encoded? |
That is actually a deliberate decision- If a contained resource has nothing linking to it, it is not valid per FHIR's rules so HAPI FHIR omits it during parsing/serialization. |
My issue was as far as i can judge that the parser did not omit the contained resources. I am using the strict parser and there were no complaints over these references and contained resources. Furthermore, inspecting the parsed resource (in the debugger) it did have all the contained resources. So nothing was thrown away by the parser. That made me think all was ok with the input and i went looking for the error elsewhere. Only during subsequent serialization did the contained resources disappear. And that in a totally quiet way. I may be digressing but: |
If it is not valid, it should at least log an error in lenient mode. In strict mode i also would have expected an DataFormatException to be thrown. |
I've also been bitten by this just now... a server we are calling has IDs like this:
Since the |
I too was bitten by this last week. @tadgh note that / is not a valid character in an id - see https://www.hl7.org/fhir/datatypes.html#id |
Yeah i know, just took me a while to catch it, since I am not the steward of this data and can't control its shape. All I can really do here is send a pointed email asking for a fix. |
Just hit us also, we did some digging and found it was indeed the ID of the resource missing the # prefix, which is very easy to do when adding resources to a ListResource (as you wouldn't adjust the IDs, since you don't when adding to a Bundle). Dropping resources silently is a very dangerous approach. |
I didn't think we still did. What version of HAPI FHIR are you using? If not 6.8.0 can you please upgrade and try again? If it's still happening, can you please provide a minimal test case? |
@jamesagnew I've tried a simple test to see how the resource without # prefix is handled in 6.8.0. It seems to me it's a still a bug. Please find the attached for a test case. |
I've run into another flavor of this problem. For adaptive forms a Questionnaire resource is contained inside a QuestionnaireResponse. The reference to the Questionnaire is the "questionnaire" field, i.e. "#questionnaireId", which is a canonical and not a reference type. So HAPI discards the contained resource when encoding to JSON. A way to bypass this behavior sure would be nice. |
Describe the bug
Contained resources are not encoded when using encodeResourceToString to encode a resource. Investigating the issue with a debugger I found that the issue seems to stem from line 280 in JsonParser.java:
List containedResources = getContainedResources().getContainedResources();
The double invocation of "getContainedResources" will always return a zero length list.
To Reproduce
Make a call like this:
FhirContext.forR4().newJsonParser().setPrettyPrint(true).encodeResourceToString(aFhirResource));
Where "aFhirResource" is a resource with contained resources. The resulting string will not have any contained resources.
Expected behavior
Contained resources are encoded to string.
Environment (please complete the following information):
The text was updated successfully, but these errors were encountered: