-
Notifications
You must be signed in to change notification settings - Fork 190
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
Cannot convert nested resource from protobuf to JSON #25
Comments
Doing a little more research, I printed the (non FHIR compliant) JSON representation of the protobuf that worked with Any, and got this:
I was able to go directly from FHIR JSON to protobuf and back just fine, and the resource type looks like this instead:
code snippet:
So even though the original protobuf seemed to work OK, it looks like it should have been a |
Confirmed, packing
This might still be considered a bug though with the protobuf types (e.g. |
Ah, actually it's still invalid JSON - notice the trailing comma in This could be because the Parameters type is empty, but it's unclear to me if that's valid or not - Parameters only requires a list of |
And with some further testing, using |
I believe this should fix it: diff --git a/py/google/fhir/json_format/_json_printer.py b/py/google/fhir/json_format/_json_printer.py
index ea224565..19f1e617 100644
--- a/py/google/fhir/json_format/_json_printer.py
+++ b/py/google/fhir/json_format/_json_printer.py
@@ -364,14 +364,18 @@ class JsonPrinter:
"""Prints the representation of message."""
self.generator.open_json_object()
+ set_fields = msg.ListFields()
+
# Add the resource type preamble if necessary
if (annotation_utils.is_resource(msg) and
self.json_format == _FhirJsonFormat.PURE):
- self.generator.add_field('resourceType', f'"{msg.DESCRIPTOR.name}",')
- self.generator.add_newline()
+ self.generator.add_field('resourceType', f'"{msg.DESCRIPTOR.name}"')
+
+ if len(set_fields):
+ self.generator.push(',')
+ self.generator.add_newline()
# print all fields
- set_fields = msg.ListFields()
for (i, (set_field, value)) in enumerate(set_fields):
if (annotation_utils.is_choice_type_field(set_field) and
self.json_format == _FhirJsonFormat.PURE): I hope you don't mind I didn't make a PR because wrapping my head around the test suite and getting things to pass will take a bit more time, but if there's no movement on this for a while I'll look into doing a complete PR, maybe next week. |
Awesome, great find and great fix. Unfortunately we're not really set up to accept PRs quite yet, so we'll handle on our end, but I really appreciate you digging in and making our job easier :) |
Sounds good. And actually that diff should just fix the JSON parsing, but I still wonder if |
So, short story: |
OK, that's interesting. So it sounds like, aside from the bug I found a fix for, there's still a bug in converting to JSON (If you give it an |
FYI the fix to this change has now been pushed to HEAD - we'll cut a release this afternoon that contains this bugfix. |
RE: If you give it an Any it doesn't know how to convert to a contained resource and just ends up being empty This is actually working as intended. The usage of |
Ah, that's awesome, thanks for fixing that! OK, thanks for the clarification on |
@nickgeorge I was looking into updating to get this bug fix, but it looks like 0.6.2 is still the latest version, which still had that bug in it. Was there something holding back the release? |
Uh sorry, we wanted to finalize some testing procedures for the pypi distribution so got delayed a bit. Shld be today if things go according to plan... |
Thanks, no worries. I'll keep an eye out |
…ith incorrect trailing comma. See #25 PiperOrigin-RevId: 380668911
There are likely other places where this would also be the case, but I found it when using
resource
inParameters.Parameter
. Trying to pass a resource (e.g.Parameters
) intoresource
throws an error, and if you do it directly mypy will give you an error as well. From what I can tell the proper way is to pack it into anAny
protobuf, but then when printing to JSON it prints invalid JSON.Here's some sample code that reproduces the issue:
and the output:
If you take out the variable and hardcode
nested_resource
toParameters()
, mypy will also give you an error:The text was updated successfully, but these errors were encountered: