Skip to content

Commit 2ae45be

Browse files
authored
fix: anyOf is not order-sensitive (#9)
1 parent 53a0161 commit 2ae45be

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/lib.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,14 @@ fn diff_inner(
336336
dbg!(&lhs);
337337
}
338338

339-
for (i, (lhs_inner, rhs_inner)) in lhs.any_of.iter().zip(rhs.any_of.iter()).enumerate() {
339+
// hack to get a stable order for anyOf. serde_json::Value does not impl Hash or Ord, so we
340+
// can't use a set.
341+
let mut lhs_any_of = lhs.any_of.clone();
342+
let mut rhs_any_of = rhs.any_of.clone();
343+
lhs_any_of.sort_by_cached_key(|x| format!("{:?}", x));
344+
rhs_any_of.sort_by_cached_key(|x| format!("{:?}", x));
345+
346+
for (i, (lhs_inner, rhs_inner)) in lhs_any_of.iter().zip(rhs_any_of.iter()).enumerate() {
340347
let new_path = format!("{json_path}.<anyOf:{i}>");
341348
diff_inner(rv, new_path, lhs_inner, rhs_inner)?;
342349
}
@@ -958,4 +965,25 @@ mod tests {
958965
// rewrite crate on top of schemars::schema::Schema
959966
assert_debug_snapshot!(diff, @"");
960967
}
968+
969+
#[test]
970+
fn any_of_order_change() {
971+
let lhs = json! {{
972+
"anyOf": [
973+
{"type": "array"},
974+
{"type": "string"},
975+
]
976+
}};
977+
978+
let rhs = json! {{
979+
"anyOf": [
980+
{"type": "string"},
981+
{"type": "array"},
982+
]
983+
}};
984+
985+
let diff = diff(lhs, rhs).unwrap();
986+
987+
assert_debug_snapshot!(diff, @"[]");
988+
}
961989
}

0 commit comments

Comments
 (0)