Skip to content

Merge patternProperties when merging object schemas - #1113

Open
Indian-boult wants to merge 2 commits into
oxidecomputer:mainfrom
Indian-boult:merge-pattern-properties
Open

Indian-boult wants to merge 2 commits into
oxidecomputer:mainfrom
Indian-boult:merge-pattern-properties

Conversation

@Indian-boult

@Indian-boult Indian-boult commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #882.

Merging two object schemas asserted that the other side had no patternProperties, so the $ref with sibling patternProperties in #882 panicked. The merge also dropped patternProperties outright, so when no property reached that assertion the result was wrong without any error. Two allOf branches that both constrain ^x-, one of them with additionalProperties: false, became pub struct ExtensionMap {} with deny_unknown_fields, which rejects every valid value.

filter_prop now checks a property that only one side names against the other side's patterns. JSON Schema 2020-12 sections 10.3.2.2 and 10.3.2.3 say every pattern that matches the name applies, and additionalProperties applies only when no pattern matches. So the property is merged with each matching pattern schema, and it still falls back to additionalProperties when none match. A pattern that does not compile matches nothing there, and conversion still reports it where the pattern is used.

The merged object keeps the patterns of both sides. A pattern on both sides takes both schemas. A pattern on one side also takes the other side's additionalProperties, because that is what validates such a name on the other side when none of the other side's patterns match. That is exact unless a name matches a pattern on each side when the two sides do not share that pattern. Such a name should not get the other side's additionalProperties, so the merged pattern is stricter than the intersection for it. When that additionalProperties is false, the pattern becomes false and is dropped, and the name is checked only against the other side's pattern, which is looser. With ^x- on a closed object and ^x-id with maxLength: 5 on the other side, an x-id value of any length passes. Keeping both patterns would not help today, because an object with two different pattern schemas already becomes an empty struct. Getting it exact would take regex intersection, and I did not want to guess at that without knowing how it fits the multi-pass design you described in the issue.

For the schema in #882, names is an array in Foo but its name matches ^([^.]+)$, which requires a string, so it can never be present. The generated type is a map from keys that match the pattern to String.

Tests

  • The new merged-pattern-properties schema test covers the reduced schema from Extending type with patternProperties panics #882, a required count whose name matches the other side's pattern and becomes u64 from the pattern's minimum, and two sides that share ^x-, which becomes a map with checked keys and values.
  • Four unit tests in merge.rs cover a matching property, a required property that matches no pattern, a pattern against a closed object, and a pattern that does not compile.

All of them panic on main at the assertion in filter_prop, except the shared ^x- case, which produces the empty struct. No other expected output changed. fmt, clippy with -D warnings, doc and the full workspace tests pass on 1.97.1.

Merging two object schemas asserted that the other side had no
patternProperties, so a `$ref` or `allOf` that combined an object with
patternProperties panicked. The merge also dropped patternProperties, so
when no property reached that assertion, two objects that share a `^x-`
pattern produced an empty struct that rejects every valid value.

A property that only one side names is now checked against the other
side's patterns. Following JSON Schema 2020-12 sections 10.3.2.2 and
10.3.2.3, every pattern that matches its name applies, and
additionalProperties applies only when none does. The merged object keeps
the patterns of both sides. A pattern on both sides takes both schemas,
and a pattern on one side also takes the other side's
additionalProperties.

The new merged-pattern-properties schema test and four merge unit tests
panic without this change.

Fixes oxidecomputer#882
The doc comment on merge_pattern_properties said struct and map generation
read patternProperties only for names that properties does not list, but
struct generation does not read them at all. It also said the result for a
name that matches a different pattern on each side is stricter than the
intersection. When the other side's additionalProperties is false, that
pattern is dropped instead, and the result is looser.
@ahl

ahl commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

This is quite an old bug; did you encounter it yourself or were you trying to help out the project?

@Indian-boult

Copy link
Copy Markdown
Contributor Author

Trying to help out. I haven't run into it in a schema of my own. I was looking through open typify issues for panics on real published schemas, and #882 stood out because the bluesky event-model schema it links is public and you said in the issue that a prototype fix was welcome. While reading merge_so_object I saw that the merged object also drops patternProperties next to a TODO, so I checked the cases that never reach the assertion in filter_prop. That is where the shared ^x- case comes from, the one that turns into an empty struct.

I reran it this morning against current main. The full run_start.json from the issue still panics at merge.rs:1152, and with this branch it generates types that compile and parse sample documents. One gap is left. hints rejects keys other than dimensions, because a struct with named properties still ignores patternProperties. That is #522, and this change leaves it alone.

The change stays inside merge.rs. If you would rather hold it for typify2, that's fine with me.

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.

Extending type with patternProperties panics

2 participants