The current OneOfConverter fails to process JSON object tokens. It attempts to deserialize every union member type, and if a type succeeds this is considered to be the correct value. Because member types are deserializing without error in some cases, despite not matching the shape of the provided JSON, the converter is returning an incorrect value.
This PR attempts to resolve this using the following logic:
Names of any non-optional property on the union members types are cached.
Upon being requested to deserialize a JSON object node, a JsonDocument is created.
The property names of cached member types are sequentially checked to ensure they exist in the document.
The first type in which all the property names are present is considered to be correct, and the document is deserialized.
It's not a particularly efficient mechanism. As far as I can tell the limitations on the Utf8JsonReader prevent peeking forward to look at the shape, which is why I've chosen this route. Very open to suggestions.
The current OneOfConverter fails to process JSON object tokens. It attempts to deserialize every union member type, and if a type succeeds this is considered to be the correct value. Because member types are deserializing without error in some cases, despite not matching the shape of the provided JSON, the converter is returning an incorrect value.
This PR attempts to resolve this using the following logic:
JsonDocument
is created.It's not a particularly efficient mechanism. As far as I can tell the limitations on the
Utf8JsonReader
prevent peeking forward to look at the shape, which is why I've chosen this route. Very open to suggestions.