Skip to content

Merge patternProperties when merging object schemas - #1113

Open
Indian-boult wants to merge 1 commit into
oxidecomputer:mainfrom
Indian-boult:merge-pattern-properties
Open

Indian-boult wants to merge 1 commit into
oxidecomputer:mainfrom
Indian-boult:merge-pattern-properties

Conversation

@Indian-boult

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. This follows how struct and map generation already read patternProperties, as describing the names that properties does not list. The case it does not model exactly is a name that matches a pattern on each side when the two sides do not share that pattern. The merged schema is stricter than the intersection there. 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
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

1 participant