Skip to content

Commit ba53bd1

Browse files
author
Konstantin Lapkovsky
committed
Merge branch 'master' into 97-add-custom-authentication-header-support
2 parents 89e45f2 + e939c8f commit ba53bd1

19 files changed

+383
-32
lines changed

src/Http/Middleware/AutoDocMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function handle($request, Closure $next)
1616
{
1717
$response = $next($request);
1818

19-
if ((config('app.env') == 'testing') && !self::$skipped && !empty($request->route())) {
19+
if ((config('app.env') === 'testing') && !self::$skipped && !empty($request->route())) {
2020
app(SwaggerService::class)->addData($request, $response);
2121
}
2222

src/Services/SwaggerService.php

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,60 @@ protected function markAsDeprecated(array $annotations)
275275
$this->item['deprecated'] = Arr::get($annotations, 'deprecated', false);
276276
}
277277

278+
protected function saveResponseSchema(?array $content, string $definition): void
279+
{
280+
if (empty($content)) {
281+
return;
282+
}
283+
284+
$schemaProperties = [];
285+
$schemaType = 'object';
286+
287+
if (array_is_list($content)) {
288+
$this->saveListResponseDefinitions($content, $schemaProperties);
289+
290+
$schemaType = 'array';
291+
} else {
292+
$this->saveObjectResponseDefinitions($content, $schemaProperties, $definition);
293+
}
294+
295+
$this->data['definitions'][$definition] = [
296+
'type' => $schemaType,
297+
'properties' => $schemaProperties
298+
];
299+
}
300+
301+
protected function saveListResponseDefinitions(array $content, array &$schemaProperties): void
302+
{
303+
$types = [];
304+
305+
foreach ($content as $value) {
306+
$type = gettype($value);
307+
308+
if (!in_array($type, $types)) {
309+
$types[] = $type;
310+
$schemaProperties['items']['allOf'][]['type'] = $type;
311+
}
312+
}
313+
}
314+
315+
protected function saveObjectResponseDefinitions(array $content, array &$schemaProperties, string $definition): void
316+
{
317+
$properties = Arr::get($this->data['definitions'], $definition, []);
318+
319+
foreach ($content as $name => $value) {
320+
$property = Arr::get($properties, $name, []);
321+
322+
if (is_null($value)) {
323+
$property['nullable'] = true;
324+
} else {
325+
$property['type'] = gettype($value);
326+
}
327+
328+
$schemaProperties[$name] = $property;
329+
}
330+
}
331+
278332
protected function parseResponse($response)
279333
{
280334
$produceList = $this->data['paths'][$this->uri][$this->method]['produces'];
@@ -319,6 +373,15 @@ protected function parseResponse($response)
319373
$produce
320374
);
321375
}
376+
377+
$action = Str::ucfirst($this->getActionName($this->uri));
378+
$definition = "{$this->method}{$action}{$code}ResponseObject";
379+
380+
$this->saveResponseSchema($content, $definition);
381+
382+
if (is_array($this->item['responses'][$code])) {
383+
$this->item['responses'][$code]['schema']['$ref'] = "#/definitions/{$definition}";
384+
}
322385
}
323386

324387
protected function saveExample($code, $content, $produce)
@@ -418,7 +481,7 @@ protected function saveGetRequestParameters($rules, array $attributes, array $an
418481
}
419482

420483
$existedParameter = Arr::first($this->item['parameters'], function ($existedParameter) use ($parameter) {
421-
return $existedParameter['name'] == $parameter;
484+
return $existedParameter['name'] === $parameter;
422485
});
423486

424487
if (empty($existedParameter)) {
@@ -536,7 +599,7 @@ protected function requestHasBody(): bool
536599
$parameters = $this->data['paths'][$this->uri][$this->method]['parameters'];
537600

538601
$bodyParamExisted = Arr::where($parameters, function ($value) {
539-
return $value['name'] == 'body';
602+
return $value['name'] === 'body';
540603
});
541604

542605
return empty($bodyParamExisted);
@@ -546,7 +609,7 @@ public function getConcreteRequest()
546609
{
547610
$controller = $this->request->route()->getActionName();
548611

549-
if ($controller == 'Closure') {
612+
if ($controller === 'Closure') {
550613
return null;
551614
}
552615

@@ -740,7 +803,7 @@ protected function camelCaseToUnderScore($input): string
740803
$ret = $matches[0];
741804

742805
foreach ($ret as &$match) {
743-
$match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
806+
$match = ($match === strtoupper($match)) ? strtolower($match) : lcfirst($match);
744807
}
745808

746809
return implode('_', $ret);

tests/fixtures/AutoDocMiddlewareTest/tmp_data_search_roles_request.json

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@
6565
}
6666
]
6767
}
68-
]
68+
],
69+
"$ref": "#/definitions/getUsersroles200ResponseObject"
6970
}
7071
}
7172
},
@@ -76,7 +77,20 @@
7677
}
7778
}
7879
},
79-
"definitions": {},
80+
"definitions": {
81+
"getUsersroles200ResponseObject": {
82+
"type": "array",
83+
"properties": {
84+
"items": {
85+
"allOf": [
86+
{
87+
"type": "array"
88+
}
89+
]
90+
}
91+
}
92+
}
93+
},
8094
"info": {
8195
"description": "This is automatically collected documentation",
8296
"version": "0.0.0",

tests/fixtures/SwaggerServiceTest/tmp_data_create_user_request.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"description": "Forbidden",
3131
"schema":
3232
{
33+
"$ref": "#/definitions/postApiusers403ResponseObject",
3334
"example":
3435
{
3536
"message": "This action is unauthorized."
@@ -68,6 +69,14 @@
6869
"first_name": "andrey",
6970
"last_name": "voronin"
7071
}
72+
},
73+
"postApiusers403ResponseObject": {
74+
"type": "object",
75+
"properties": {
76+
"message": {
77+
"type": "string"
78+
}
79+
}
7180
}
7281
},
7382
"info":

tests/fixtures/SwaggerServiceTest/tmp_data_get_user_request.json

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
"id": 2,
6161
"name": "client"
6262
}
63-
}
63+
},
64+
"$ref": "#/definitions/getUsers{id}assignRole{roleId}200ResponseObject"
6465
}
6566
}
6667
},
@@ -71,7 +72,25 @@
7172
}
7273
}
7374
},
74-
"definitions": {},
75+
"definitions": {
76+
"getUsers{id}assignRole{roleId}200ResponseObject": {
77+
"type": "object",
78+
"properties": {
79+
"id": {
80+
"type": "integer"
81+
},
82+
"name": {
83+
"type": "string"
84+
},
85+
"likes_count": {
86+
"type": "integer"
87+
},
88+
"role": {
89+
"type": "array"
90+
}
91+
}
92+
}
93+
},
7594
"info": {
7695
"description": "This is automatically collected documentation",
7796
"version": "0.0.0",

tests/fixtures/SwaggerServiceTest/tmp_data_post_user_request.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
}
5656
]
5757
}
58-
]
58+
],
59+
"$ref": "#/definitions/postUsers200ResponseObject"
5960
}
6061
}
6162
},
@@ -97,6 +98,18 @@
9798
"required": [
9899
"query"
99100
]
101+
},
102+
"postUsers200ResponseObject": {
103+
"type": "array",
104+
"properties": {
105+
"items": {
106+
"allOf": [
107+
{
108+
"type": "array"
109+
}
110+
]
111+
}
112+
}
100113
}
101114
},
102115
"info": {

tests/fixtures/SwaggerServiceTest/tmp_data_post_user_request_with_object_params.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
}
5454
]
5555
}
56-
]
56+
],
57+
"$ref": "#/definitions/postUsers200ResponseObject"
5758
}
5859
}
5960
},
@@ -93,6 +94,18 @@
9394
"notification_settings": "RonasIT\\Support\\Tests\\Support\\Mock\\TestNotificationSetting"
9495
},
9596
"required": ["query"]
97+
},
98+
"postUsers200ResponseObject": {
99+
"type": "array",
100+
"properties": {
101+
"items": {
102+
"allOf": [
103+
{
104+
"type": "array"
105+
}
106+
]
107+
}
108+
}
96109
}
97110
},
98111
"info": {

tests/fixtures/SwaggerServiceTest/tmp_data_put_user_request_with_early_generated_doc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@
103103
"204": {
104104
"description": "Operation successfully done",
105105
"schema": {
106-
"example": null
106+
"example": null,
107+
"$ref": "#/definitions/patchUsers{id}204ResponseObject"
107108
}
108109
}
109110
},

tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_closure_request.json

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
}
4444
]
4545
}
46-
]
46+
],
47+
"$ref": "#/definitions/getUsersroles200ResponseObject"
4748
}
4849
}
4950
},
@@ -52,7 +53,20 @@
5253
}
5354
}
5455
},
55-
"definitions": {},
56+
"definitions": {
57+
"getUsersroles200ResponseObject": {
58+
"type": "array",
59+
"properties": {
60+
"items": {
61+
"allOf": [
62+
{
63+
"type": "array"
64+
}
65+
]
66+
}
67+
}
68+
}
69+
},
5670
"info": {
5771
"description": "This is automatically collected documentation",
5872
"version": "0.0.0",

tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_request.json

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@
6565
}
6666
]
6767
}
68-
]
68+
],
69+
"$ref": "#/definitions/getUsersroles200ResponseObject"
6970
}
7071
}
7172
},
@@ -76,7 +77,20 @@
7677
}
7778
}
7879
},
79-
"definitions": {},
80+
"definitions": {
81+
"getUsersroles200ResponseObject": {
82+
"type": "array",
83+
"properties": {
84+
"items": {
85+
"allOf": [
86+
{
87+
"type": "array"
88+
}
89+
]
90+
}
91+
}
92+
}
93+
},
8094
"info": {
8195
"description": "This is automatically collected documentation",
8296
"version": "0.0.0",

0 commit comments

Comments
 (0)