Repository navigation
Merge patternProperties when merging object schemas - #1113
Indian-boult wants to merge 2 commits 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
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.
|
This is quite an old bug; did you encounter it yourself or were you trying to help out the project? |
|
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 I reran it this morning against current main. The full The change stays inside |
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 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'sadditionalProperties, so the merged pattern is stricter than the intersection for it. When thatadditionalPropertiesisfalse, the pattern becomesfalseand 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-idwithmaxLength: 5on the other side, anx-idvalue 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,
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.