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

Deserialization of Fhir object requires elements beyond "resourceType" when using JsonSerializer.Deserialize #2391

Closed
cames-be opened this issue Feb 20, 2023 · 3 comments
Assignees

Comments

@cames-be
Copy link

Describe the bug
The Fhir specification states that the "resourceType" element is a required element for a resource, but no other elements are required. When deserializing a resource using the FhirJsonParser.Parse function only the "resourceType" element is required to be present. When attempting to deserialize the same resource using the JsonSerializer.Deserialize function, an exception is thrown that states An object needs to have at least one property. This goes against the guidance in the developer guide http://hl7.org/fhir/r4/overview-dev.html since the "data" or subsequent elements are Optional

Each instance of a resource consists of:

  • resourceType (line 2) - Required: FHIR defines many different types of resources. See the full index
  • id (line 3) - The id of this resource. Always present when a resource is exchanged, except during the create operation (below)
  • meta (lines 4 - 7) - Usually Present: Common use/context data to all resources and managed by the infrastructure. Missing if there is no metadata
  • text (lines 8 - 11) - Recommended: XHTML that provides a human readable representation for the resource
  • extension (lines 12 - 17) - Optional: Extensions defined by the extensibility framework
  • data (lines 18 - 43) - Optional: data elements - a different set, defined for each type of resource

To Reproduce
The following code will reproduce the behavior:

string str = "{\"resourceType\": \"Parameters\"}";

// This will deserialize fine
var works = new FhirJsonParser().Parse<Parameters>(str);

JsonSerializerOptions options = new JsonSerializerOptions().ForFhir(typeof(Parameters).Assembly);
// This will throw an exception
var throws = JsonSerializer.Deserialize<Parameters>(str, options);

Expected behavior
The JsonSerializer.Deserialize function should not throw an exception and instead should return a deserialized resource with no populated elements.

Screenshots
image

Version used:
R4

  • 4.3.0

Additional context
The ERR.OBJECTS_CANNOT_BE_EMPTY error should not be added to the state.Errors collection https://github.com/FirelyTeam/firely-net-common/blob/develop/src/Hl7.Fhir.Support.Poco/Serialization/FhirJsonPocoDeserializer.cs#L285

@marcovisserFurore
Copy link
Member

This is due to constraint ele-1, but it is actually the responsibility of the validator instead of serializer. We have to research this.

@ewoutkramer
Copy link
Member

See #2235.

@ewoutkramer
Copy link
Member

See #2550.

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

No branches or pull requests

3 participants