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
2 changes: 2 additions & 0 deletions src/extension/alterschema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ sourcemeta_library(NAMESPACE sourcemeta PROJECT core NAME alterschema
common/content_schema_without_media_type.h
common/dependencies_property_tautology.h
common/dependent_required_tautology.h
common/draft_official_dialect_with_https.h
common/draft_official_dialect_without_empty_fragment.h
common/draft_ref_siblings.h
common/drop_allof_empty_schemas.h
Expand All @@ -52,6 +53,7 @@ sourcemeta_library(NAMESPACE sourcemeta PROJECT core NAME alterschema
common/min_contains_without_contains.h
common/minimum_real_for_integer.h
common/modern_official_dialect_with_empty_fragment.h
common/modern_official_dialect_with_http.h
common/non_applicable_additional_items.h
common/non_applicable_enum_validation_keywords.h
common/non_applicable_type_specific_keywords.h
Expand Down
4 changes: 4 additions & 0 deletions src/extension/alterschema/alterschema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ inline auto APPLIES_TO_POINTERS(std::vector<Pointer> &&keywords)
#include "common/content_schema_without_media_type.h"
#include "common/dependencies_property_tautology.h"
#include "common/dependent_required_tautology.h"
#include "common/draft_official_dialect_with_https.h"
#include "common/draft_official_dialect_without_empty_fragment.h"
#include "common/draft_ref_siblings.h"
#include "common/drop_allof_empty_schemas.h"
Expand All @@ -80,6 +81,7 @@ inline auto APPLIES_TO_POINTERS(std::vector<Pointer> &&keywords)
#include "common/min_contains_without_contains.h"
#include "common/minimum_real_for_integer.h"
#include "common/modern_official_dialect_with_empty_fragment.h"
#include "common/modern_official_dialect_with_http.h"
#include "common/non_applicable_additional_items.h"
#include "common/non_applicable_enum_validation_keywords.h"
#include "common/non_applicable_type_specific_keywords.h"
Expand Down Expand Up @@ -149,6 +151,7 @@ auto add(SchemaTransformer &bundle, const AlterSchemaMode mode) -> void {

bundle.add<ContentMediaTypeWithoutEncoding>();
bundle.add<ContentSchemaWithoutMediaType>();
bundle.add<DraftOfficialDialectWithHttps>();
bundle.add<DraftOfficialDialectWithoutEmptyFragment>();
bundle.add<NonApplicableTypeSpecificKeywords>();
bundle.add<AnyOfRemoveFalseSchemas>();
Expand Down Expand Up @@ -184,6 +187,7 @@ auto add(SchemaTransformer &bundle, const AlterSchemaMode mode) -> void {
bundle.add<ConstInEnum>();
bundle.add<NonApplicableAdditionalItems>();
bundle.add<ModernOfficialDialectWithEmptyFragment>();
bundle.add<ModernOfficialDialectWithHttp>();
bundle.add<ExclusiveMaximumNumberAndMaximum>();
bundle.add<ExclusiveMinimumNumberAndMinimum>();
bundle.add<DraftRefSiblings>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
class DraftOfficialDialectWithHttps final : public SchemaTransformRule {
public:
using mutates = std::true_type;
using reframe_after_transform = std::true_type;
DraftOfficialDialectWithHttps()
: SchemaTransformRule{
"draft_official_dialect_with_https",
"The official dialect URI of Draft 7 and older must use "
"\"http://\" instead of \"https://\""} {};

[[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 &location,
const sourcemeta::core::SchemaWalker &,
const sourcemeta::core::SchemaResolver &) const
-> sourcemeta::core::SchemaTransformRule::Result override {
using sourcemeta::core::SchemaBaseDialect;
ONLY_CONTINUE_IF(
location.base_dialect == SchemaBaseDialect::JSON_Schema_Draft_7 ||
location.base_dialect == SchemaBaseDialect::JSON_Schema_Draft_7_Hyper ||
location.base_dialect == SchemaBaseDialect::JSON_Schema_Draft_6 ||
location.base_dialect == SchemaBaseDialect::JSON_Schema_Draft_6_Hyper ||
location.base_dialect == SchemaBaseDialect::JSON_Schema_Draft_4 ||
location.base_dialect == SchemaBaseDialect::JSON_Schema_Draft_4_Hyper ||
location.base_dialect == SchemaBaseDialect::JSON_Schema_Draft_3 ||
location.base_dialect == SchemaBaseDialect::JSON_Schema_Draft_3_Hyper ||
location.base_dialect == SchemaBaseDialect::JSON_Schema_Draft_2_Hyper ||
location.base_dialect == SchemaBaseDialect::JSON_Schema_Draft_1_Hyper ||
location.base_dialect == SchemaBaseDialect::JSON_Schema_Draft_0_Hyper);
ONLY_CONTINUE_IF(schema.is_object() && schema.defines("$schema") &&
schema.at("$schema").is_string());
const auto &dialect{schema.at("$schema").to_string()};
ONLY_CONTINUE_IF(dialect.starts_with("https://json-schema.org/"));
ONLY_CONTINUE_IF(
dialect == "https://json-schema.org/draft-07/schema" ||
dialect == "https://json-schema.org/draft-07/schema#" ||
dialect == "https://json-schema.org/draft-07/hyper-schema" ||
dialect == "https://json-schema.org/draft-07/hyper-schema#" ||
dialect == "https://json-schema.org/draft-06/schema" ||
dialect == "https://json-schema.org/draft-06/schema#" ||
dialect == "https://json-schema.org/draft-06/hyper-schema" ||
dialect == "https://json-schema.org/draft-06/hyper-schema#" ||
dialect == "https://json-schema.org/draft-04/schema" ||
dialect == "https://json-schema.org/draft-04/schema#" ||
dialect == "https://json-schema.org/draft-04/hyper-schema" ||
dialect == "https://json-schema.org/draft-04/hyper-schema#" ||
dialect == "https://json-schema.org/draft-03/schema" ||
dialect == "https://json-schema.org/draft-03/schema#" ||
dialect == "https://json-schema.org/draft-03/hyper-schema" ||
dialect == "https://json-schema.org/draft-03/hyper-schema#" ||
dialect == "https://json-schema.org/draft-02/schema" ||
dialect == "https://json-schema.org/draft-02/schema#" ||
dialect == "https://json-schema.org/draft-02/hyper-schema" ||
dialect == "https://json-schema.org/draft-02/hyper-schema#" ||
dialect == "https://json-schema.org/draft-01/schema" ||
dialect == "https://json-schema.org/draft-01/schema#" ||
dialect == "https://json-schema.org/draft-01/hyper-schema" ||
dialect == "https://json-schema.org/draft-01/hyper-schema#" ||
dialect == "https://json-schema.org/draft-00/schema" ||
dialect == "https://json-schema.org/draft-00/schema#" ||
dialect == "https://json-schema.org/draft-00/hyper-schema" ||
dialect == "https://json-schema.org/draft-00/hyper-schema#");
return APPLIES_TO_KEYWORDS("$schema");
}

auto transform(sourcemeta::core::JSON &schema, const Result &) const
-> void override {
const auto &old_dialect{schema.at("$schema").to_string()};
std::string new_dialect{"http://"};
new_dialect += old_dialect.substr(8);
schema.at("$schema").into(sourcemeta::core::JSON{new_dialect});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class ModernOfficialDialectWithHttp final : public SchemaTransformRule {
public:
using mutates = std::true_type;
using reframe_after_transform = std::true_type;
ModernOfficialDialectWithHttp()
: SchemaTransformRule{
"modern_official_dialect_with_http",
"The official dialect URI of 2019-09 and later must use "
"\"https://\" instead of \"http://\""} {};

[[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 &location,
const sourcemeta::core::SchemaWalker &,
const sourcemeta::core::SchemaResolver &) const
-> sourcemeta::core::SchemaTransformRule::Result override {
using sourcemeta::core::SchemaBaseDialect;
ONLY_CONTINUE_IF(
location.base_dialect == SchemaBaseDialect::JSON_Schema_2020_12 ||
location.base_dialect == SchemaBaseDialect::JSON_Schema_2020_12_Hyper ||
location.base_dialect == SchemaBaseDialect::JSON_Schema_2019_09 ||
location.base_dialect == SchemaBaseDialect::JSON_Schema_2019_09_Hyper);
ONLY_CONTINUE_IF(schema.is_object() && schema.defines("$schema") &&
schema.at("$schema").is_string());
const auto &dialect{schema.at("$schema").to_string()};
ONLY_CONTINUE_IF(dialect.starts_with("http://json-schema.org/"));
ONLY_CONTINUE_IF(
dialect == "http://json-schema.org/draft/2020-12/schema" ||
dialect == "http://json-schema.org/draft/2020-12/schema#" ||
dialect == "http://json-schema.org/draft/2020-12/hyper-schema" ||
dialect == "http://json-schema.org/draft/2020-12/hyper-schema#" ||
dialect == "http://json-schema.org/draft/2019-09/schema" ||
dialect == "http://json-schema.org/draft/2019-09/schema#" ||
dialect == "http://json-schema.org/draft/2019-09/hyper-schema" ||
dialect == "http://json-schema.org/draft/2019-09/hyper-schema#");
return APPLIES_TO_KEYWORDS("$schema");
}

auto transform(sourcemeta::core::JSON &schema, const Result &) const
-> void override {
const auto &old_dialect{schema.at("$schema").to_string()};
std::string new_dialect{"https://"};
new_dialect += old_dialect.substr(7);
schema.at("$schema").into(sourcemeta::core::JSON{new_dialect});
}
};
73 changes: 73 additions & 0 deletions test/alterschema/alterschema_lint_2019_09_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4811,3 +4811,76 @@ TEST(AlterSchema_lint_2019_09, invalid_external_ref_2) {
EXPECT_TRUE(result.first);
EXPECT_EQ(traces.size(), 0);
}

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

LINT_AND_FIX(document, result, traces);

EXPECT_FALSE(result.first);

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_http_2) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft/2019-09/hyper-schema",
"type": "string"
})JSON");

LINT_AND_FIX(document, result, traces);

EXPECT_FALSE(result.first);

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_http_3) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft/2019-09/schema#",
"type": "string"
})JSON");

LINT_AND_FIX(document, result, traces);

EXPECT_FALSE(result.first);

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_http_already_https) {
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(document, result, traces);

EXPECT_FALSE(result.first);

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);
}
92 changes: 92 additions & 0 deletions test/alterschema/alterschema_lint_2020_12_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10354,3 +10354,95 @@ TEST(AlterSchema_lint_2020_12, invalid_external_ref_11) {
"resolved",
false);
}

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

LINT_AND_FIX(document, result, traces);

EXPECT_FALSE(result.first);

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_http_2) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft/2020-12/hyper-schema",
"type": "string"
})JSON");

LINT_AND_FIX(document, result, traces);

EXPECT_FALSE(result.first);

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_http_3) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft/2020-12/schema#",
"type": "string"
})JSON");

LINT_AND_FIX(document, result, traces);

EXPECT_FALSE(result.first);

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_http_already_https) {
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(document, result, traces);

EXPECT_FALSE(result.first);

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_http_non_dialect_uri) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$ref": "http://json-schema.org/draft/2020-12/meta/applicator"
})JSON");

LINT_AND_FIX(document, result, traces);

EXPECT_FALSE(result.first);

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

EXPECT_EQ(document, expected);
}
18 changes: 18 additions & 0 deletions test/alterschema/alterschema_lint_draft0_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,21 @@ TEST(AlterSchema_lint_draft0, unknown_keywords_prefix_10) {

EXPECT_EQ(document, expected);
}

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

LINT_AND_FIX(document, result, traces);

EXPECT_TRUE(result.first);

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);
}
18 changes: 18 additions & 0 deletions test/alterschema/alterschema_lint_draft1_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1095,3 +1095,21 @@ TEST(AlterSchema_lint_draft1, unknown_keywords_prefix_10) {

EXPECT_EQ(document, expected);
}

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

LINT_AND_FIX(document, result, traces);

EXPECT_FALSE(result.first);

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);
}
Loading
Loading