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
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ struct SchemaWalkerResult {
/// The keywords a given keyword depends on (if any) during the evaluation
/// process
std::unordered_set<std::string_view> dependencies;
/// The keywords a given keyword depends on for evaluation ordering purposes
/// only (not semantic dependencies)
std::unordered_set<std::string_view> order_dependencies;
/// The JSON instance types that this keyword applies to (empty means all)
JSON::TypeSet instances;

Expand All @@ -175,9 +178,12 @@ struct SchemaWalkerResult {
SchemaWalkerResult(SchemaKeywordType type_,
std::optional<Vocabularies::URI> vocabulary_,
std::unordered_set<std::string_view> dependencies_,
std::unordered_set<std::string_view> order_dependencies_,
JSON::TypeSet instances_)
: type{type_}, vocabulary{std::move(vocabulary_)},
dependencies{std::move(dependencies_)}, instances{instances_} {}
dependencies{std::move(dependencies_)},
order_dependencies{std::move(order_dependencies_)},
instances{instances_} {}
};

/// @ingroup jsonschema
Expand Down
13 changes: 11 additions & 2 deletions src/core/jsonschema/jsonschema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -474,14 +474,23 @@ auto sourcemeta::core::schema_keyword_priority(
const sourcemeta::core::Vocabularies &vocabularies,
const sourcemeta::core::SchemaWalker &walker) -> std::uint64_t {
const auto &result{walker(keyword, vocabularies)};
return std::accumulate(
const auto priority_from_dependencies{std::accumulate(
result.dependencies.cbegin(), result.dependencies.cend(),
static_cast<std::uint64_t>(0),
[&vocabularies, &walker](const auto accumulator, const auto &dependency) {
return std::max(
accumulator,
schema_keyword_priority(dependency, vocabularies, walker) + 1);
});
})};
const auto priority_from_order_dependencies{std::accumulate(
result.order_dependencies.cbegin(), result.order_dependencies.cend(),
static_cast<std::uint64_t>(0),
[&vocabularies, &walker](const auto accumulator, const auto &dependency) {
return std::max(
accumulator,
schema_keyword_priority(dependency, vocabularies, walker) + 1);
})};
return std::max(priority_from_dependencies, priority_from_order_dependencies);
}

auto sourcemeta::core::wrap(const sourcemeta::core::JSON::String &identifier)
Expand Down
138 changes: 94 additions & 44 deletions src/core/jsonschema/known_walker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ using KeywordHandler =
const SchemaWalkerResult &(*)(const Vocabularies &vocabularies);

static const SchemaWalkerResult UNKNOWN_RESULT{
SchemaKeywordType::Unknown, std::nullopt, {}, {}};
SchemaKeywordType::Unknown, std::nullopt, {}, {}, {}};

static const SchemaWalkerResult UNKNOWN_WITH_REF_RESULT{
SchemaKeywordType::Unknown, std::nullopt, {"$ref"}, {}};
SchemaKeywordType::Unknown, std::nullopt, {"$ref"}, {}, {}};

auto has_draft3_to_7(const Vocabularies &vocabularies) -> bool {
return vocabularies.contains(Known::JSON_Schema_Draft_7) ||
Expand All @@ -30,14 +30,21 @@ auto has_draft3_to_7(const Vocabularies &vocabularies) -> bool {
#define RETURN_WITH_DEPENDENCIES(_vocabulary, _types, _strategy, ...) \
{ \
static const SchemaWalkerResult result{ \
SchemaKeywordType::_strategy, _vocabulary, {__VA_ARGS__}, _types}; \
SchemaKeywordType::_strategy, _vocabulary, {__VA_ARGS__}, {}, _types}; \
return result; \
}

#define RETURN_WITH_ORDER_DEPENDENCIES(_vocabulary, _types, _strategy, ...) \
{ \
static const SchemaWalkerResult result{ \
SchemaKeywordType::_strategy, _vocabulary, {}, {__VA_ARGS__}, _types}; \
return result; \
}

#define RETURN(_vocabulary, _types, _strategy) \
{ \
static const SchemaWalkerResult result{ \
SchemaKeywordType::_strategy, _vocabulary, {}, _types}; \
SchemaKeywordType::_strategy, _vocabulary, {}, {}, _types}; \
return result; \
}

Expand All @@ -47,6 +54,13 @@ auto has_draft3_to_7(const Vocabularies &vocabularies) -> bool {
RETURN_WITH_DEPENDENCIES(_vocabulary, _types, _strategy, __VA_ARGS__) \
}

#define CHECK_VOCABULARY_WITH_ORDER_DEPENDENCIES(_vocabulary, _types, \
_strategy, ...) \
if (vocabularies.contains(_vocabulary)) { \
RETURN_WITH_ORDER_DEPENDENCIES(_vocabulary, _types, _strategy, \
__VA_ARGS__) \
}

#define CHECK_VOCABULARY(_vocabulary, _types, _strategy) \
if (vocabularies.contains(_vocabulary)) { \
RETURN(_vocabulary, _types, _strategy) \
Expand Down Expand Up @@ -340,7 +354,7 @@ auto handle_properties(const Vocabularies &vocabularies)
-> const SchemaWalkerResult & {
if (vocabularies.contains(Known::JSON_Schema_2020_12_Applicator)) {
if (vocabularies.contains(Known::JSON_Schema_2020_12_Validation)) {
RETURN_WITH_DEPENDENCIES(
RETURN_WITH_ORDER_DEPENDENCIES(
Known::JSON_Schema_2020_12_Applicator, make_set({JSON::Type::Object}),
ApplicatorMembersTraversePropertyStatic, "required")
}
Expand All @@ -350,32 +364,68 @@ auto handle_properties(const Vocabularies &vocabularies)
}
if (vocabularies.contains(Known::JSON_Schema_2019_09_Applicator)) {
if (vocabularies.contains(Known::JSON_Schema_2019_09_Validation)) {
RETURN_WITH_DEPENDENCIES(
RETURN_WITH_ORDER_DEPENDENCIES(
Known::JSON_Schema_2019_09_Applicator, make_set({JSON::Type::Object}),
ApplicatorMembersTraversePropertyStatic, "required")
}
RETURN(Known::JSON_Schema_2019_09_Applicator,
make_set({JSON::Type::Object}),
ApplicatorMembersTraversePropertyStatic)
}
CHECK_VOCABULARY_WITH_DEPENDENCIES(
Known::JSON_Schema_Draft_7, make_set({JSON::Type::Object}),
ApplicatorMembersTraversePropertyStatic, "$ref", "required")
CHECK_VOCABULARY_WITH_DEPENDENCIES(
Known::JSON_Schema_Draft_7_Hyper, make_set({JSON::Type::Object}),
ApplicatorMembersTraversePropertyStatic, "$ref", "required")
CHECK_VOCABULARY_WITH_DEPENDENCIES(
Known::JSON_Schema_Draft_6, make_set({JSON::Type::Object}),
ApplicatorMembersTraversePropertyStatic, "$ref", "required")
CHECK_VOCABULARY_WITH_DEPENDENCIES(
Known::JSON_Schema_Draft_6_Hyper, make_set({JSON::Type::Object}),
ApplicatorMembersTraversePropertyStatic, "$ref", "required")
CHECK_VOCABULARY_WITH_DEPENDENCIES(
Known::JSON_Schema_Draft_4, make_set({JSON::Type::Object}),
ApplicatorMembersTraversePropertyStatic, "$ref", "required")
CHECK_VOCABULARY_WITH_DEPENDENCIES(
Known::JSON_Schema_Draft_4_Hyper, make_set({JSON::Type::Object}),
ApplicatorMembersTraversePropertyStatic, "$ref", "required")
if (vocabularies.contains(Known::JSON_Schema_Draft_7)) {
static const SchemaWalkerResult result{
SchemaKeywordType::ApplicatorMembersTraversePropertyStatic,
Known::JSON_Schema_Draft_7,
{"$ref"},
{"required"},
make_set({JSON::Type::Object})};
return result;
}
if (vocabularies.contains(Known::JSON_Schema_Draft_7_Hyper)) {
static const SchemaWalkerResult result{
SchemaKeywordType::ApplicatorMembersTraversePropertyStatic,
Known::JSON_Schema_Draft_7_Hyper,
{"$ref"},
{"required"},
make_set({JSON::Type::Object})};
return result;
}
if (vocabularies.contains(Known::JSON_Schema_Draft_6)) {
static const SchemaWalkerResult result{
SchemaKeywordType::ApplicatorMembersTraversePropertyStatic,
Known::JSON_Schema_Draft_6,
{"$ref"},
{"required"},
make_set({JSON::Type::Object})};
return result;
}
if (vocabularies.contains(Known::JSON_Schema_Draft_6_Hyper)) {
static const SchemaWalkerResult result{
SchemaKeywordType::ApplicatorMembersTraversePropertyStatic,
Known::JSON_Schema_Draft_6_Hyper,
{"$ref"},
{"required"},
make_set({JSON::Type::Object})};
return result;
}
if (vocabularies.contains(Known::JSON_Schema_Draft_4)) {
static const SchemaWalkerResult result{
SchemaKeywordType::ApplicatorMembersTraversePropertyStatic,
Known::JSON_Schema_Draft_4,
{"$ref"},
{"required"},
make_set({JSON::Type::Object})};
return result;
}
if (vocabularies.contains(Known::JSON_Schema_Draft_4_Hyper)) {
static const SchemaWalkerResult result{
SchemaKeywordType::ApplicatorMembersTraversePropertyStatic,
Known::JSON_Schema_Draft_4_Hyper,
{"$ref"},
{"required"},
make_set({JSON::Type::Object})};
return result;
}
CHECK_VOCABULARY_WITH_DEPENDENCIES(
Known::JSON_Schema_Draft_3, make_set({JSON::Type::Object}),
ApplicatorMembersTraversePropertyStatic, "$ref")
Expand Down Expand Up @@ -718,30 +768,30 @@ auto handle_type(const Vocabularies &vocabularies)
-> const SchemaWalkerResult & {
if (vocabularies.contains(Known::JSON_Schema_2020_12_Validation)) {
if (vocabularies.contains(Known::JSON_Schema_2020_12_Applicator)) {
RETURN_WITH_DEPENDENCIES(Known::JSON_Schema_2020_12_Validation, {},
Assertion, "properties")
RETURN_WITH_ORDER_DEPENDENCIES(Known::JSON_Schema_2020_12_Validation, {},
Assertion, "properties")
}
RETURN(Known::JSON_Schema_2020_12_Validation, {}, Assertion)
}
if (vocabularies.contains(Known::JSON_Schema_2019_09_Validation)) {
if (vocabularies.contains(Known::JSON_Schema_2019_09_Applicator)) {
RETURN_WITH_DEPENDENCIES(Known::JSON_Schema_2019_09_Validation, {},
Assertion, "properties")
RETURN_WITH_ORDER_DEPENDENCIES(Known::JSON_Schema_2019_09_Validation, {},
Assertion, "properties")
}
RETURN(Known::JSON_Schema_2019_09_Validation, {}, Assertion)
}
CHECK_VOCABULARY_WITH_DEPENDENCIES(Known::JSON_Schema_Draft_7, {}, Assertion,
("properties"))
CHECK_VOCABULARY_WITH_DEPENDENCIES(Known::JSON_Schema_Draft_7_Hyper, {},
Assertion, "properties")
CHECK_VOCABULARY_WITH_DEPENDENCIES(Known::JSON_Schema_Draft_6, {}, Assertion,
("properties"))
CHECK_VOCABULARY_WITH_DEPENDENCIES(Known::JSON_Schema_Draft_6_Hyper, {},
Assertion, "properties")
CHECK_VOCABULARY_WITH_DEPENDENCIES(Known::JSON_Schema_Draft_4, {}, Assertion,
("properties"))
CHECK_VOCABULARY_WITH_DEPENDENCIES(Known::JSON_Schema_Draft_4_Hyper, {},
Assertion, "properties")
CHECK_VOCABULARY_WITH_ORDER_DEPENDENCIES(Known::JSON_Schema_Draft_7, {},
Assertion, "properties")
CHECK_VOCABULARY_WITH_ORDER_DEPENDENCIES(Known::JSON_Schema_Draft_7_Hyper, {},
Assertion, "properties")
CHECK_VOCABULARY_WITH_ORDER_DEPENDENCIES(Known::JSON_Schema_Draft_6, {},
Assertion, "properties")
CHECK_VOCABULARY_WITH_ORDER_DEPENDENCIES(Known::JSON_Schema_Draft_6_Hyper, {},
Assertion, "properties")
CHECK_VOCABULARY_WITH_ORDER_DEPENDENCIES(Known::JSON_Schema_Draft_4, {},
Assertion, "properties")
CHECK_VOCABULARY_WITH_ORDER_DEPENDENCIES(Known::JSON_Schema_Draft_4_Hyper, {},
Assertion, "properties")
CHECK_VOCABULARY_WITH_DEPENDENCIES(Known::JSON_Schema_Draft_3, {},
ApplicatorElementsInPlaceSome, "$ref")
CHECK_VOCABULARY_WITH_DEPENDENCIES(Known::JSON_Schema_Draft_3_Hyper, {},
Expand Down Expand Up @@ -834,10 +884,10 @@ auto handle_multipleOf(const Vocabularies &vocabularies)

auto handle_maximum(const Vocabularies &vocabularies)
-> const SchemaWalkerResult & {
CHECK_VOCABULARY_WITH_DEPENDENCIES(
CHECK_VOCABULARY_WITH_ORDER_DEPENDENCIES(
Known::JSON_Schema_2020_12_Validation,
make_set({JSON::Type::Integer, JSON::Type::Real}), Assertion, "type")
CHECK_VOCABULARY_WITH_DEPENDENCIES(
CHECK_VOCABULARY_WITH_ORDER_DEPENDENCIES(
Known::JSON_Schema_2019_09_Validation,
make_set({JSON::Type::Integer, JSON::Type::Real}), Assertion, "type")
CHECK_VOCABULARY_WITH_DEPENDENCIES(
Expand Down Expand Up @@ -881,10 +931,10 @@ auto handle_maximum(const Vocabularies &vocabularies)

auto handle_minimum(const Vocabularies &vocabularies)
-> const SchemaWalkerResult & {
CHECK_VOCABULARY_WITH_DEPENDENCIES(
CHECK_VOCABULARY_WITH_ORDER_DEPENDENCIES(
Known::JSON_Schema_2020_12_Validation,
make_set({JSON::Type::Integer, JSON::Type::Real}), Assertion, "type")
CHECK_VOCABULARY_WITH_DEPENDENCIES(
CHECK_VOCABULARY_WITH_ORDER_DEPENDENCIES(
Known::JSON_Schema_2019_09_Validation,
make_set({JSON::Type::Integer, JSON::Type::Real}), Assertion, "type")
CHECK_VOCABULARY_WITH_DEPENDENCIES(
Expand Down
9 changes: 2 additions & 7 deletions src/extension/alterschema/common/unnecessary_allof_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ class UnnecessaryAllOfWrapper final : public SchemaTransformRule {
continue;
}

if (metadata.type != SchemaKeywordType::Assertion &&
dependency_blocked.contains(keyword)) {
if (dependency_blocked.contains(keyword)) {
continue;
}

Expand All @@ -77,11 +76,7 @@ class UnnecessaryAllOfWrapper final : public SchemaTransformRule {
continue;
}

// TODO: Fix the fact that the walker declares dependencies for
// evaluation order convenience, not semantic dependencies (i.e. many
// assertion keywords depend on `type`)
if (metadata.type != SchemaKeywordType::Assertion &&
std::ranges::any_of(
if (std::ranges::any_of(
metadata.dependencies, [&](const auto &dependency) {
return !entry.defines(std::string{dependency}) &&
(schema.defines(std::string{dependency}) ||
Expand Down
2 changes: 1 addition & 1 deletion test/alterschema/alterschema_lint_2020_12_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2593,8 +2593,8 @@ TEST(
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"$ref": "https://example.com/foo",
"properties": { "bar": { "type": "string" } },
"allOf": [
{ "properties": { "bar": { "type": "string" } } },
{ "$ref": "https://example.com/baz", "type": "object" }
]
})JSON");
Expand Down
Loading