Skip to content

Don't change the ordering of enumeration values when removing duplicates #1842

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 1 commit into from
Jul 10, 2025
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
19 changes: 13 additions & 6 deletions src/extension/alterschema/linter/duplicate_enum_values.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,18 @@ class DuplicateEnumValues final : public SchemaTransformRule {
}

auto transform(JSON &schema) const -> void override {
auto collection = schema.at("enum");
std::sort(collection.as_array().begin(), collection.as_array().end());
auto last =
std::unique(collection.as_array().begin(), collection.as_array().end());
collection.erase(last, collection.as_array().end());
schema.at("enum").into(std::move(collection));
// We want to be super careful to maintain the current ordering
// as we delete the duplicates
auto &enumeration{schema.at("enum")};
std::unordered_set<JSON, HashJSON<JSON>> cache;
for (auto iterator = enumeration.as_array().cbegin();
iterator != enumeration.as_array().cend();) {
if (cache.contains(*iterator)) {
iterator = enumeration.erase(iterator);
} else {
cache.emplace(*iterator);
iterator++;
}
}
}
};
2 changes: 1 addition & 1 deletion test/alterschema/alterschema_lint_2019_09_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ TEST(AlterSchema_lint_2019_09, duplicate_enum_values_1) {

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

EXPECT_EQ(document, expected);
Expand Down
18 changes: 17 additions & 1 deletion test/alterschema/alterschema_lint_2020_12_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,23 @@ TEST(AlterSchema_lint_2020_12, duplicate_enum_values_1) {

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"enum": [ 1, 2, 3, {} ]
"enum": [ 1, {}, 2, 3 ]
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2020_12, duplicate_enum_values_2) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"enum": [ "S", "C", "U", "F", "E", "N", "L", "R", "U", null ]
})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",
"enum": [ "S", "C", "U", "F", "E", "N", "L", "R", null ]
})JSON");

EXPECT_EQ(document, expected);
Expand Down
2 changes: 1 addition & 1 deletion test/alterschema/alterschema_lint_draft1_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ TEST(AlterSchema_lint_draft1, duplicate_enum_values_1) {

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-01/schema#",
"enum": [ 1, 2, 3, {} ]
"enum": [ 1, {}, 2, 3 ]
})JSON");

EXPECT_EQ(document, expected);
Expand Down
2 changes: 1 addition & 1 deletion test/alterschema/alterschema_lint_draft2_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ TEST(AlterSchema_lint_draft2, duplicate_enum_values_2) {

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-02/schema#",
"enum": [ 1, 2, 3, {} ]
"enum": [ 1, {}, 2, 3 ]
})JSON");

EXPECT_EQ(document, expected);
Expand Down
2 changes: 1 addition & 1 deletion test/alterschema/alterschema_lint_draft3_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ TEST(AlterSchema_lint_draft3, duplicate_enum_values_3) {

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-03/schema#",
"enum": [ 1, 2, 3, {} ]
"enum": [ 1, {}, 2, 3 ]
})JSON");

EXPECT_EQ(document, expected);
Expand Down
2 changes: 1 addition & 1 deletion test/alterschema/alterschema_lint_draft4_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ TEST(AlterSchema_lint_draft4, duplicate_enum_values_4) {

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
"enum": [ 1, 2, 3, {} ]
"enum": [ 1, {}, 2, 3 ]
})JSON");

EXPECT_EQ(document, expected);
Expand Down
2 changes: 1 addition & 1 deletion test/alterschema/alterschema_lint_draft6_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ TEST(AlterSchema_lint_draft6, duplicate_enum_values_6) {

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-06/schema#",
"enum": [ 1, 2, 3, {} ]
"enum": [ 1, {}, 2, 3 ]
})JSON");

EXPECT_EQ(document, expected);
Expand Down
2 changes: 1 addition & 1 deletion test/alterschema/alterschema_lint_draft7_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ TEST(AlterSchema_lint_draft7, duplicate_enum_values_7) {

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-07/schema#",
"enum": [ 1, 2, 3, {} ]
"enum": [ 1, {}, 2, 3 ]
})JSON");

EXPECT_EQ(document, expected);
Expand Down