fix(ack): align discriminator errors across parse and encode#111
Merged
Conversation
Encode was returning aggregated nested errors when the discriminator
was missing, malformed, or unknown, while parse returned focused
SchemaConstraintsErrors. The two paths now share
`_validateDiscriminatorKey`, so encode emits the same required/invalid-
type/enum constraint error users already see at parse time.
Encode still short-circuits to the named branch when the runtime map
carries the discriminator, and still tries each branch for model- or
codec-backed runtimes that synthesize the discriminator during encode.
A new `_encodeBranch` helper centralizes the per-branch try/catch and
the boundary checks ("must emit key", "value matches branch label")
that the fast and slow paths both perform.
Adds regression coverage for parse/encode error parity, a map-runtime
codec that emits the discriminator, and the three focused encode error
shapes (missing, non-string, unknown) for union-owned discriminators.
|
To view this pull requests documentation preview, visit the following URL: Documentation is deployed and generated using docs.page. |
The previous commit added `_matchesAnyBranchRuntime` plus a map-block in `validateRuntimeWithContext` so that calling validateRuntime directly stayed consistent with what encode would do for a map missing the discriminator. The consistency was edge-case and the cost was ~21 lines that most users never observe. Drop both. The encode fast path now calls `_validateDiscriminatorKey` inline to keep focused errors for non-string and unknown discriminator values (this used to fall out of the validateRuntime block). validateRuntime called directly is now stricter than encode for the specific case of "a JsonMap missing the discriminator key", which is a defensible invariant: a valid discriminated runtime carries its label. Behavior preserved: PR #107 union-owned discriminator (branches that omit the property) still works via `effectiveBranch`, and PR #109 codec branches that synthesize the discriminator at encode time still succeed through the slow path. Net: -33 lines on this PR.
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.
Summary
SchemaConstraintsErrors as parse for missing, non-string, and unknown discriminator values, by sharing_validateDiscriminatorKeybetween the parse path andvalidateRuntimeWithContext._encodeBranchto dedupe the per-branchtry/catch → encoderThrewand the boundary checks ("must emit key", "value matches branch label") that the fast and slow paths both run.Test plan
dart test test/schemas/discriminated_object_schema_test.dart— 27/27dart test(full ack suite) — 898/898dart analyze --fatal-infos— cleandart format --set-exit-if-changed— clean