Repository navigation
render an anyOf with at most one object subschema as an untagged enum - #1121
Open
Indian-boult wants to merge 1 commit into
Open
Indian-boult wants to merge 1 commit into
Indian-boult wants to merge 1 commit into
Conversation
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.
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 #895.
An
anyOfwhose 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.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::Valueand a string enum in one andIpv4AddrandIpv6Addrin 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_oftakes theoneOfpath 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. AnanyOfwith two or more possible objects, such as the one in #710, keeps the flattened struct.may_be_objectdecides this conservatively. It follows$ref,type,const,enum,allOf,oneOfandanyOf, and answers yes whenever it can't tell.The second part is map keys. A map whose
propertyNamesis aoneOforanyOf, directly or through a$ref, gets an untagged enum key type, and that enum has noEqorHash, so the generatedHashMapdoesn't compile. That already happens on main foroneOf, and without this theanyOfchange would turn two schemas, crucible-code-schema and ttheme-work, from a clean error into code that doesn't build. Such keys now useString, which every JSON object key is.Variants keep schema order, the same as for a
oneOf. For ty'sPythonPlatform, that means"all"deserializes as the free-form string variant rather than theAllvariant. No data is lost and it round-trips, but it's less precise than it could be. I left the order alone to matchoneOf.Testing
tests/schemas/any-of-non-object.jsoncovers the ty, ruff and uv shapes, both kinds of constrained map key, and a two-objectanyOfthat stays a struct. Without theconvert_any_ofchange the schema test fails with the error above, and without the key change the output switches to anEnvDirectKeykey type.Outside the repo I also checked the points below.
88and"dynamic"for the ruff line width, and both the string and object forms of the uv build constraint, while0is rejected.