Skip to content

render an anyOf with at most one object subschema as an untagged enum - #1121

Open
Indian-boult wants to merge 1 commit into
oxidecomputer:mainfrom
Indian-boult:anyof-non-object-branches
Open

Indian-boult wants to merge 1 commit into
oxidecomputer:mainfrom
Indian-boult:anyof-non-object-branches

Conversation

@Indian-boult

Copy link
Copy Markdown
Contributor

Fixes #895.

An anyOf whose subschemas aren't mutually exclusive becomes a struct of #[serde(flatten)] Option<_> members, and serde can flatten only objects. So when a member is a string, a number or a string enum, the type can't be serialized or deserialized. In 0.9 that showed up at runtime. Since typespace checks flattening, 0.10 rejects the schema instead. This is the error for ty's schema.

`PythonPlatform` flattens the property `subtype_0`, whose type does not serialize as an object; serde can flatten only a struct, an enum, or a map

I ran typify 0.9 and 0.10.2 over the schemastore catalog. 29 schemas that 0.9 generated fail on 0.10.2 for this reason alone, among them ruff, uv, ty, pre-commit-hooks, rust-toolchain, maturin and pubspec. Two more, github-action and mikrotik-mcp, still generate but fail at runtime. Their members are types the flatten check lets through, serde_json::Value and a string enum in one and Ipv4Addr and Ipv6Addr in the other, so a plain "bash" or "10.0.0.1" doesn't deserialize.

The change

When at most one subschema can be an object, convert_any_of takes the oneOf path and produces an untagged enum. With at most one object there are no combinations of members to represent, so in terms of the power set approach in #414 the enum of the subschemas is already the full answer. An anyOf with two or more possible objects, such as the one in #710, keeps the flattened struct.

may_be_object decides this conservatively. It follows $ref, type, const, enum, allOf, oneOf and anyOf, and answers yes whenever it can't tell.

The second part is map keys. A map whose propertyNames is a oneOf or anyOf, directly or through a $ref, gets an untagged enum key type, and that enum has no Eq or Hash, so the generated HashMap doesn't compile. That already happens on main for oneOf, and without this the anyOf change would turn two schemas, crucible-code-schema and ttheme-work, from a clean error into code that doesn't build. Such keys now use String, which every JSON object key is.

Variants keep schema order, the same as for a oneOf. For ty's PythonPlatform, that means "all" deserializes as the free-form string variant rather than the All variant. No data is lost and it round-trips, but it's less precise than it could be. I left the order alone to match oneOf.

Testing

tests/schemas/any-of-non-object.json covers the ty, ruff and uv shapes, both kinds of constrained map key, and a two-object anyOf that stays a struct. Without the convert_any_of change the schema test fails with the error above, and without the key change the output switches to an EnvDirectKey key type.

Outside the repo I also checked the points below.

  • No existing fixture output changes, and the workspace tests, clippy and docs pass.
  • Ruff, uv, ty, pre-commit-hooks, rust-toolchain, crucible-code-schema, ttheme-work, github-action, maturin and pubspec all generate and compile together.
  • The fixture types round-trip real values, for example 88 and "dynamic" for the ruff line width, and both the string and object forms of the uv build constraint, while 0 is rejected.
  • Every other schemastore schema that main generates produces the same output byte for byte, apart from github-action and mikrotik-mcp, and so do 26 Oxide OpenAPI documents.

An anyOf whose subschemas aren't mutually exclusive becomes a struct of
flattened Option members, and serde can flatten only objects. When at
most one subschema can be an object, nothing is lost by treating the
anyOf as a oneOf, so it now becomes the same untagged enum. 0.10
rejected these schemas outright, among them the ruff, uv, ty,
pre-commit-hooks and rust-toolchain schemas on schemastore.

A map whose propertyNames is a oneOf or anyOf now uses String keys,
since the untagged enum for the key has no Eq or Hash.
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.

anyOf over string schemas generates #[serde(flatten)] on String, causing runtime error “can only flatten structs and maps (got a string)”

1 participant