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 @@ -24,6 +24,7 @@ sourcemeta_library(NAMESPACE sourcemeta PROJECT core NAME alterschema
common/anyof_false_simplify.h
common/anyof_remove_false_schemas.h
common/anyof_true_simplify.h
common/const_in_enum.h
common/const_with_type.h
common/orphan_definitions.h
common/content_media_type_without_encoding.h
Expand Down Expand Up @@ -71,6 +72,7 @@ sourcemeta_library(NAMESPACE sourcemeta PROJECT core NAME alterschema

# Linter
linter/comment_trim.h
linter/const_not_in_enum.h
linter/content_schema_default.h
linter/definitions_to_defs.h
linter/dependencies_default.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 @@ -52,6 +52,7 @@ inline auto APPLIES_TO_POINTERS(std::vector<Pointer> &&keywords)
#include "common/anyof_false_simplify.h"
#include "common/anyof_remove_false_schemas.h"
#include "common/anyof_true_simplify.h"
#include "common/const_in_enum.h"
#include "common/const_with_type.h"
#include "common/content_media_type_without_encoding.h"
#include "common/content_schema_without_media_type.h"
Expand Down Expand Up @@ -99,6 +100,7 @@ inline auto APPLIES_TO_POINTERS(std::vector<Pointer> &&keywords)

// Linter
#include "linter/comment_trim.h"
#include "linter/const_not_in_enum.h"
#include "linter/content_schema_default.h"
#include "linter/definitions_to_defs.h"
#include "linter/dependencies_default.h"
Expand Down Expand Up @@ -176,6 +178,7 @@ auto add(SchemaTransformer &bundle, const AlterSchemaMode mode) -> void {
bundle.add<DuplicateEnumValues>();
bundle.add<DuplicateRequiredValues>();
bundle.add<ConstWithType>();
bundle.add<ConstInEnum>();
bundle.add<NonApplicableAdditionalItems>();
bundle.add<ModernOfficialDialectWithEmptyFragment>();
bundle.add<ExclusiveMaximumNumberAndMaximum>();
Expand Down Expand Up @@ -207,6 +210,7 @@ auto add(SchemaTransformer &bundle, const AlterSchemaMode mode) -> void {

if (mode == AlterSchemaMode::Linter) {
bundle.add<EqualNumericBoundsToConst>();
bundle.add<ConstNotInEnum>();
bundle.add<ContentSchemaDefault>();
bundle.add<DependenciesDefault>();
bundle.add<DependentRequiredDefault>();
Expand Down
34 changes: 34 additions & 0 deletions src/extension/alterschema/common/const_in_enum.h
Copy link
Member

Choose a reason for hiding this comment

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

@AcEKaycgR Actually just a last minor thing: can you move this rule to the common directory? I think its fine to run both here and for the canonicalizer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Understood i will do that now

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class ConstInEnum final : public SchemaTransformRule {
public:
using mutates = std::true_type;
using reframe_after_transform = std::true_type;
ConstInEnum()
: SchemaTransformRule{
"const_in_enum",
"If the `const` and `enum` keyword overlap, then `enum` is "
"redundant and can be removed"} {};

[[nodiscard]] auto
condition(const sourcemeta::core::JSON &schema,
const sourcemeta::core::JSON &,
const sourcemeta::core::Vocabularies &vocabularies,
const sourcemeta::core::SchemaFrame &,
const sourcemeta::core::SchemaFrame::Location &,
const sourcemeta::core::SchemaWalker &,
const sourcemeta::core::SchemaResolver &) const
-> sourcemeta::core::SchemaTransformRule::Result override {
ONLY_CONTINUE_IF(vocabularies.contains_any(
{Vocabularies::Known::JSON_Schema_2020_12_Validation,
Vocabularies::Known::JSON_Schema_2019_09_Validation,
Vocabularies::Known::JSON_Schema_Draft_7,
Vocabularies::Known::JSON_Schema_Draft_6}) &&
schema.is_object() && schema.defines("const") &&
schema.defines("enum") && schema.at("enum").is_array() &&
schema.at("enum").contains(schema.at("const")));
return APPLIES_TO_KEYWORDS("const", "enum");
}

auto transform(JSON &schema, const Result &) const -> void override {
schema.erase("enum");
}
};
30 changes: 30 additions & 0 deletions src/extension/alterschema/linter/const_not_in_enum.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class ConstNotInEnum final : public SchemaTransformRule {
public:
using mutates = std::false_type;
using reframe_after_transform = std::false_type;
ConstNotInEnum()
: SchemaTransformRule{
"const_not_in_enum",
"Do not set the `const` and `enum` keyword at the same time, "
"mainly when their values diverge"} {};

[[nodiscard]] auto
condition(const sourcemeta::core::JSON &schema,
const sourcemeta::core::JSON &,
const sourcemeta::core::Vocabularies &vocabularies,
const sourcemeta::core::SchemaFrame &,
const sourcemeta::core::SchemaFrame::Location &,
const sourcemeta::core::SchemaWalker &,
const sourcemeta::core::SchemaResolver &) const
-> sourcemeta::core::SchemaTransformRule::Result override {
ONLY_CONTINUE_IF(vocabularies.contains_any(
{Vocabularies::Known::JSON_Schema_2020_12_Validation,
Vocabularies::Known::JSON_Schema_2019_09_Validation,
Vocabularies::Known::JSON_Schema_Draft_7,
Vocabularies::Known::JSON_Schema_Draft_6}) &&
schema.is_object() && schema.defines("const") &&
schema.defines("enum") && schema.at("enum").is_array() &&
!schema.at("enum").contains(schema.at("const")));
return APPLIES_TO_KEYWORDS("const", "enum");
}
};
179 changes: 179 additions & 0 deletions test/alterschema/alterschema_lint_2019_09_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4494,3 +4494,182 @@ TEST(AlterSchema_lint_2019_09, empty_object_as_true_1) {

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, const_in_enum_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"title": "Test",
"description": "Test description",
"examples": [1],
"const": 1,
"enum": [1, 2, 3]
})JSON");

LINT_AND_FIX(document, result, traces);

EXPECT_TRUE(result.first);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"title": "Test",
"description": "Test description",
"examples": [1],
"const": 1
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, const_without_enum) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"title": "Test",
"description": "Test description",
"examples": [1],
"const": "foo"
})JSON");

LINT_AND_FIX(document, result, traces);

EXPECT_TRUE(result.first);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"title": "Test",
"description": "Test description",
"examples": [1],
"const": "foo"
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, enum_without_const) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"title": "Test",
"description": "Test description",
"examples": [1],
"enum": [1, 2, 3]
})JSON");

LINT_AND_FIX(document, result, traces);

EXPECT_TRUE(result.first);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"title": "Test",
"description": "Test description",
"examples": [1],
"enum": [1, 2, 3]
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, const_in_enum_4) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"title": "Test",
"description": "Test description",
"examples": [{}],
"properties": {
"foo": {
"const": 1,
"enum": [1, 2, 3]
}
}
})JSON");

LINT_AND_FIX(document, result, traces);

EXPECT_TRUE(result.first);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"title": "Test",
"description": "Test description",
"examples": [{}],
"properties": {
"foo": {
"const": 1
}
}
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, const_in_enum_edge_case_preserves_siblings) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://example.com/schemas/my-schema",
"description": "Edge case schema",
"examples": [{}],
"title": "Edge Case Schema",
"x-custom-annotation": "should not be deleted",
"const": 1,
"enum": [1, 2, 3]
})JSON");

LINT_AND_FIX(document, result, traces);

EXPECT_TRUE(result.first);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://example.com/schemas/my-schema",
"description": "Edge case schema",
"examples": [{}],
"title": "Edge Case Schema",
"x-custom-annotation": "should not be deleted",
"const": 1
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, const_not_in_enum_1) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"title": "Test",
"description": "Test description",
"examples": [1],
"const": 1,
"enum": [2, 3]
})JSON");

LINT_WITHOUT_FIX(document, result, traces);

EXPECT_FALSE(result.first);
EXPECT_EQ(traces.size(), 1);
EXPECT_LINT_TRACE(traces, 0, "", "const_not_in_enum",
"Do not set the `const` and `enum` keyword at the same "
"time, mainly when their values diverge",
false);
}

TEST(AlterSchema_lint_2019_09, const_not_in_enum_2) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"title": "Test",
"description": "Test description",
"examples": [{}],
"properties": {
"foo": {
"const": 1,
"enum": [2, 3]
}
}
})JSON");

LINT_WITHOUT_FIX(document, result, traces);

EXPECT_FALSE(result.first);
EXPECT_EQ(traces.size(), 1);
EXPECT_LINT_TRACE(traces, 0, "/properties/foo", "const_not_in_enum",
"Do not set the `const` and `enum` keyword at the same "
"time, mainly when their values diverge",
false);
}
Loading
Loading