Skip to content

Commit 42eaf87

Browse files
committed
More bug fixes
Signed-off-by: Juan Cruz Viotti <[email protected]>
1 parent 157a928 commit 42eaf87

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

src/alterschema/common/double_negation_elimination.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ class DoubleNegationElimination final : public SchemaTransformRule {
4444
auto transform(JSON &schema, const Result &) const -> void override {
4545
auto inner{schema.at("not").at("not")};
4646
schema.erase("not");
47+
48+
while (inner.is_object() && inner.size() == 1 && inner.defines("not") &&
49+
inner.at("not").is_object() && inner.at("not").size() == 1 &&
50+
inner.at("not").defines("not") &&
51+
!(inner.at("not").at("not").is_boolean() &&
52+
!inner.at("not").at("not").to_boolean())) {
53+
auto next{inner.at("not").at("not")};
54+
inner = std::move(next);
55+
}
56+
4757
if (inner.is_object()) {
4858
schema.merge(inner.as_object());
4959
}
@@ -53,7 +63,10 @@ class DoubleNegationElimination final : public SchemaTransformRule {
5363
const Pointer &target,
5464
const Pointer &current) const
5565
-> Pointer override {
56-
const Pointer old_prefix{current.concat({"not", "not"})};
66+
auto old_prefix{current.concat({"not", "not"})};
67+
while (target.starts_with(old_prefix.concat({"not", "not"}))) {
68+
old_prefix = old_prefix.concat({"not", "not"});
69+
}
5770
if (!target.starts_with(old_prefix)) {
5871
return target;
5972
}

test/alterschema/alterschema_lint_draft7_test.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4420,3 +4420,39 @@ TEST(AlterSchema_lint_draft7, triple_negation_to_single) {
44204420

44214421
EXPECT_EQ(document, expected);
44224422
}
4423+
4424+
TEST(AlterSchema_lint_draft7, quadruple_negation_to_identity) {
4425+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
4426+
"$schema": "http://json-schema.org/draft-07/schema#",
4427+
"not": { "not": { "not": { "not": { "type": "string" } } } }
4428+
})JSON");
4429+
4430+
LINT_AND_FIX(document, result, traces);
4431+
4432+
EXPECT_FALSE(result.first);
4433+
4434+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
4435+
"$schema": "http://json-schema.org/draft-07/schema#",
4436+
"type": "string"
4437+
})JSON");
4438+
4439+
EXPECT_EQ(document, expected);
4440+
}
4441+
4442+
TEST(AlterSchema_lint_draft7, quintuple_negation_to_single) {
4443+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
4444+
"$schema": "http://json-schema.org/draft-07/schema#",
4445+
"not": { "not": { "not": { "not": { "not": { "type": "string" } } } } }
4446+
})JSON");
4447+
4448+
LINT_AND_FIX(document, result, traces);
4449+
4450+
EXPECT_FALSE(result.first);
4451+
4452+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
4453+
"$schema": "http://json-schema.org/draft-07/schema#",
4454+
"not": { "type": "string" }
4455+
})JSON");
4456+
4457+
EXPECT_EQ(document, expected);
4458+
}

0 commit comments

Comments
 (0)