Skip to content
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

fix: Recursively resolve references in anyOf #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions src/diff_walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,29 +350,23 @@ impl<F: FnMut(Change)> DiffWalker<F> {
Ok(())
}

fn resolve_ref<'a>(root_schema: &'a RootSchema, reference: &str) -> Option<&'a Schema> {
if let Some(definition_name) = reference.strip_prefix("#/definitions/") {
let schema_object = root_schema.definitions.get(definition_name)?;
Some(schema_object)
} else {
None
}
}

fn resolve_references(
&mut self,
lhs: &mut SchemaObject,
rhs: &mut SchemaObject,
root: &RootSchema,
obj: &mut SchemaObject,
) -> Result<(), Error> {
if let Some(ref reference) = lhs.reference {
if let Some(lhs_inner) = Self::resolve_ref(&self.lhs_root, reference) {
*lhs = lhs_inner.clone().into_object();
if let Some(ref reference) = obj.reference {
if let Some(definition_name) = reference.strip_prefix("#/definitions/") {
if let Some(schema_object) = root.definitions.get(definition_name) {
*obj = schema_object.clone().into_object();
}
}
}

if let Some(ref reference) = rhs.reference {
if let Some(rhs_inner) = Self::resolve_ref(&self.rhs_root, reference) {
*rhs = rhs_inner.clone().into_object();
if let Some(ref mut any_of) = obj.subschemas().any_of {
for subschema in any_of {
if let Schema::Object(ref mut subobj) = subschema {
Self::resolve_references(root, subobj)?;
}
}
}

Expand Down Expand Up @@ -459,7 +453,8 @@ impl<F: FnMut(Change)> DiffWalker<F> {
lhs: &mut SchemaObject,
rhs: &mut SchemaObject,
) -> Result<(), Error> {
self.resolve_references(lhs, rhs)?;
Self::resolve_references(&self.lhs_root, lhs)?;
Self::resolve_references(&self.rhs_root, rhs)?;
let is_lhs_split = Self::split_types(lhs);
let is_rhs_split = Self::split_types(rhs);
self.diff_any_of(json_path, is_rhs_split, lhs, rhs)?;
Expand Down
21 changes: 21 additions & 0 deletions tests/fixtures/events-diff/test1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"lhs": {},
"rhs": {
"anyOf": [
{
"$ref": "#/definitions/myobject"
},
{
"$ref": "#/definitions/myarray"
}
],
"definitions": {
"myobject": {
"type": "object"
},
"myarray": {
"type": "array"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
source: tests/test.rs
expression: diff
info:
lhs: {}
rhs:
anyOf:
- $ref: "#/definitions/myobject"
- $ref: "#/definitions/myarray"
definitions:
myarray:
type: array
myobject:
type: object
input_file: tests/fixtures/events-diff/test1.json
---
[
Change {
path: "",
change: TypeRemove {
removed: String,
},
},
Change {
path: "",
change: TypeRemove {
removed: Number,
},
},
Change {
path: "",
change: TypeRemove {
removed: Integer,
},
},
Change {
path: "",
change: TypeRemove {
removed: Boolean,
},
},
Change {
path: "",
change: TypeRemove {
removed: Null,
},
},
]
Loading