Skip to content

Linter: create rule to remove additional items when items is an object #1805

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 6 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/extension/alterschema/alterschema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ static auto every_item_is_boolean(const T &container) -> bool {
#include "linter/content_media_type_without_encoding.h"
#include "linter/content_schema_default.h"
#include "linter/content_schema_without_media_type.h"
#include "linter/dangling_additional_items.h"
Copy link
Member

@jviotti jviotti Jul 3, 2025

Choose a reason for hiding this comment

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

Can we all it additional_items_with_schema_items? The dangling part seems confusing at first and other rules follow the with and without convention. I think additional_items_with_schema_items is still short enough and very self-explanatory

Copy link
Member

Choose a reason for hiding this comment

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

Also, you have to update the CMakeLists.txt file to list this file too

#include "linter/dependencies_default.h"
#include "linter/dependencies_property_tautology.h"
#include "linter/dependent_required_default.h"
Expand Down Expand Up @@ -112,6 +113,7 @@ auto add(SchemaTransformer &bundle, const AlterSchemaMode mode)
bundle.add<DuplicateEnumValues>();
bundle.add<DuplicateRequiredValues>();
bundle.add<ConstWithType>();
bundle.add<DanglingAdditionalItems>();
bundle.add<ExclusiveMaximumNumberAndMaximum>();
bundle.add<ExclusiveMinimumNumberAndMinimum>();

Expand Down
30 changes: 30 additions & 0 deletions src/extension/alterschema/linter/dangling_additional_items.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class DanglingAdditionalItems final : public SchemaTransformRule {
public:
DanglingAdditionalItems()
: SchemaTransformRule{
"dangling_additional_items",
"`additionalItems` is ignored when `items` is an object"} {};

[[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 {
return contains_any(
vocabularies,
{"https://json-schema.org/draft/2019-09/vocab/applicator",
"http://json-schema.org/draft-07/schema#",
"http://json-schema.org/draft-06/schema#",
"http://json-schema.org/draft-04/schema#"}) &&
Copy link
Member

Choose a reason for hiding this comment

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

I believe this rule applies to Draft 3 as well. Can you confirm on the spec?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it does, adding tests for it as well

schema.is_object() && schema.defines("items") &&
schema.defines("additionalItems") && schema.at("items").is_object();
}

auto transform(JSON &schema) const -> void override {
schema.erase("additionalItems");
}
};
47 changes: 45 additions & 2 deletions test/alterschema/alterschema_lint_2019_09_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1661,8 +1661,7 @@ TEST(AlterSchema_lint_2019_09, unnecessary_allof_ref_wrapper_5) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"allOf": [
{
"type": "integer",
{ "type": "integer",
Copy link
Member

Choose a reason for hiding this comment

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

Unintended change?

"$ref": "https://example.com"
}
]
Expand All @@ -1680,3 +1679,47 @@ TEST(AlterSchema_lint_2019_09, unnecessary_allof_ref_wrapper_5) {

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, dangling_additional_items_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"items": {
"type": "number"
},
"additionalItems": false
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"items": {
"type": "number"
}
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, dangling_additional_items_2) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"items": {
"unevaluatedProperties": false
},
"additionalItems": {
"type": "string"
}
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"items": {
"unevaluatedProperties": false
}
})JSON");

EXPECT_EQ(document, expected);
}
86 changes: 86 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,89 @@ TEST(AlterSchema_lint_draft4, unnecessary_allof_ref_wrapper_1) {

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_draft4, dangling_additional_items_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
"items": {
"type": "number"
},
"additionalItems": false
})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#",
"items": {
"type": "number"
}
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_draft4, dangling_additional_items_2) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": {
"type": "string"
},
"additionalItems": {
"type": "boolean"
}
})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": "array",
"items": {
"type": "string"
}
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_draft4, dangling_additional_items_no_change_array_items) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
"items": [
{ "type": "string" },
{ "type": "number" }
],
"additionalItems": false
})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#",
"items": [
{ "type": "string" },
{ "type": "number" }
],
"additionalItems": false
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_draft4, dangling_additional_items_no_change_no_items) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalItems": false
})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#",
"additionalItems": false
})JSON");

EXPECT_EQ(document, expected);
}
42 changes: 42 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,45 @@ TEST(AlterSchema_lint_draft6, unnecessary_allof_ref_wrapper_1) {

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_draft6, dangling_additional_items_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-06/schema#",
"items": {
"type": "number"
},
"additionalItems": false
})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#",
"items": {
"type": "number"
}
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_draft6, dangling_additional_items_2) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-06/schema#",
"items": {
"const": "foo"
},
"additionalItems": true
})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#",
"items": {
"const": "foo"
}
})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, dangling_additional_items_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-07/schema#",
"items": {
"type": "number"
},
"additionalItems": false
})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#",
"items": {
"type": "number"
}
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_draft7, dangling_additional_items_2) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"if": { "type": "string" },
"then": { "minLength": 1 }
},
"additionalItems": {
"type": "number"
}
})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": "array",
"items": {
"if": { "type": "string" },
"then": { "minLength": 1 }
}
})JSON");

EXPECT_EQ(document, expected);
}