Skip to content

Linter: create rule to remove empty fragment #1825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
20 changes: 18 additions & 2 deletions src/core/jsonschema/official_resolver.in.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ auto sourcemeta::core::schema_official_resolver(std::string_view identifier)
"https://json-schema.org/draft/2020-12/hyper-schema") {
return sourcemeta::core::parse_json(
R"EOF(@METASCHEMA_HYPERSCHEMA_2020_12@)EOF");
} else if (identifier ==
"https://json-schema.org/draft/2020-12/hyper-schema#") {
auto schema = sourcemeta::core::parse_json(
R"EOF(@METASCHEMA_HYPERSCHEMA_2020_12@)EOF");
schema.at("$id").into(sourcemeta::core::JSON{identifier});
return schema;
} else if (identifier ==
"https://json-schema.org/draft/2020-12/meta/applicator") {
return sourcemeta::core::parse_json(
Expand Down Expand Up @@ -54,8 +60,10 @@ auto sourcemeta::core::schema_official_resolver(std::string_view identifier)

// Just for compatibility given that this is such a common issue
} else if (identifier == "https://json-schema.org/draft/2020-12/schema#") {
return sourcemeta::core::parse_json(
auto schema = sourcemeta::core::parse_json(
R"EOF(@METASCHEMA_JSONSCHEMA_2020_12@)EOF");
schema.at("$id").into(sourcemeta::core::JSON{identifier});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should never mutate the official meta-schemas, as they are supposed to be immutable. What was the intention?

return schema;

// JSON Schema 2019-09
} else if (identifier == "https://json-schema.org/draft/2019-09/schema") {
Expand All @@ -65,6 +73,12 @@ auto sourcemeta::core::schema_official_resolver(std::string_view identifier)
"https://json-schema.org/draft/2019-09/hyper-schema") {
return sourcemeta::core::parse_json(
R"EOF(@METASCHEMA_HYPERSCHEMA_2019_09@)EOF");
} else if (identifier ==
"https://json-schema.org/draft/2019-09/hyper-schema#") {
auto schema = sourcemeta::core::parse_json(
R"EOF(@METASCHEMA_HYPERSCHEMA_2019_09@)EOF");
schema.at("$id").into(sourcemeta::core::JSON{identifier});
return schema;
} else if (identifier ==
"https://json-schema.org/draft/2019-09/meta/applicator") {
return sourcemeta::core::parse_json(
Expand Down Expand Up @@ -105,8 +119,10 @@ auto sourcemeta::core::schema_official_resolver(std::string_view identifier)

// Just for compatibility given that this is such a common issue
} else if (identifier == "https://json-schema.org/draft/2019-09/schema#") {
return sourcemeta::core::parse_json(
auto schema = sourcemeta::core::parse_json(
R"EOF(@METASCHEMA_JSONSCHEMA_2019_09@)EOF");
schema.at("$id").into(sourcemeta::core::JSON{identifier});
return schema;

// JSON Schema Draft7
} else if (identifier == "http://json-schema.org/draft-07/schema#" ||
Expand Down
1 change: 1 addition & 0 deletions src/extension/alterschema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ sourcemeta_library(NAMESPACE sourcemeta PROJECT core NAME alterschema
linter/if_without_then_else.h
linter/max_contains_without_contains.h
linter/min_contains_without_contains.h
linter/modern_official_dialect_with_empty_fragment.h
linter/then_without_if.h)

if(SOURCEMETA_CORE_INSTALL)
Expand Down
2 changes: 2 additions & 0 deletions src/extension/alterschema/alterschema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ contains_any(const Vocabularies &container,
#include "linter/maximum_real_for_integer.h"
#include "linter/min_contains_without_contains.h"
#include "linter/minimum_real_for_integer.h"
#include "linter/modern_official_dialect_with_empty_fragment.h"
#include "linter/multiple_of_default.h"
#include "linter/non_applicable_type_specific_keywords.h"
#include "linter/pattern_properties_default.h"
Expand Down Expand Up @@ -109,6 +110,7 @@ auto add(SchemaTransformer &bundle, const AlterSchemaMode mode)
bundle.add<DuplicateRequiredValues>();
bundle.add<ConstWithType>();
bundle.add<AdditionalItemsWithSchemaItems>();
bundle.add<ModernOfficialDialectWithEmptyFragment>();
bundle.add<ExclusiveMaximumNumberAndMaximum>();
bundle.add<ExclusiveMinimumNumberAndMinimum>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class ModernOfficialDialectWithEmptyFragment final
: public SchemaTransformRule {
public:
ModernOfficialDialectWithEmptyFragment()
: SchemaTransformRule{
"modern_official_dialect_with_empty_fragment",
"The official dialect URI of 2019-09 and newer versions must "
"not contain the empty fragment"} {};

[[nodiscard]] auto condition(const sourcemeta::core::JSON &schema,
const sourcemeta::core::JSON &,
const sourcemeta::core::Vocabularies &,
const sourcemeta::core::SchemaFrame &,
const sourcemeta::core::SchemaFrame::Location &,
const sourcemeta::core::SchemaWalker &,
const sourcemeta::core::SchemaResolver &) const
-> sourcemeta::core::SchemaTransformRule::Result override {
if (!schema.is_object() || !schema.defines("$schema") ||
!schema.at("$schema").is_string()) {
return false;
}

const auto &schema_value = schema.at("$schema").to_string();
return (
schema_value == "https://json-schema.org/draft/2019-09/schema#" ||
schema_value == "https://json-schema.org/draft/2019-09/hyper-schema#" ||
schema_value == "https://json-schema.org/draft/2020-12/schema#" ||
schema_value == "https://json-schema.org/draft/2020-12/hyper-schema#");
}

auto transform(sourcemeta::core::JSON &schema) const -> void override {
auto schema_value = schema.at("$schema").to_string();
schema_value.pop_back();
schema.at("$schema").into(sourcemeta::core::JSON{std::move(schema_value)});
}
};
48 changes: 48 additions & 0 deletions test/alterschema/alterschema_lint_2019_09_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1984,3 +1984,51 @@ TEST(AlterSchema_lint_2019_09, additional_items_with_schema_items_array_items) {

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, modern_official_dialect_with_empty_fragment_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema#",
"type": "string"
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "string"
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, modern_official_dialect_with_empty_fragment_2) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/hyper-schema#",
"type": "string"
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/hyper-schema",
"type": "string"
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, modern_official_dialect_with_empty_fragment_3) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "string"
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "string"
})JSON");

EXPECT_EQ(document, expected);
}
49 changes: 49 additions & 0 deletions test/alterschema/alterschema_lint_2020_12_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2158,3 +2158,52 @@ TEST(AlterSchema_lint_2020_12, unnecessary_allof_wrapper_properties_4) {

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2020_12, modern_official_dialect_with_empty_fragment_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"type": "string"
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "string"
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2020_12, modern_official_dialect_with_empty_fragment_2) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/hyper-schema#",
"type": "string"
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/hyper-schema",
"type": "string"
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2020_12, modern_official_dialect_with_empty_fragment_3) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "string"
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

// Should remain unchanged since there's no empty fragment
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "string"
})JSON");

EXPECT_EQ(document, expected);
}
Loading