Skip to content

[Linter]: create rule that adds missing hash to metaschema uri's of drafts before 2019-09 #1823

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/extension/alterschema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ sourcemeta_library(NAMESPACE sourcemeta PROJECT core NAME alterschema
linter/dependent_required_tautology.h
linter/equal_numeric_bounds_to_enum.h
linter/maximum_real_for_integer.h
linter/draft_official_dialect_without_empty_fragment.h
linter/minimum_real_for_integer.h
linter/single_type_array.h
linter/enum_to_const.h
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 @@ -44,6 +44,7 @@ contains_any(const Vocabularies &container,
#include "linter/dependencies_property_tautology.h"
#include "linter/dependent_required_default.h"
#include "linter/dependent_required_tautology.h"
#include "linter/draft_official_dialect_without_empty_fragment.h"
#include "linter/duplicate_allof_branches.h"
#include "linter/duplicate_anyof_branches.h"
#include "linter/duplicate_enum_values.h"
Expand Down Expand Up @@ -84,6 +85,7 @@ auto add(SchemaTransformer &bundle, const AlterSchemaMode mode)
// Common rules that apply to all modes
bundle.add<ContentMediaTypeWithoutEncoding>();
bundle.add<ContentSchemaWithoutMediaType>();
bundle.add<DraftOfficialDialectWithoutEmptyFragment>();
bundle.add<NonApplicableTypeSpecificKeywords>();
bundle.add<UnnecessaryAllOfWrapperModern>();
bundle.add<UnnecessaryAllOfWrapperDraft>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class DraftOfficialDialectWithoutEmptyFragment final
: public SchemaTransformRule {
public:
DraftOfficialDialectWithoutEmptyFragment()
: SchemaTransformRule{"draft_official_dialect_without_empty_fragment",
"The official dialect URI of Draft 7 and older "
"versions must 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 == "http://json-schema.org/draft-07/schema" ||
schema_value == "http://json-schema.org/draft-07/hyper-schema" ||
schema_value == "http://json-schema.org/draft-06/schema" ||
schema_value == "http://json-schema.org/draft-06/hyper-schema" ||
schema_value == "http://json-schema.org/draft-04/schema" ||
schema_value == "http://json-schema.org/draft-04/hyper-schema" ||
schema_value == "http://json-schema.org/draft-03/schema" ||
schema_value == "http://json-schema.org/draft-03/hyper-schema" ||
schema_value == "http://json-schema.org/draft-02/schema" ||
schema_value == "http://json-schema.org/draft-02/hyper-schema" ||
schema_value == "http://json-schema.org/draft-01/schema" ||
schema_value == "http://json-schema.org/draft-01/hyper-schema" ||
schema_value == "http://json-schema.org/draft-00/schema" ||
schema_value == "http://json-schema.org/draft-00/hyper-schema");
}

auto transform(sourcemeta::core::JSON &schema) const -> void override {
auto schema_value = schema.at("$schema").to_string();
schema_value += "#";
schema.at("$schema").into(sourcemeta::core::JSON{std::move(schema_value)});
}
};
16 changes: 16 additions & 0 deletions test/alterschema/alterschema_lint_draft0_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,19 @@ TEST(AlterSchema_lint_draft0, boolean_true_1) {

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_draft0, draft_official_dialect_without_empty_fragment_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-00/schema",
"type": "string"
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

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

EXPECT_EQ(document, expected);
}
16 changes: 16 additions & 0 deletions test/alterschema/alterschema_lint_draft1_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,19 @@ TEST(AlterSchema_lint_draft1, equal_numeric_bounds_to_enum_2) {

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_draft1, draft_official_dialect_without_empty_fragment_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-01/schema",
"type": "string"
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

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

EXPECT_EQ(document, expected);
}
16 changes: 16 additions & 0 deletions test/alterschema/alterschema_lint_draft2_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,19 @@ TEST(AlterSchema_lint_draft2, equal_numeric_bounds_to_enum_2) {

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_draft2, draft_official_dialect_without_empty_fragment_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-02/schema",
"type": "string"
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

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

EXPECT_EQ(document, expected);
}
16 changes: 16 additions & 0 deletions test/alterschema/alterschema_lint_draft3_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -579,3 +579,19 @@ TEST(AlterSchema_lint_draft3, equal_numeric_bounds_to_enum_2) {

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_draft3, draft_official_dialect_without_empty_fragment_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-03/schema",
"type": "string"
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

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

EXPECT_EQ(document, expected);
}
16 changes: 16 additions & 0 deletions test/alterschema/alterschema_lint_draft4_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -921,3 +921,19 @@ TEST(AlterSchema_lint_draft4, unnecessary_allof_wrapper_properties_1) {

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_draft4, draft_official_dialect_without_empty_fragment_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema",
"type": "string"
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

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

EXPECT_EQ(document, expected);
}
16 changes: 16 additions & 0 deletions test/alterschema/alterschema_lint_draft6_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1183,3 +1183,19 @@ TEST(AlterSchema_lint_draft6, unnecessary_allof_wrapper_properties_1) {

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_draft6, draft_official_dialect_without_empty_fragment_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-06/schema",
"type": "string"
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

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

EXPECT_EQ(document, expected);
}
50 changes: 50 additions & 0 deletions test/alterschema/alterschema_lint_draft7_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1283,3 +1283,53 @@ TEST(AlterSchema_lint_draft7, unnecessary_allof_wrapper_properties_1) {

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_draft7, draft_official_dialect_without_empty_fragment_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-07/schema",
"type": "string"
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

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

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_draft7,
draft_official_dialect_without_empty_fragment_hyper) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-07/hyper-schema",
"type": "string"
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

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

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_draft7,
draft_official_dialect_without_empty_fragment_already_has_hash) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "string"
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

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

EXPECT_EQ(document, expected);
}
Loading