From 0845fb5b27e372161c9dd0454e8176263b042e08 Mon Sep 17 00:00:00 2001 From: Vazha Omanashvili Date: Mon, 10 Jun 2024 13:22:24 +0300 Subject: [PATCH] fix(rulesets): fixed array-items type property selector (#2638) Co-authored-by: Vazha Omanashvili --- .../src/oas/__tests__/array-items.test.ts | 157 +++++++++++++++++- packages/rulesets/src/oas/index.ts | 24 ++- 2 files changed, 177 insertions(+), 4 deletions(-) diff --git a/packages/rulesets/src/oas/__tests__/array-items.test.ts b/packages/rulesets/src/oas/__tests__/array-items.test.ts index 144ca5d6e..a0ae12dab 100644 --- a/packages/rulesets/src/oas/__tests__/array-items.test.ts +++ b/packages/rulesets/src/oas/__tests__/array-items.test.ts @@ -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: { @@ -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: { @@ -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, + }, + ], + }, ]); diff --git a/packages/rulesets/src/oas/index.ts b/packages/rulesets/src/oas/index.ts index d59122c2f..62057d456 100644 --- a/packages/rulesets/src/oas/index.ts +++ b/packages/rulesets/src/oas/index.ts @@ -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': { @@ -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',