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

New json deserializer silently accepts duplicate choice type properties of differing types #2662

Closed
almostchristian opened this issue Jan 11, 2024 · 0 comments · Fixed by #2722

Comments

@almostchristian
Copy link
Contributor

Describe the bug
The new System.Text.Json based deserializer silently accepts duplicate choice types if they are of different types. The old deserializer fails with an exception. From what I observed, it will use the first property encountered and ignore the rest.

To Reproduce
Reproduction code:

var scenario = """
    {
      "resourceType": "Patient",
      "deceasedBoolean": true,
      "deceasedDateTime": "2022"
    }
    """;

var jsonSerializerOptions = new JsonSerializerOptions().ForFhir();

// successfully parses with new deserializer
var patient = JsonSerializer.Deserialize<Patient>(scenario, jsonSerializerOptions);

Console.WriteLine("New deserializer: " + patient.ToJson());

// fails with old jsonParser
try
{
    patient = new FhirJsonParser().Parse<Patient>(scenario);
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString());
}

Output:

New deserializer: {"resourceType":"Patient","deceasedBoolean":"2022"}
System.FormatException: While building a POCO: Element 'deceased' must not repeat (at Patient.deceasedDateTime[0])
   at Hl7.Fhir.Serialization.ComplexTypeReader.RaiseFormatError(String message, String location)
   at Hl7.Fhir.Serialization.ComplexTypeReader.read(ClassMapping mapping, IEnumerable`1 members, Base existing)
   at Hl7.Fhir.Serialization.ComplexTypeReader.Deserialize(ClassMapping mapping, Base existing)
   at Hl7.Fhir.Serialization.PocoBuilder.<>c__DisplayClass9_0.<BuildFrom>g__build|0()
   at Hl7.Fhir.Serialization.PocoBuilder.BuildFrom(ITypedElement source)
   at Hl7.Fhir.Serialization.PocoBuilder.BuildFrom(ISourceNode source, ClassMapping mapping)
   at Hl7.Fhir.Serialization.PocoBuilder.BuildFrom(ISourceNode source, Type dataType)
   at Hl7.Fhir.ElementModel.PocoBuilderExtensions.ToPoco(ISourceNode source, ModelInspector inspector, Type pocoType, PocoBuilderSettings settings)
   at Hl7.Fhir.Serialization.BaseFhirParser.Parse(ISourceNode node, Type type)
   at Hl7.Fhir.Serialization.FhirJsonParser.Parse(String json, Type dataType)
   at Hl7.Fhir.Serialization.FhirJsonParser.Parse[T](String json)
   at Program.<Main>$(String[] args) in D:\repos\ConsoleApp1\Program.cs:line 26

Expected behavior
An exception should be thrown similar to the old deserializer.

Version used:

  • Version: 5.5.0
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

Successfully merging a pull request may close this issue.

2 participants