firely-net-sdk icon indicating copy to clipboard operation
firely-net-sdk copied to clipboard

Json serialization with `_summary=true` results in empty objects for primitive fields with extensions

Open almostchristian opened this issue 6 months ago • 2 comments

Describe the bug Extensions on primitive elements when serialized with a summary serialization filter results in empty objects {} which fails in roundtrip.

To Reproduce


var patient = new Patient
{
    BirthDateElement = new Date("1990")
    {
        Extension = [new Extension("birthTime", new Instant(DateTimeOffset.Now))]
    }
};

var jsonOptions = new JsonSerializerOptions()
    .ForFhir(new FhirJsonPocoSerializerSettings { SummaryFilter = SerializationFilter.ForSummary() })
    .Pretty();

var serialized = JsonSerializer.Serialize(patient, jsonOptions);

Console.WriteLine(serialized);

// Throws an exception
var deserialized = JsonSerializer.Deserialize<Patient>(serialized, jsonOptions);

Console.WriteLine(deserialized.ToJson());

Serialized json:

{
  "resourceType": "Patient",
  "birthDate": "1990",
  "_birthDate": {}
}

Exception:

Hl7.Fhir.Serialization.DeserializationFailedException: One or more errors occurred. (An object needs to have at least one property. At Patient.birthDate, line 5, position 2)
   at Hl7.Fhir.Serialization.PocoSerializationEngine.deserializeAndFilterErrors(BaseFhirJsonPocoDeserializer deserializer, Utf8JsonReader& reader)
   at Hl7.Fhir.Serialization.PocoSerializationEngine.DeserializeFromJson(Utf8JsonReader& reader)
   at Hl7.Fhir.Serialization.FhirJsonConverter`1.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo`1 jsonTypeInfo, Nullable`1 actualByteCount)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 json, JsonTypeInfo`1 jsonTypeInfo)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
   at Program.<Main>$(String[] args) in D:\Repos\TestFhirSerialization\TestFhirSerialization\Program.cs:line 25

Version used:

  • Version: 5.11.6

almostchristian avatar May 16 '25 02:05 almostchristian

Refinement: Might be tricky, Once we look at children of an element, we already have serialized the parent. So it might be hard and expensive to calculate if an element is empty. This is not straightforward.

Pragmatic solution: we only check this for complex primitive elements (with extensions or id's)

mmsmits avatar May 21 '25 14:05 mmsmits

I think the pragmatic solution you presented should suffice in my opinion

almostchristian avatar May 21 '25 17:05 almostchristian