Skip to content
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
7 changes: 6 additions & 1 deletion src/alterschema/canonicalizer/type_array_to_any_of.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ class TypeArrayToAnyOf final : public SchemaTransformRule {
}

const auto &metadata{walker(entry.first, vocabularies)};
if (metadata.instances.any()) {
if (metadata.instances.any() &&
!(vocabularies.contains_any(
{Vocabularies::Known::JSON_Schema_2020_12_Unevaluated,
Vocabularies::Known::JSON_Schema_2019_09_Applicator}) &&
(entry.first == "unevaluatedProperties" ||
entry.first == "unevaluatedItems"))) {
this->keyword_instances_[entry.first] = metadata.instances;
}
}
Expand Down
28 changes: 25 additions & 3 deletions src/alterschema/common/unnecessary_allof_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ class UnnecessaryAllOfWrapper final : public SchemaTransformRule {

std::unordered_set<std::string_view> dependency_blocked;
for (const auto &entry : schema.as_object()) {
if ((entry.first == "unevaluatedProperties" ||
entry.first == "unevaluatedItems") &&
vocabularies.contains_any(
{Vocabularies::Known::JSON_Schema_2020_12_Unevaluated,
Vocabularies::Known::JSON_Schema_2019_09_Applicator})) {
continue;
}

for (const auto &dependency :
walker(entry.first, vocabularies).dependencies) {
dependency_blocked.emplace(dependency);
Expand Down Expand Up @@ -70,6 +78,14 @@ class UnnecessaryAllOfWrapper final : public SchemaTransformRule {
continue;
}

if (vocabularies.contains_any(
{Vocabularies::Known::JSON_Schema_2020_12_Unevaluated,
Vocabularies::Known::JSON_Schema_2019_09_Applicator}) &&
(entry.defines("unevaluatedProperties") ||
entry.defines("unevaluatedItems"))) {
continue;
}

for (const auto &keyword_entry : entry.as_object()) {
const auto &keyword{keyword_entry.first};
const auto &metadata{walker(keyword, vocabularies)};
Expand Down Expand Up @@ -101,9 +117,15 @@ class UnnecessaryAllOfWrapper final : public SchemaTransformRule {
locations.push_back(Pointer{KEYWORD, index - 1, keyword});
elevated.emplace(keyword);

for (const auto &dependency : metadata.dependencies) {
if (!entry.defines(std::string{dependency})) {
dependency_blocked.emplace(dependency);
if (!(vocabularies.contains_any(
{Vocabularies::Known::JSON_Schema_2020_12_Unevaluated,
Vocabularies::Known::JSON_Schema_2019_09_Applicator}) &&
(keyword == "unevaluatedProperties" ||
keyword == "unevaluatedItems"))) {
for (const auto &dependency : metadata.dependencies) {
if (!entry.defines(std::string{dependency})) {
dependency_blocked.emplace(dependency);
}
}
}
}
Expand Down
95 changes: 95 additions & 0 deletions test/alterschema/alterschema_canonicalize_2019_09_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,101 @@ TEST(AlterSchema_canonicalize_2019_09,
EXPECT_EQ(document, expected);
}

TEST(AlterSchema_canonicalize_2019_09,
unevaluated_properties_with_allof_properties) {
auto document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"unevaluatedProperties": { "type": "boolean" },
"allOf": [
{ "properties": { "foo": { "type": "string" } } }
]
})JSON");

CANONICALIZE(document, result, traces);

EXPECT_TRUE(result.first);

const auto expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"anyOf": [
{ "enum": [ null ] },
{ "enum": [ false, true ] },
{
"type": "object",
"minProperties": 0,
"properties": {
"foo": {
"type": "string",
"minLength": 0
}
}
},
{
"type": "array",
"minItems": 0,
"items": true
},
{
"type": "string",
"minLength": 0
},
{ "type": "number" }
],
"unevaluatedProperties": {
"enum": [ false, true ]
}
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_canonicalize_2019_09, unevaluated_properties_inside_allof) {
auto document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"allOf": [
{ "properties": { "foo": true } },
{ "unevaluatedProperties": false }
]
})JSON");

CANONICALIZE(document, result, traces);

EXPECT_TRUE(result.first);

const auto expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"allOf": [
{
"anyOf": [
{ "enum": [ null ] },
{ "enum": [ false, true ] },
{
"type": "object",
"minProperties": 0,
"properties": {}
},
{
"type": "array",
"minItems": 0,
"items": true
},
{
"type": "string",
"minLength": 0
},
{ "type": "number" }
],
"unevaluatedProperties": false
}
],
"properties": {
"foo": true
}
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_canonicalize_2019_09, items_implicit_1) {
auto document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
Expand Down
53 changes: 53 additions & 0 deletions test/alterschema/alterschema_canonicalize_2020_12_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,59 @@ TEST(AlterSchema_canonicalize_2020_12,
EXPECT_EQ(document, expected);
}

TEST(AlterSchema_canonicalize_2020_12,
unevaluated_properties_with_root_and_allof_properties) {
auto document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"foo": { "type": "object" }
},
"allOf": [
{ "properties": { "bar": true } }
],
"unevaluatedProperties": false
})JSON");

CANONICALIZE(document, result, traces);

EXPECT_TRUE(result.first);

const auto expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"anyOf": [
{ "enum": [ null ] },
{ "enum": [ false, true ] },
{
"type": "object",
"minProperties": 0,
"properties": {
"foo": {
"type": "object",
"minProperties": 0,
"properties": {}
}
}
},
{
"type": "array",
"minItems": 0,
"items": true
},
{
"type": "string",
"minLength": 0
},
{ "type": "number" }
],
"properties": {
"bar": true
},
"unevaluatedProperties": false
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_canonicalize_2020_12,
ref_into_nested_subschema_within_resource) {
auto document = sourcemeta::core::parse_json(R"JSON({
Expand Down
56 changes: 25 additions & 31 deletions test/alterschema/alterschema_lint_2020_12_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7305,9 +7305,7 @@ TEST(AlterSchema_lint_2020_12,
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"unevaluatedProperties": false,
"allOf": [
{ "properties": { "foo": { "type": "string" } } }
]
"properties": { "foo": { "type": "string" } }
})JSON");

EXPECT_EQ(document, expected);
Expand All @@ -7331,9 +7329,7 @@ TEST(
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"unevaluatedProperties": false,
"allOf": [
{ "patternProperties": { "^f": { "type": "string" } } }
]
"patternProperties": { "^f": { "type": "string" } }
})JSON");

EXPECT_EQ(document, expected);
Expand All @@ -7357,9 +7353,7 @@ TEST(
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"unevaluatedProperties": false,
"allOf": [
{ "additionalProperties": { "type": "string" } }
]
"additionalProperties": { "type": "string" }
})JSON");

EXPECT_EQ(document, expected);
Expand Down Expand Up @@ -7436,10 +7430,14 @@ TEST(

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": { "foo": { "type": "string" } },
"patternProperties": { "^b": { "type": "number" } },
"additionalProperties": { "type": "boolean" },
"unevaluatedProperties": false
"allOf": [
{
"properties": { "foo": { "type": "string" } },
"patternProperties": { "^b": { "type": "number" } },
"additionalProperties": { "type": "boolean" },
"unevaluatedProperties": false
}
]
})JSON");

EXPECT_EQ(document, expected);
Expand All @@ -7462,9 +7460,7 @@ TEST(AlterSchema_lint_2020_12,
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"unevaluatedItems": false,
"allOf": [
{ "prefixItems": [ { "type": "string" } ] }
]
"prefixItems": [ { "type": "string" } ]
})JSON");

EXPECT_EQ(document, expected);
Expand All @@ -7487,9 +7483,7 @@ TEST(AlterSchema_lint_2020_12,
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"unevaluatedItems": false,
"allOf": [
{ "items": { "type": "string" } }
]
"items": { "type": "string" }
})JSON");

EXPECT_EQ(document, expected);
Expand All @@ -7512,9 +7506,7 @@ TEST(AlterSchema_lint_2020_12,
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"unevaluatedItems": false,
"allOf": [
{ "contains": { "type": "string" } }
]
"contains": { "type": "string" }
})JSON");

EXPECT_EQ(document, expected);
Expand Down Expand Up @@ -7590,10 +7582,14 @@ TEST(AlterSchema_lint_2020_12,

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"prefixItems": [ { "type": "string" } ],
"items": { "type": "number" },
"contains": { "minimum": 0 },
"unevaluatedItems": false
"allOf": [
{
"prefixItems": [ { "type": "string" } ],
"items": { "type": "number" },
"contains": { "minimum": 0 },
"unevaluatedItems": false
}
]
})JSON");

EXPECT_EQ(document, expected);
Expand Down Expand Up @@ -7759,12 +7755,10 @@ TEST(AlterSchema_lint_2020_12,
"$schema": "https://json-schema.org/draft/2020-12/schema",
"unevaluatedProperties": false,
"unevaluatedItems": false,
"title": "Test schema",
"description": "A test",
"allOf": [
{ "properties": { "foo": { "type": "string" } } },
{ "prefixItems": [ { "type": "number" } ] }
]
"title": "Test schema",
"prefixItems": [ { "type": "number" } ],
"properties": { "foo": { "type": "string" } }
})JSON");

EXPECT_EQ(document, expected);
Expand Down
Loading