Skip to content

Commit 601a8fa

Browse files
committed
test: check unsupported bodyRequest format
1 parent c69c90b commit 601a8fa

File tree

4 files changed

+311
-3
lines changed

4 files changed

+311
-3
lines changed

src/Validators/SwaggerSpecValidator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,10 @@ protected function validateResponse(array $response, string $statusCode, string
202202
array_merge(self::SCHEMA_TYPES, ['file']),
203203
"{$responseId}.schema"
204204
);
205-
}
206205

207-
if (!empty($response['items'])) {
208-
$this->validateItems($response['items'], "{$responseId}.items");
206+
if (!empty($response['schema']['items'])) {
207+
$this->validateItems($response['schema']['items'], "{$responseId}.schema.items");
208+
}
209209
}
210210
}
211211

tests/SwaggerServiceTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,16 @@ public static function getConstructorInvalidTmpData(): array
289289
'exception' => InvalidSwaggerSpecException::class,
290290
'exceptionMessage' => "Validation failed. Field 'securityDefinitions.0.in' has an invalid value: invalid. Allowed values: query, header.",
291291
],
292+
[
293+
'tmpDoc' => 'documentation/invalid_format__request_body__invalid_content',
294+
'exception' => InvalidSwaggerSpecException::class,
295+
'exceptionMessage' => "Validation failed. Operation 'paths./users/{id}.post' has body parameters. Only one or the other is allowed.",
296+
],
297+
[
298+
'tmpDoc' => 'documentation/invalid_format__response__invalid_items',
299+
'exception' => InvalidSwaggerSpecException::class,
300+
'exceptionMessage' => "Validation failed. 'paths./users/{id}.post.responses.200.schema.items' should have required fields: type.",
301+
],
292302
];
293303
}
294304

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"openapi": "3.1.0",
3+
"info": {
4+
"version": "1.0.0",
5+
"title": "Invalid API"
6+
},
7+
"paths": {
8+
"/users/{id}": {
9+
"post": {
10+
"consumes": [],
11+
"produces": [
12+
"application/json"
13+
],
14+
"parameters": [
15+
{
16+
"in": "path",
17+
"name": "id",
18+
"description": "",
19+
"required": true,
20+
"schema": {
21+
"type": "string"
22+
}
23+
}
24+
],
25+
"requestBody": {
26+
"content": {
27+
"image/png": {
28+
"items": []
29+
}
30+
},
31+
"required": true,
32+
"description": ""
33+
},
34+
"responses": {
35+
"default": {
36+
"description": "hello world"
37+
}
38+
}
39+
}
40+
}
41+
}
42+
}
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
{
2+
"openapi": "3.1.0",
3+
"info": {
4+
"version": "1.0.0",
5+
"title": "Swagger Petstore 2.0"
6+
},
7+
"host": "petstore.swagger.io",
8+
"basePath": "/v2",
9+
"tags": [
10+
{
11+
"name": "pet",
12+
"description": "Everything about your Pets",
13+
"externalDocs": {
14+
"description": "Find out more",
15+
"url": "http://swagger.io"
16+
}
17+
},
18+
{
19+
"name": "store",
20+
"description": "Access to Petstore orders"
21+
},
22+
{
23+
"name": "user",
24+
"description": "Operations about user",
25+
"externalDocs": {
26+
"description": "Find out more about our store",
27+
"url": "http://swagger.io"
28+
}
29+
}
30+
],
31+
"schemes": [
32+
"https",
33+
"http"
34+
],
35+
"paths": {
36+
"/users/{id}": {
37+
"post": {
38+
"tags": [
39+
"pet"
40+
],
41+
"summary": "Finds Pets by status",
42+
"description": "Multiple status values can be provided with comma separated strings",
43+
"operationId": "findPetsByStatus",
44+
"produces": [
45+
"application/xml",
46+
"application/json"
47+
],
48+
"parameters": [
49+
{
50+
"in": "path",
51+
"name": "id",
52+
"description": "",
53+
"required": true,
54+
"schema": {
55+
"type": "string"
56+
}
57+
}
58+
],
59+
"responses": {
60+
"200": {
61+
"description": "successful operation",
62+
"schema": {
63+
"type": "array",
64+
"items": {
65+
"$ref": "#/definitions/Pet"
66+
}
67+
}
68+
},
69+
"400": {
70+
"description": "Invalid status value"
71+
}
72+
},
73+
"security": [
74+
{
75+
"petstore_auth": [
76+
"write:pets",
77+
"read:pets"
78+
]
79+
}
80+
]
81+
}
82+
}
83+
},
84+
"securityDefinitions": {
85+
"petstore_auth": {
86+
"type": "oauth2",
87+
"authorizationUrl": "http://petstore.swagger.io/oauth/dialog",
88+
"flow": "implicit",
89+
"scopes": {
90+
"write:pets": "modify pets in your account",
91+
"read:pets": "read your pets"
92+
}
93+
},
94+
"api_key": {
95+
"type": "apiKey",
96+
"name": "api_key",
97+
"in": "header"
98+
}
99+
},
100+
"definitions": {
101+
"Order": {
102+
"type": "object",
103+
"properties": {
104+
"id": {
105+
"type": "integer",
106+
"format": "int64"
107+
},
108+
"petId": {
109+
"type": "integer",
110+
"format": "int64"
111+
},
112+
"quantity": {
113+
"type": "integer",
114+
"format": "int32"
115+
},
116+
"shipDate": {
117+
"type": "string",
118+
"format": "date-time"
119+
},
120+
"status": {
121+
"type": "string",
122+
"description": "Order Status",
123+
"enum": [
124+
"placed",
125+
"approved",
126+
"delivered"
127+
]
128+
},
129+
"complete": {
130+
"type": "boolean",
131+
"default": false
132+
}
133+
},
134+
"xml": {
135+
"name": "Order"
136+
}
137+
},
138+
"Category": {
139+
"type": "object",
140+
"properties": {
141+
"id": {
142+
"type": "integer",
143+
"format": "int64"
144+
},
145+
"name": {
146+
"type": "string"
147+
}
148+
},
149+
"xml": {
150+
"name": "Category"
151+
}
152+
},
153+
"User": {
154+
"type": "object",
155+
"properties": {
156+
"id": {
157+
"type": "integer",
158+
"format": "int64"
159+
},
160+
"username": {
161+
"type": "string"
162+
},
163+
"firstName": {
164+
"type": "string"
165+
},
166+
"lastName": {
167+
"type": "string"
168+
},
169+
"email": {
170+
"type": "string"
171+
},
172+
"password": {
173+
"type": "string"
174+
},
175+
"phone": {
176+
"type": "string"
177+
},
178+
"userStatus": {
179+
"type": "integer",
180+
"format": "int32",
181+
"description": "User Status"
182+
}
183+
},
184+
"xml": {
185+
"name": "User"
186+
}
187+
},
188+
"Tag": {
189+
"type": "object",
190+
"properties": {
191+
"id": {
192+
"type": "integer",
193+
"format": "int64"
194+
},
195+
"name": {
196+
"type": "string"
197+
}
198+
},
199+
"xml": {
200+
"name": "Tag"
201+
}
202+
},
203+
"Pet": {
204+
"type": "object",
205+
"required": [
206+
"name",
207+
"photoUrls"
208+
],
209+
"properties": {
210+
"id": {
211+
"type": "integer",
212+
"format": "int64"
213+
},
214+
"category": {
215+
"$ref": "#/definitions/Category"
216+
},
217+
"name": {
218+
"type": "string",
219+
"example": "doggie"
220+
},
221+
"photoUrls": {
222+
"type": "array",
223+
"xml": {
224+
"name": "photoUrl",
225+
"wrapped": true
226+
},
227+
"items": {
228+
"type": "string"
229+
}
230+
},
231+
"tags": {
232+
"type": "array",
233+
"xml": {
234+
"name": "tag",
235+
"wrapped": true
236+
},
237+
"items": {
238+
"$ref": "#/definitions/Tag"
239+
}
240+
},
241+
"status": {
242+
"type": "string",
243+
"description": "pet status in the store",
244+
"enum": [
245+
"available",
246+
"pending",
247+
"sold"
248+
]
249+
}
250+
},
251+
"xml": {
252+
"name": "Pet"
253+
}
254+
}
255+
}
256+
}

0 commit comments

Comments
 (0)