Skip to content

Commit 0845fb5

Browse files
rainumVazha Omanashvili
andauthored
fix(rulesets): fixed array-items type property selector (#2638)
Co-authored-by: Vazha Omanashvili <[email protected]>
1 parent d2b465c commit 0845fb5

File tree

2 files changed

+177
-4
lines changed

2 files changed

+177
-4
lines changed

packages/rulesets/src/oas/__tests__/array-items.test.ts

Lines changed: 155 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,41 @@ testRule('array-items', [
2626
},
2727

2828
{
29-
name: 'array items sibling is present',
29+
name: 'array items sibling is present in a oas2 document',
30+
document: {
31+
swagger: '2.0',
32+
securityDefinitions: {
33+
apikey: {},
34+
$ref: '#/securityDefinitions/apikey',
35+
},
36+
paths: {
37+
$ref: '#/securityDefinitions/apikey',
38+
'/path': {
39+
get: {
40+
'200': {
41+
schema: {
42+
type: 'array',
43+
items: {},
44+
},
45+
},
46+
},
47+
post: {
48+
'201': {
49+
type: 'array',
50+
items: {
51+
type: 'array',
52+
items: {},
53+
},
54+
},
55+
},
56+
},
57+
},
58+
},
59+
errors: [],
60+
},
61+
62+
{
63+
name: 'array items sibling is present in oas3 document',
3064
document: {
3165
$ref: '#/',
3266
responses: {
@@ -46,8 +80,85 @@ testRule('array-items', [
4680
},
4781
errors: [],
4882
},
83+
84+
{
85+
name: 'array items sibling is present in oas3.1 document',
86+
document: {
87+
openapi: '3.1.0',
88+
paths: {
89+
'/path': {
90+
get: {
91+
responses: {
92+
'200': {
93+
type: 'array',
94+
items: {},
95+
},
96+
},
97+
},
98+
post: {
99+
responses: {
100+
'201': {
101+
type: 'array',
102+
items: {
103+
type: 'array',
104+
items: {},
105+
},
106+
},
107+
},
108+
},
109+
},
110+
},
111+
},
112+
errors: [],
113+
},
114+
49115
{
50-
name: 'array items sibling is missing',
116+
name: 'array items sibling is missing in a oas2 document',
117+
document: {
118+
swagger: '2.0',
119+
securityDefinitions: {
120+
apikey: {},
121+
$ref: '#/securityDefinitions/apikey',
122+
},
123+
paths: {
124+
$ref: '#/securityDefinitions/apikey',
125+
'/path': {
126+
get: {
127+
'200': {
128+
schema: {
129+
type: 'array',
130+
},
131+
},
132+
},
133+
post: {
134+
'201': {
135+
type: 'array',
136+
items: {
137+
type: 'array',
138+
},
139+
},
140+
},
141+
},
142+
},
143+
},
144+
errors: [
145+
{
146+
code: 'array-items',
147+
message: 'Schemas with "type: array", require a sibling "items" field',
148+
path: ['paths', '/path', 'get', '200', 'schema'],
149+
severity: DiagnosticSeverity.Error,
150+
},
151+
{
152+
code: 'array-items',
153+
message: 'Schemas with "type: array", require a sibling "items" field',
154+
path: ['paths', '/path', 'post', '201', 'items'],
155+
severity: DiagnosticSeverity.Error,
156+
},
157+
],
158+
},
159+
160+
{
161+
name: 'array items sibling is missing in oas3 document',
51162
document: {
52163
$ref: '#/',
53164
responses: {
@@ -78,4 +189,46 @@ testRule('array-items', [
78189
},
79190
],
80191
},
192+
193+
{
194+
name: 'array items sibling is missing in oas3.1 document',
195+
document: {
196+
openapi: '3.1.0',
197+
paths: {
198+
'/path': {
199+
get: {
200+
responses: {
201+
'200': {
202+
type: 'array',
203+
},
204+
},
205+
},
206+
post: {
207+
responses: {
208+
'201': {
209+
type: 'array',
210+
items: {
211+
type: 'array',
212+
},
213+
},
214+
},
215+
},
216+
},
217+
},
218+
},
219+
errors: [
220+
{
221+
code: 'array-items',
222+
message: 'Schemas with "type: array", require a sibling "items" field',
223+
path: ['paths', '/path', 'get', 'responses', '200'],
224+
severity: DiagnosticSeverity.Error,
225+
},
226+
{
227+
code: 'array-items',
228+
message: 'Schemas with "type: array", require a sibling "items" field',
229+
path: ['paths', '/path', 'post', 'responses', '201', 'items'],
230+
severity: DiagnosticSeverity.Error,
231+
},
232+
],
233+
},
81234
]);

packages/rulesets/src/oas/index.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,27 @@ const ruleset = {
5757
},
5858
],
5959
},
60+
ArrayProperties: {
61+
targets: [
62+
{
63+
formats: [oas2, oas3_0],
64+
given: [
65+
// Check for type: 'array'
66+
'$..[?(@ && @.type=="array")]',
67+
],
68+
},
69+
{
70+
formats: [oas3_1],
71+
given: [
72+
// Still check for type: 'array'
73+
'$..[?(@ && @.type=="array")]',
74+
75+
// also check for type: ['array', ...]
76+
'$..[?(@ && @.type && @.type.constructor.name === "Array" && @.type.includes("array"))]',
77+
],
78+
},
79+
],
80+
},
6081
},
6182
rules: {
6283
'operation-success-response': {
@@ -362,12 +383,11 @@ const ruleset = {
362383
},
363384
},
364385
'array-items': {
365-
formats: [oas3_0],
366386
message: 'Schemas with "type: array", require a sibling "items" field',
367387
severity: 0,
368388
recommended: true,
369389
resolved: false,
370-
given: "$..[?(@.type === 'array')]",
390+
given: '#ArrayProperties',
371391
then: {
372392
function: truthy,
373393
field: 'items',

0 commit comments

Comments
 (0)