fix(ack): synthesize union-owned discriminator on encode for codec branches#113
Merged
Conversation
…anches Codec branches whose encoder omits the union-owned discriminator could be decoded but not re-encoded: the effective branch injected the discriminator as a bare required literal that the codec's intermediate map lacked, so ObjectSchema encode-validation failed. Carry the synthesized value in an internal ObjectSchema.encodeOnlyDefaults map, set only on codec-backed branches (gated by a CodecSchema on the wrapper spine via the threaded underCodec flag). Plain branches keep the bare required literal, so encoding a payload missing the discriminator still fails — no automatic branch selection. The field is excluded from ==/hashCode/toMap, so effective branches stay equal to authored twins and JSON Schema export is unchanged (discriminator stays required, no default).
|
To view this pull requests documentation preview, visit the following URL: Documentation is deployed and generated using docs.page. |
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.
Problem
A discriminated-union codec branch whose encoder omits the union-owned discriminator could be decoded but not re-encoded — a broken round-trip:
The effective branch injects the discriminator as a bare required literal; for a codec branch the intermediate map omits it, so
ObjectSchemaencode-validation fails. (This is theflutter_codecuse case; the generated map-backed extension-type path is unaffected because it carries the discriminator in the map.)Fix
Synthesize the discriminator on encode for codec branches only:
ObjectSchema.encodeOnlyDefaultsmap supplies a property value when the key is absent on encode.underCodecflag threaded througheffectiveDiscriminatedBranch— flipped true only when aCodecSchemasits on the wrapper spine.==/hashCode/toMap, so effective branches stay equal to authored twins and JSON Schema export is unchanged (discriminator staysrequired, nodefault) with zero schema-model changes.Tests
Added: codec-omits-discriminator encode + round-trip,
passthrough()+codec, multi typed-codec branch selection, nestedDefault/Codecgating, and a plain-branch regression lock.Verified locally:
dart analyze packages/ack/lib— no issuesacksuite — 921 passedack_json_schema_builder— 49 passedack_generatordiscriminated integration — 19 passed