Skip to content

Commit

Permalink
fix(rulesets): fixed array-items type property selector (#2638)
Browse files Browse the repository at this point in the history
Co-authored-by: Vazha Omanashvili <[email protected]>
  • Loading branch information
rainum and Vazha Omanashvili authored Jun 10, 2024
1 parent d2b465c commit 0845fb5
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 4 deletions.
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

0 comments on commit 0845fb5

Please sign in to comment.