Skip to content
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

fix(rulesets): fixed array-items type property selector #2638

Merged
merged 1 commit into from
Jun 10, 2024
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
157 changes: 155 additions & 2 deletions packages/rulesets/src/oas/__tests__/array-items.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,41 @@ testRule('array-items', [
},

{
name: 'array items sibling is present',
name: 'array items sibling is present in a oas2 document',
document: {
swagger: '2.0',
securityDefinitions: {
apikey: {},
$ref: '#/securityDefinitions/apikey',
},
paths: {
$ref: '#/securityDefinitions/apikey',
'/path': {
get: {
'200': {
schema: {
type: 'array',
items: {},
},
},
},
post: {
'201': {
type: 'array',
items: {
type: 'array',
items: {},
},
},
},
},
},
},
errors: [],
},

{
name: 'array items sibling is present in oas3 document',
document: {
$ref: '#/',
responses: {
Expand All @@ -46,8 +80,85 @@ testRule('array-items', [
},
errors: [],
},

{
name: 'array items sibling is present in oas3.1 document',
document: {
openapi: '3.1.0',
paths: {
'/path': {
get: {
responses: {
'200': {
type: 'array',
items: {},
},
},
},
post: {
responses: {
'201': {
type: 'array',
items: {
type: 'array',
items: {},
},
},
},
},
},
},
},
errors: [],
},

{
name: 'array items sibling is missing',
name: 'array items sibling is missing in a oas2 document',
document: {
swagger: '2.0',
securityDefinitions: {
apikey: {},
$ref: '#/securityDefinitions/apikey',
},
paths: {
$ref: '#/securityDefinitions/apikey',
'/path': {
get: {
'200': {
schema: {
type: 'array',
},
},
},
post: {
'201': {
type: 'array',
items: {
type: 'array',
},
},
},
},
},
},
errors: [
{
code: 'array-items',
message: 'Schemas with "type: array", require a sibling "items" field',
path: ['paths', '/path', 'get', '200', 'schema'],
severity: DiagnosticSeverity.Error,
},
{
code: 'array-items',
message: 'Schemas with "type: array", require a sibling "items" field',
path: ['paths', '/path', 'post', '201', 'items'],
severity: DiagnosticSeverity.Error,
},
],
},

{
name: 'array items sibling is missing in oas3 document',
document: {
$ref: '#/',
responses: {
Expand Down Expand Up @@ -78,4 +189,46 @@ testRule('array-items', [
},
],
},

{
name: 'array items sibling is missing in oas3.1 document',
document: {
openapi: '3.1.0',
paths: {
'/path': {
get: {
responses: {
'200': {
type: 'array',
},
},
},
post: {
responses: {
'201': {
type: 'array',
items: {
type: 'array',
},
},
},
},
},
},
},
errors: [
{
code: 'array-items',
message: 'Schemas with "type: array", require a sibling "items" field',
path: ['paths', '/path', 'get', 'responses', '200'],
severity: DiagnosticSeverity.Error,
},
{
code: 'array-items',
message: 'Schemas with "type: array", require a sibling "items" field',
path: ['paths', '/path', 'post', 'responses', '201', 'items'],
severity: DiagnosticSeverity.Error,
},
],
},
]);
24 changes: 22 additions & 2 deletions packages/rulesets/src/oas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ const ruleset = {
},
],
},
ArrayProperties: {
targets: [
{
formats: [oas2, oas3_0],
given: [
// Check for type: 'array'
'$..[?(@ && @.type=="array")]',
],
},
{
formats: [oas3_1],
given: [
// Still check for type: 'array'
'$..[?(@ && @.type=="array")]',

// also check for type: ['array', ...]
'$..[?(@ && @.type && @.type.constructor.name === "Array" && @.type.includes("array"))]',
],
},
],
},
},
rules: {
'operation-success-response': {
Expand Down Expand Up @@ -362,12 +383,11 @@ const ruleset = {
},
},
'array-items': {
formats: [oas3_0],
message: 'Schemas with "type: array", require a sibling "items" field',
severity: 0,
recommended: true,
resolved: false,
given: "$..[?(@.type === 'array')]",
given: '#ArrayProperties',
then: {
function: truthy,
field: 'items',
Expand Down