Skip to content

fix: keep arbitrary keys on an unconstrained object union member (#70) - #71

Closed
lightsofapollo wants to merge 1 commit into
mainfrom
fix/unconstrained-object-union-member
Closed

fix: keep arbitrary keys on an unconstrained object union member (#70)#71
lightsofapollo wants to merge 1 commit into
mainfrom
fix/unconstrained-object-union-member

Conversation

@lightsofapollo

Copy link
Copy Markdown
Contributor

Fixes #70.

The bug

Every member of a type: [string, number, boolean, object, array, "null"] union shares a single SchemaDetails, so the object member can carry no object shape at all. build_typed_multi_union_variant still routed it through add_allocated_object_schema, which projected it as a closed, empty struct:

pub struct GetEventResponsePayloadObject {}

{"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's Object arm checks should_use_dynamic_json before building a struct. The typed-multi path had no equivalent check. It couldn't reuse is_dynamic_object_pattern directly either, because that starts from schema.schema_type(), which on a TypedMulti reports only the first non-null member (string, here).

So the object-shape half of is_dynamic_object_pattern is split into object_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:

///Object variant in union
pub type GetEventResponsePayloadObject = std::collections::BTreeMap<String, serde_json::Value>;

#[serde(untagged)]
pub enum GetEventResponsePayload {
    String(String),
    Number(f64),
    Boolean(bool),
    GetEventResponsePayloadObject(GetEventResponsePayloadObject),
    GetEventResponsePayloadArray(GetEventResponsePayloadArray),
}

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::Value would 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 (including false, which stays a closed empty struct), patternProperties, propertyNames, minProperties/maxProperties, dependentRequired/dependentSchemas, if/then/else.

Verification

tests/unconstrained_object_union_member_test.rs:

  • the generated shape for an unconstrained member, and that it is not a struct;
  • a member with properties and a member with additionalProperties: false still generate structs;
  • a scratch-crate test that compiles the generated code and round-trips {"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, and cargo fmt --check are clean.

Follow-up (not in this PR)

Found while verifying: the anyOf/oneOf branch path still gives a bare {"type": "object"} branch a serde_json::Value alias, which is greedy in an untagged enum — with anyOf: [{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

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
@vercel

vercel Bot commented Sep 8, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
openapi-to-rust Ready Ready Preview Sep 8, 2026 1:03pm UTC

Request Review

@lightsofapollo

Copy link
Copy Markdown
Contributor Author

Folding this into #72 so everything from this investigation lands as one reviewable change — #72 now targets main and contains this commit plus the related union fixes. Closing in favor of that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug]: Unconstrained object in a multi-type schema silently drops properties

1 participant