-
Notifications
You must be signed in to change notification settings - Fork 105
fix allOf of oneOfs by introducing cartesian product #982
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PolyProgrammist
wants to merge
3
commits into
oxidecomputer:main
Choose a base branch
from
PolyProgrammist:allOfofOneOfs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -332,7 +332,20 @@ pub(crate) fn try_merge_with_subschemas( | |
| } | ||
|
|
||
| if let Some(one_of) = one_of { | ||
| let merged_subschemas = try_merge_with_each_subschema(&schema_object, one_of, defs); | ||
| // Check if the base schema already has a oneOf - if so, we need to | ||
| // compute the Cartesian product of the two oneOfs | ||
| let base_one_of = schema_object | ||
| .subschemas | ||
| .as_ref() | ||
| .and_then(|ss| ss.one_of.as_ref()); | ||
|
|
||
| let merged_subschemas = if let Some(base_variants) = base_one_of { | ||
| // Cartesian product: for each base variant and each new variant, | ||
| // merge them together | ||
| try_merge_oneof_cartesian_product(base_variants, one_of, defs) | ||
| } else { | ||
| try_merge_with_each_subschema(&schema_object, one_of, defs) | ||
| }; | ||
|
|
||
| match merged_subschemas.len() { | ||
| 0 => return Err(()), | ||
|
|
@@ -413,6 +426,34 @@ fn try_merge_with_each_subschema( | |
| joined_schemas | ||
| } | ||
|
|
||
| /// Compute the Cartesian product of two oneOf schemas. For each combination | ||
| /// of a variant from the first oneOf and a variant from the second oneOf, | ||
| /// merge them together. This is used when we have `allOf: [oneOf[A,B], oneOf[C,D]]` | ||
| /// which should produce `oneOf[A∩C, A∩D, B∩C, B∩D]`. | ||
| fn try_merge_oneof_cartesian_product( | ||
| base_variants: &[Schema], | ||
| new_variants: &[Schema], | ||
| defs: &BTreeMap<RefKey, Schema>, | ||
| ) -> Vec<Schema> { | ||
| let mut result = Vec::new(); | ||
|
|
||
| for base_variant in base_variants { | ||
| for new_variant in new_variants { | ||
| // Try to merge each pair of variants | ||
| if let Ok(merged) = try_merge_schema(base_variant, new_variant, defs) { | ||
| // Only include if the merge produced a satisfiable schema | ||
| if merged != Schema::Bool(false) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does |
||
| && !result.iter().any(|existing| existing.roughly(&merged)) | ||
| { | ||
| result.push(merged); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| result | ||
| } | ||
|
|
||
| fn merge_schema_not( | ||
| schema: &Schema, | ||
| not_schema: &Schema, | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems possible for there to be literal or effective duplicates in this list.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see there may be oneOf: [A, B] and oneOf: [C, D]
And maybe something like oneOf[{}, {type: string}] and oneOf[{type:string}, {type:string}] where for all of them the result would be {type:string}.
Do you think comparing like that is enough?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not quite right:
oneOf[{type: string}, {type: string}]would be equivalent to the schemafalse... and so therefore would the outerallOf.but perhaps something like this?
{ "allOf": [ { "oneOf": [ { "type": "object", "required": ["x"], "properties": { "x": { "const": 1 } } }, { "type": "object", "required": ["x"], "properties": { "x": { "const": 2 } } } ] }, { "oneOf": [ { "type": "object", "required": ["y"], "properties": { "y": { "const": 3 } } }, { "type": "object", "required": ["x", "y"], "properties": { "x": { "const": 1 }, "y": { "const": 3 } } } ] } ] }I think the expansion you have in mind is more of less
{ "oneOf": [ { "allOf": [ { "type": "object", "required": ["x"], "properties": { "x": { "const": 1 } } }, { "type": "object", "required": ["y"], "properties": { "y": { "const": 3 } } } ] }, { "allOf": [ { "type": "object", "required": ["x"], "properties": { "x": { "const": 1 } } }, { "type": "object", "required": ["x", "y"], "properties": { "x": { "const": 1 }, "y": { "const": 3 } } } ] }, { "allOf": [ { "type": "object", "required": ["x"], "properties": { "x": { "const": 2 } } }, { "type": "object", "required": ["y"], "properties": { "y": { "const": 3 } } } ] }, { "allOf": [ { "type": "object", "required": ["x"], "properties": { "x": { "const": 2 } } }, { "type": "object", "required": ["x", "y"], "properties": { "x": { "const": 1 }, "y": { "const": 3 } } } ] } ] }And if we further reduce that we get
{ "oneOf": [ { "type": "object", "required": ["x", "y"], "properties": { "x": { "const": 1 }, "y": { "const": 3 } } }, { "type": "object", "required": ["x", "y"], "properties": { "x": { "const": 1 }, "y": { "const": 3 } } }, { "type": "object", "required": ["x", "y"], "properties": { "x": { "const": 2 }, "y": { "const": 3 } } }, false ] }The first 2 are identical; the last one is unsatisfiable. I think we need check for exclusivity--perhaps by seeing if the A ∪ ⌐B is empty?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually that's not quite right and now I'm deeply uncertain about whether there might be duplicates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that I've had time to sleep on it:
If we have two sets of schemas, each known to consist of mutually exclusive schemas, then the cartesian product of those two sets must also be pairwise mutually exclusive. Why? A value that was valid for multiple schemas of the cartesian product would necessarily be valid for multiple schemas in (at least) one of the input schema sets (which would invalidate the initial premise).
However! I don't believe that typify currently ensures that
oneOfs are mutually exclusive. Part of the (in-progress) typify rewrite is to ensure that that mutual exclusivity. For example, with an input like:{ "oneOf": [ { "type": "object", "required": ["y"], "properties": { "y": { "const": 3 } } }, { "type": "object", "required": ["x", "y"], "properties": { "x": { "const": 1 }, "y": { "const": 3 } } } ] }We would produce a type like this:
Which is all to say: this PR is wrong, but it's no more wrong that what we currently have and, indeed, it's quite a bit better in all but the most esoteric cases.