Skip to content

Commit 53a0161

Browse files
authored
fix: Fix bug where additionalProperties was not true in changeset (#7)
1 parent b92bce5 commit 53a0161

File tree

1 file changed

+45
-15
lines changed

1 file changed

+45
-15
lines changed

src/lib.rs

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl JsonSchemaType {
187187
}
188188
}
189189

190-
#[derive(Default, Eq, PartialEq, Debug)]
190+
#[derive(Default, Eq, PartialEq, Debug, Clone)]
191191
struct JsonSchema {
192192
ty: Option<JsonSchemaType>,
193193
constant: Option<Value>,
@@ -197,22 +197,29 @@ struct JsonSchema {
197197
items: Option<JsonSchemaItems>,
198198
}
199199

200-
impl JsonSchema {
200+
trait JsonSchemaExt {
201+
fn is_true(&self) -> bool;
202+
}
203+
204+
impl JsonSchemaExt for JsonSchema {
201205
fn is_true(&self) -> bool {
202206
*self == JsonSchema::default()
203207
}
204208
}
205209

210+
impl JsonSchemaExt for Option<Box<JsonSchema>> {
211+
fn is_true(&self) -> bool {
212+
self.as_deref().map_or(true, JsonSchema::is_true)
213+
}
214+
}
215+
206216
impl<'de> Deserialize<'de> for JsonSchema {
207217
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
208218
// perhaps catch this error as well, if needed
209219
let value = Value::deserialize(deserializer)?;
210220
if let Value::Bool(boolean) = value {
211221
if boolean {
212-
Ok(JsonSchema {
213-
ty: Some(JsonSchemaType::Any),
214-
..Default::default()
215-
})
222+
Ok(JsonSchema::default())
216223
} else {
217224
Ok(JsonSchema {
218225
ty: Some(JsonSchemaType::Never),
@@ -227,7 +234,7 @@ impl<'de> Deserialize<'de> for JsonSchema {
227234
}
228235
}
229236

230-
#[derive(Deserialize, Eq, PartialEq, Debug)]
237+
#[derive(Deserialize, Eq, PartialEq, Debug, Clone)]
231238
#[serde(untagged)]
232239
enum JsonSchemaItems {
233240
ExactItems(Vec<JsonSchema>),
@@ -362,10 +369,7 @@ fn diff_inner(
362369
rv.push(Change {
363370
path: json_path.clone(),
364371
change: ChangeKind::PropertyRemove {
365-
lhs_additional_properties: lhs
366-
.additional_properties
367-
.as_deref()
368-
.map_or(true, JsonSchema::is_true),
372+
lhs_additional_properties: lhs.additional_properties.is_true(),
369373
removed: removed.to_owned(),
370374
},
371375
});
@@ -375,10 +379,7 @@ fn diff_inner(
375379
rv.push(Change {
376380
path: json_path.clone(),
377381
change: ChangeKind::PropertyAdd {
378-
lhs_additional_properties: lhs
379-
.additional_properties
380-
.as_deref()
381-
.map_or(true, JsonSchema::is_true),
382+
lhs_additional_properties: lhs.additional_properties.is_true(),
382383
added: added.to_owned(),
383384
},
384385
});
@@ -892,6 +893,35 @@ mod tests {
892893
"###);
893894
}
894895

896+
#[test]
897+
fn remove_property_while_allowing_additional_properties() {
898+
let lhs = json! {{
899+
"type": "object",
900+
"properties": {
901+
"foobar": {"type": "string"}
902+
},
903+
"additionalProperties": true
904+
}};
905+
906+
let rhs = json! {{
907+
"type": "object",
908+
"additionalProperties": true
909+
}};
910+
911+
let diff = diff(lhs, rhs).unwrap();
912+
assert_debug_snapshot!(diff, @r###"
913+
[
914+
Change {
915+
path: "",
916+
change: PropertyRemove {
917+
lhs_additional_properties: true,
918+
removed: "foobar",
919+
},
920+
},
921+
]
922+
"###);
923+
}
924+
895925
#[test]
896926
#[ignore]
897927
fn add_property_in_array() {

0 commit comments

Comments
 (0)