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

feat: Add support for changes to "required" properties #19

Merged
merged 2 commits into from
May 8, 2023
Merged
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
31 changes: 31 additions & 0 deletions src/diff_walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,36 @@ impl DiffWalker {
Ok(())
}

fn diff_required(
&mut self,
json_path: &str,
lhs: &mut SchemaObject,
rhs: &mut SchemaObject,
) -> Result<(), Error> {
let lhs_required = &lhs.object().required;
let rhs_required = &rhs.object().required;

for removed in lhs_required.difference(rhs_required) {
self.changes.push(Change {
path: json_path.to_owned(),
change: ChangeKind::RequiredRemove {
property: removed.clone(),
},
});
}

for added in rhs_required.difference(lhs_required) {
self.changes.push(Change {
path: json_path.to_owned(),
change: ChangeKind::RequiredAdd {
property: added.clone(),
},
});
}

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)?;
Expand Down Expand Up @@ -302,6 +332,7 @@ impl DiffWalker {
self.diff_range(json_path, lhs, rhs)?;
self.diff_additional_properties(json_path, lhs, rhs)?;
self.diff_array_items(json_path, lhs, rhs)?;
self.diff_required(json_path, lhs, rhs)?;
Ok(())
}
}
Expand Down
51 changes: 51 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,4 +877,55 @@ mod tests {
@"[]"
);
}

#[test]
fn drop_required() {
let lhs = json! {{
"required": ["value"]
}};
let rhs = json! {{
"required": [],
}};

let diff = diff(lhs, rhs).unwrap();

assert_debug_snapshot!(
diff,
@r###"
[
Change {
path: "",
change: RequiredRemove {
property: "value",
},
},
]
"###
);
}

#[test]
fn add_required() {
let lhs = json! {{
"required": []
}};
let rhs = json! {{
"required": ["value"],
}};

let diff = diff(lhs, rhs).unwrap();
assert_debug_snapshot!(
diff,
@r###"
[
Change {
path: "",
change: RequiredAdd {
property: "value",
},
},
]
"###
);
}
}
12 changes: 12 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ pub enum ChangeKind {
/// The new length of the tuple
new_length: usize,
},
/// A previously required property has been removed
RequiredRemove {
/// The property that is no longer required
property: String,
},
/// A previously optional property has been made required
RequiredAdd {
/// The property that is now required
property: String,
},
}

impl ChangeKind {
Expand Down Expand Up @@ -128,6 +138,8 @@ impl ChangeKind {
Self::TupleToArray { .. } => false,
Self::ArrayToTuple { .. } => true,
Self::TupleChange { .. } => true,
Self::RequiredRemove { .. } => false,
Self::RequiredAdd { .. } => true,
}
}
}
Expand Down