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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/metaschema_uri_missing_hash.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 @@ -71,6 +71,7 @@ static auto every_item_is_boolean(const T &container) -> bool {
#include "linter/items_schema_default.h"
#include "linter/max_contains_without_contains.h"
#include "linter/maximum_real_for_integer.h"
#include "linter/metaschema_uri_missing_hash.h"
#include "linter/min_contains_without_contains.h"
#include "linter/minimum_real_for_integer.h"
#include "linter/non_applicable_type_specific_keywords.h"
Expand All @@ -93,6 +94,7 @@ auto add(SchemaTransformer &bundle, const AlterSchemaMode mode)
// Common rules that apply to all modes
bundle.add<ContentMediaTypeWithoutEncoding>();
bundle.add<ContentSchemaWithoutMediaType>();
bundle.add<MetaschemaUriMissingHash>();
bundle.add<NonApplicableTypeSpecificKeywords>();
bundle.add<UnnecessaryAllOfRefWrapper>();
bundle.add<DuplicateAllOfBranches>();
Expand Down
45 changes: 45 additions & 0 deletions src/extension/alterschema/linter/metaschema_uri_missing_hash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class MetaschemaUriMissingHash final : public SchemaTransformRule {
public:
MetaschemaUriMissingHash()
: SchemaTransformRule{"metaschema_uri_missing_hash",
Copy link
Member

@jviotti jviotti Jul 8, 2025

Choose a reason for hiding this comment

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

Can we call the id draft_official_dialect_without_empty_fragment? In particular, "hash" is not the correct term. In URI parlance, it is an empty fragment

Copy link
Member

Choose a reason for hiding this comment

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

Then for the other rule where we remove the empty fragment in 2019-09 and 2020-12 we can call it modern_official_dialect_with_empty_fragment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, will update those in the rule repo as well

"The canonical metaschema URIs end with `schema#`. "
Copy link
Member

@jviotti jviotti Jul 8, 2025

Choose a reason for hiding this comment

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

Maybe simplify it like this:

The official dialect URI of Draft 7 and older versions must contain the empty fragment

"Omitting the fragment identifier can confuse "
"tooling and may bypass caching"} {};

[[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();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const auto schema_value = schema.at("$schema").to_string();
const auto &schema_value{schema.at("$schema").to_string()};

Otherwise you are unnecessarily copying the string

return !schema_value.empty() && schema_value.back() != '#' &&
Copy link
Member

Choose a reason for hiding this comment

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

I don't think you need these first two checks. Just checking schema_value against the possibilities afterwards is enough?

(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{schema_value});
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
schema.at("$schema").into(sourcemeta::core::JSON{schema_value});
schema.at("$schema").into(sourcemeta::core::JSON{std::move(schema_value)});

To avoid yet another copy

}
};
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, metaschema_uri_missing_hash_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, metaschema_uri_missing_hash_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, metaschema_uri_missing_hash_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, metaschema_uri_missing_hash_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 @@ -761,3 +761,19 @@ TEST(AlterSchema_lint_draft4, unnecessary_allof_ref_wrapper_1) {

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_draft4, metaschema_uri_missing_hash_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 @@ -1112,3 +1112,19 @@ TEST(AlterSchema_lint_draft6, unnecessary_allof_ref_wrapper_1) {

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_draft6, metaschema_uri_missing_hash_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);
}
48 changes: 48 additions & 0 deletions test/alterschema/alterschema_lint_draft7_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1208,3 +1208,51 @@ TEST(AlterSchema_lint_draft7, unnecessary_allof_ref_wrapper_1) {

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_draft7, metaschema_uri_missing_hash_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, metaschema_uri_missing_hash_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, metaschema_uri_missing_hash_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);
}