Repository navigation
Merge patternProperties when merging object schemas - #1113
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
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
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 #882.
Merging two object schemas asserted that the other side had no
patternProperties, so the$refwith siblingpatternPropertiesin #882 panicked. The merge also droppedpatternPropertiesoutright, so when no property reached that assertion the result was wrong without any error. TwoallOfbranches that both constrain^x-, one of them withadditionalProperties: false, becamepub struct ExtensionMap {}withdeny_unknown_fields, which rejects every valid value.filter_propnow 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, andadditionalPropertiesapplies only when no pattern matches. So the property is merged with each matching pattern schema, and it still falls back toadditionalPropertieswhen 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 readpatternProperties, as describing the names thatpropertiesdoes 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,
namesis an array inFoobut 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 toString.Tests
merged-pattern-propertiesschema test covers the reduced schema from Extending type with patternProperties panics #882, a requiredcountwhose name matches the other side's pattern and becomesu64from the pattern'sminimum, and two sides that share^x-, which becomes a map with checked keys and values.merge.rscover 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.