fix: keep arbitrary keys on an unconstrained object union member (#70) - #71
Closed
lightsofapollo wants to merge 1 commit into
Closed
fix: keep arbitrary keys on an unconstrained object union member (#70)#71lightsofapollo wants to merge 1 commit into
lightsofapollo wants to merge 1 commit into
Conversation
All members of a `type: [...]` union share one `SchemaDetails`, so the
`object` member may carry no object shape at all. It was still routed
through `add_allocated_object_schema`, which projected it as a closed,
empty struct: `{"payload": {"key": "value"}}` deserialized fine and then
serialized back as `{"payload": {}}`, silently dropping every key.
The single-type path already guards this with `should_use_dynamic_json`;
the typed-multi path did not. Split the object-shape half of
`is_dynamic_object_pattern` into `object_shape_is_unconstrained` so the
union member can ask the same question — `schema_type()` reports only
the first non-null member of a `type: [...]` list, so that caller cannot
reuse the type check — and give an unconstrained member a
`BTreeMap<String, serde_json::Value>` carrier. Unlike `serde_json::Value`
the map matches only objects, so the untagged enum keeps routing arrays
and scalars to their own members.
Members the spec does shape (`properties`, `required`,
`additionalProperties`, `minProperties`/`maxProperties`, and the rest)
keep their generated structs.
The regression test compiles the generated code in a scratch crate and
round-trips every member of the issue's union.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012mBAVbYp2iUSL8VwpyDf19
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
Author
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.
Fixes #70.
The bug
Every member of a
type: [string, number, boolean, object, array, "null"]union shares a singleSchemaDetails, so theobjectmember can carry no object shape at all.build_typed_multi_union_variantstill routed it throughadd_allocated_object_schema, which projected it as a closed, empty struct:{"payload":{"key":"value"}}deserialized without error and then serialized back as{"payload":{}}— every key silently dropped. Scalars and the array member were fine.Root cause
The single-type path already guards this:
analyze_single_typed_schema'sObjectarm checksshould_use_dynamic_jsonbefore building a struct. The typed-multi path had no equivalent check. It couldn't reuseis_dynamic_object_patterndirectly either, because that starts fromschema.schema_type(), which on aTypedMultireports only the first non-null member (string, here).So the object-shape half of
is_dynamic_object_patternis split intoobject_shape_is_unconstrained(&SchemaDetails), which asks the question without the type check, and both callers use it.The fix
An unconstrained object member now gets a map carrier instead of a struct:
BTreeMap<String, serde_json::Value>is the shape this codebase already uses for an untyped map (UntypedShape::ValueMap, the#[serde(flatten)]additional-properties carrier). Critically it matches only a JSON object, so the untagged enum keeps routing arrays and scalars to their own members —serde_json::Valuewould swallow them. The generated type name is unchanged, so only its definition moves.Members the spec does shape are untouched and keep their generated structs:
properties,required,additionalProperties(includingfalse, which stays a closed empty struct),patternProperties,propertyNames,minProperties/maxProperties,dependentRequired/dependentSchemas,if/then/else.Verification
tests/unconstrained_object_union_member_test.rs:propertiesand a member withadditionalProperties: falsestill generate structs;{"key":"value"},{"nested":{"a":[1,2]},"b":null},{},"text",1.5,true,["a",1],[]through the union, and asserts an object lands in the object member.cargo test --all-features,cargo clippy --all-features -- -D warnings, andcargo fmt --checkare clean.Follow-up (not in this PR)
Found while verifying: the
anyOf/oneOfbranch path still gives a bare{"type": "object"}branch aserde_json::Valuealias, which is greedy in an untagged enum — withanyOf: [{type: object}, {type: string}]a JSON string deserializes into the object branch. No data loss, but the wrong variant is selected. Tracked separately; the same map carrier is the fix.🤖 Generated with Claude Code
https://claude.ai/code/session_012mBAVbYp2iUSL8VwpyDf19