Add OnUnresolvedSubtype callback to detect unregistered subtypes (#112) - #193
Open
manuc66 wants to merge 4 commits into
Open
Add OnUnresolvedSubtype callback to detect unregistered subtypes (#112)#193manuc66 wants to merge 4 commits into
manuc66 wants to merge 4 commits into
Conversation
Fires once per JSON element whose subtype cannot be resolved (unknown or missing discriminator value, or no matching property in property-presence mode), on both the Newtonsoft.Json and System.Text.Json backends. The callback receives the parent type, discriminator name/value, whether the discriminator was present, and the fallback subtype that will be used. Extends the existing fallback point in ResolveType/GetType so no behavior changes when no callback is registered. #112
Covers resolved subtype (no callback), unknown discriminator value, missing discriminator, no configured fallback, one callback per unresolved element in a nested tree, and property-presence mode. #112
…olvedSubtypeInfo The public API keeps the README as its documentation; no XML doc comments on fluent builder methods or value classes, consistent with the rest of the repo.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #112
Problem
The reporter of #112 wants to detect, while deserializing a polymorphic tree, every object whose
Typediscriminator is not registered — without throwing on the first mismatch. Today the only way is a "trap" fallback type: its constructor records each unknownTypeinto a static list, which is not thread-safe and requires cleanup after deserialization.Fix
Adds
OnUnresolvedSubtypeto all four builders (JsonSubtypesConverterBuilder/JsonSubtypesWithPropertyConverterBuilder, in bothJsonSubTypesandJsonSubTypes.Text.Json). The callback fires once per JSON element whose subtype cannot be resolved: unknown or missing discriminator value, or no matching property in property-presence mode.The hook sits at the existing single failure point in both backends (
resolvedType ?? GetFallbackSubType(...)), so nothing changes when no callback is registered.UnresolvedSubtypeInfoexposes the parent type, the discriminator name and value, aHasDiscriminatorflag (to distinguish a missing discriminator from an unknown value, both otherwise read asnull) and the fallback subtype that will be used (nullwhen none is configured).Tests
JsonSubTypes.Text.Json.Tests: 139 pass on net8.0 and net10.0. Six new tests: no callback when the subtype resolves, unknown value, missing discriminator, no fallback configured, one callback per unresolved element in a nested tree, property-presence mode.JsonSubTypes.Tests(net46): compiles, but the NUnit suite cannot execute under Linux/VSTest, so I validated the same six scenarios through a net8.0 harness against the netstandard2.0 build (all pass).Honest notes
JsonPathis available in the Newtonsoft backend but not in System.Text.Json (resolution works on aJsonDocument, which carries no path), so I omitted it from the info type.Feedback welcome on the API shape (method name, and what
UnresolvedSubtypeInfoexposes).