Skip to content

fix: Fix bug where additionalProperties was not true in changeset #7

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

Merged
merged 2 commits into from
Apr 3, 2023
Merged
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
60 changes: 45 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl JsonSchemaType {
}
}

#[derive(Default, Eq, PartialEq, Debug)]
#[derive(Default, Eq, PartialEq, Debug, Clone)]
struct JsonSchema {
ty: Option<JsonSchemaType>,
constant: Option<Value>,
Expand All @@ -197,22 +197,29 @@ struct JsonSchema {
items: Option<JsonSchemaItems>,
}

impl JsonSchema {
trait JsonSchemaExt {
fn is_true(&self) -> bool;
}

impl JsonSchemaExt for JsonSchema {
fn is_true(&self) -> bool {
*self == JsonSchema::default()
}
}

impl JsonSchemaExt for Option<Box<JsonSchema>> {
fn is_true(&self) -> bool {
self.as_deref().map_or(true, JsonSchema::is_true)
}
}

impl<'de> Deserialize<'de> for JsonSchema {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
// perhaps catch this error as well, if needed
let value = Value::deserialize(deserializer)?;
if let Value::Bool(boolean) = value {
if boolean {
Ok(JsonSchema {
ty: Some(JsonSchemaType::Any),
..Default::default()
})
Ok(JsonSchema::default())
} else {
Ok(JsonSchema {
ty: Some(JsonSchemaType::Never),
Expand All @@ -227,7 +234,7 @@ impl<'de> Deserialize<'de> for JsonSchema {
}
}

#[derive(Deserialize, Eq, PartialEq, Debug)]
#[derive(Deserialize, Eq, PartialEq, Debug, Clone)]
#[serde(untagged)]
enum JsonSchemaItems {
ExactItems(Vec<JsonSchema>),
Expand Down Expand Up @@ -362,10 +369,7 @@ fn diff_inner(
rv.push(Change {
path: json_path.clone(),
change: ChangeKind::PropertyRemove {
lhs_additional_properties: lhs
.additional_properties
.as_deref()
.map_or(true, JsonSchema::is_true),
lhs_additional_properties: lhs.additional_properties.is_true(),
removed: removed.to_owned(),
},
});
Expand All @@ -375,10 +379,7 @@ fn diff_inner(
rv.push(Change {
path: json_path.clone(),
change: ChangeKind::PropertyAdd {
lhs_additional_properties: lhs
.additional_properties
.as_deref()
.map_or(true, JsonSchema::is_true),
lhs_additional_properties: lhs.additional_properties.is_true(),
added: added.to_owned(),
},
});
Expand Down Expand Up @@ -892,6 +893,35 @@ mod tests {
"###);
}

#[test]
fn remove_property_while_allowing_additional_properties() {
let lhs = json! {{
"type": "object",
"properties": {
"foobar": {"type": "string"}
},
"additionalProperties": true
}};

let rhs = json! {{
"type": "object",
"additionalProperties": true
}};

let diff = diff(lhs, rhs).unwrap();
assert_debug_snapshot!(diff, @r###"
[
Change {
path: "",
change: PropertyRemove {
lhs_additional_properties: true,
removed: "foobar",
},
},
]
"###);
}

#[test]
#[ignore]
fn add_property_in_array() {
Expand Down