Skip to content

Commit d020f16

Browse files
authored
Merge pull request #64 from RonasIT/20_limit_response_data
#20 :- limit response data
2 parents e7d92ee + a40fd60 commit d020f16

9 files changed

+404
-15
lines changed

config/auto-doc.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,16 @@
142142
*/
143143
'additional_paths' => [],
144144

145+
/*
146+
|--------------------------------------------------------------------------
147+
| Response example array items limit
148+
|--------------------------------------------------------------------------
149+
| All array responses will be automatically cut for the config items count
150+
|
151+
| Note: you should collect documentation after the config change
152+
*/
153+
'response_example_limit_count' => 5,
154+
145155
/*
146156
|--------------------------------------------------------------------------
147157
| Swagger documentation visibility environments list
@@ -154,5 +164,5 @@
154164
'development'
155165
],
156166

157-
'config_version' => '2.3'
167+
'config_version' => '2.4'
158168
];

src/Services/SwaggerService.php

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ class SwaggerService
4848
'int' => 'integer'
4949
];
5050

51-
protected $documentation;
52-
5351
public function __construct(Container $container)
5452
{
5553
$this->initConfig();
@@ -272,12 +270,32 @@ protected function parseResponse($response)
272270
}
273271

274272
$responses = $this->item['responses'];
273+
274+
$responseExampleLimitCount = config('auto-doc.response_example_limit_count');
275+
276+
$content = json_decode($response->getContent(), true);
277+
278+
if (!empty($responseExampleLimitCount)) {
279+
if (!empty($content['data'])) {
280+
$limitedResponseData = array_slice($content['data'], 0, $responseExampleLimitCount, true);
281+
$content['data'] = $limitedResponseData;
282+
$content['to'] = count($limitedResponseData);
283+
$content['total'] = count($limitedResponseData);
284+
}
285+
}
286+
287+
if (!empty($content['exception'])) {
288+
$uselessKeys = array_keys(Arr::except($content, ['message']));
289+
290+
$content = Arr::except($content, $uselessKeys);
291+
}
292+
275293
$code = $response->getStatusCode();
276294

277295
if (!in_array($code, $responses)) {
278296
$this->saveExample(
279-
$response->getStatusCode(),
280-
$response->getContent(),
297+
$code,
298+
json_encode($content, JSON_PRETTY_PRINT),
281299
$produce
282300
);
283301
}
@@ -621,7 +639,7 @@ public function saveProductionData()
621639

622640
public function getDocFileContent()
623641
{
624-
$this->documentation = $this->driver->getDocumentation();
642+
$documentation = $this->driver->getDocumentation();
625643

626644
$additionalDocs = config('auto-doc.additional_paths', []);
627645

@@ -633,15 +651,15 @@ public function getDocFileContent()
633651
foreach ($paths as $path) {
634652
$additionalDocPath = $fileContent['paths'][$path];
635653

636-
if (empty($this->documentation['paths'][$path])) {
637-
$this->documentation['paths'][$path] = $additionalDocPath;
654+
if (empty($documentation['paths'][$path])) {
655+
$documentation['paths'][$path] = $additionalDocPath;
638656
} else {
639-
$methods = array_keys($this->documentation['paths'][$path]);
657+
$methods = array_keys($documentation['paths'][$path]);
640658
$additionalDocMethods = array_keys($additionalDocPath);
641659

642660
foreach ($additionalDocMethods as $method) {
643661
if (!in_array($method, $methods)) {
644-
$this->documentation['paths'][$path][$method] = $additionalDocPath[$method];
662+
$documentation['paths'][$path][$method] = $additionalDocPath[$method];
645663
}
646664
}
647665
}
@@ -650,13 +668,13 @@ public function getDocFileContent()
650668
$definitions = array_keys($fileContent['definitions']);
651669

652670
foreach ($definitions as $definition) {
653-
if (empty($this->documentation['definitions'][$definition])) {
654-
$this->documentation['definitions'][$definition] = $fileContent['definitions'][$definition];
671+
if (empty($documentation['definitions'][$definition])) {
672+
$documentation['definitions'][$definition] = $fileContent['definitions'][$definition];
655673
}
656674
}
657675
}
658676

659-
return $this->documentation;
677+
return $documentation;
660678
}
661679

662680
protected function camelCaseToUnderScore($input): string

tests/SwaggerServiceTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,39 @@ public function testAddDataPostRequest()
210210

211211
$service->addData($request, $response);
212212
}
213+
214+
public function testCutExceptions()
215+
{
216+
$this->mockDriverSaveTmpData($this->getJsonFixture('tmp_data_create_user_request'));
217+
218+
$service = app(SwaggerService::class);
219+
220+
$request = $this->generateRequest('post', '/api/users', [
221+
'first_name' => 'andrey',
222+
'last_name' => 'voronin'
223+
]);
224+
225+
$response = new Response($this->getFixture('example_forbidden_user_response.json'), 403, [
226+
'Content-type' => 'application/json'
227+
]);
228+
229+
$service->addData($request, $response);
230+
}
231+
232+
public function testLimitResponseData()
233+
{
234+
config(['auto-doc.response_example_limit_count' => 1]);
235+
236+
$this->mockDriverSaveTmpData($this->getJsonFixture('tmp_data_search_users_request'));
237+
238+
$service = app(SwaggerService::class);
239+
240+
$request = $this->generateRequest('get', '/api/users');
241+
242+
$response = new Response($this->getFixture('example_success_users_response.json'), 200, [
243+
'Content-type' => 'application/json'
244+
]);
245+
246+
$service->addData($request, $response);
247+
}
213248
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"message": "This action is unauthorized.",
3+
"exception": "Symfony\\Component\\HttpKernel\\Exception\\AccessDeniedHttpException",
4+
"file": "\/app\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Exceptions\/Handler.php",
5+
"line": 384,
6+
"trace": [
7+
{
8+
"file": "\/app\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Exceptions\/Handler.php",
9+
"line": 356,
10+
"function": "prepareException",
11+
"class": "Illuminate\\Foundation\\Exceptions\\Handler",
12+
"type": "->"
13+
},
14+
{
15+
"file": "\/app\/vendor\/laravel\/framework\/src\/Illuminate\/Routing\/Pipeline.php",
16+
"line": 51,
17+
"function": "render",
18+
"class": "Illuminate\\Foundation\\Exceptions\\Handler",
19+
"type": "->"
20+
}
21+
]
22+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
{
2+
"current_page": 1,
3+
"data": [
4+
{
5+
"id": 1,
6+
"first_name": "Billy",
7+
"last_name": "Coleman",
8+
"email": "billy.coleman@example.com",
9+
"created_at": null,
10+
"updated_at": null,
11+
"role_id": 1,
12+
"date_of_birth": "1986-05-20",
13+
"phone": "+79535482530",
14+
"position": "admin",
15+
"starts_on": "2022-04-16 00:00:00",
16+
"hr_id": null,
17+
"manager_id": null,
18+
"lead_id": null,
19+
"avatar_id": null,
20+
"deleted_at": null,
21+
"company_id": 1
22+
},
23+
{
24+
"id": 2,
25+
"first_name": "Charlotte",
26+
"last_name": "Lyons",
27+
"email": "flavell@example.com",
28+
"created_at": null,
29+
"updated_at": null,
30+
"role_id": 2,
31+
"date_of_birth": "1992-12-04",
32+
"phone": "89255892221",
33+
"position": "manager",
34+
"starts_on": "2022-04-21 00:00:00",
35+
"hr_id": 1,
36+
"manager_id": 1,
37+
"lead_id": 1,
38+
"avatar_id": 1,
39+
"deleted_at": null,
40+
"company_id": 1
41+
},
42+
{
43+
"id": 3,
44+
"first_name": "Andrew",
45+
"last_name": "Montgomery",
46+
"email": "retoh@example.com",
47+
"created_at": null,
48+
"updated_at": null,
49+
"role_id": 3,
50+
"date_of_birth": "2001-06-30",
51+
"phone": "89162002943",
52+
"position": "intern",
53+
"starts_on": "2022-04-26 00:00:00",
54+
"hr_id": 2,
55+
"manager_id": 2,
56+
"lead_id": 1,
57+
"avatar_id": 2,
58+
"deleted_at": null,
59+
"company_id": 1
60+
},
61+
{
62+
"id": 4,
63+
"first_name": "Alexey",
64+
"last_name": "Petrov",
65+
"email": "alex@example.com",
66+
"created_at": null,
67+
"updated_at": null,
68+
"role_id": 3,
69+
"date_of_birth": "2001-06-30",
70+
"phone": "891820029004",
71+
"position": "hr",
72+
"starts_on": "2022-05-09 00:00:00",
73+
"hr_id": 2,
74+
"manager_id": 2,
75+
"lead_id": 1,
76+
"avatar_id": null,
77+
"deleted_at": "2018-11-11T11:11:11.000000Z",
78+
"company_id": 1
79+
},
80+
{
81+
"id": 10,
82+
"first_name": "Billy3",
83+
"last_name": "Coleman2",
84+
"email": "billy.coleman1@example.com",
85+
"created_at": null,
86+
"updated_at": null,
87+
"role_id": 2,
88+
"date_of_birth": "1986-05-20",
89+
"phone": "+795354825303",
90+
"position": "admin",
91+
"starts_on": "2022-04-16 00:00:00",
92+
"hr_id": null,
93+
"manager_id": null,
94+
"lead_id": null,
95+
"avatar_id": null,
96+
"deleted_at": null,
97+
"company_id": 1
98+
}
99+
],
100+
"first_page_url": "http:\/\/localhost\/api\/users?page=1",
101+
"from": 1,
102+
"last_page": 1,
103+
"last_page_url": "http:\/\/localhost\/api\/users?page=1",
104+
"links": [
105+
{
106+
"url": null,
107+
"label": "« Previous",
108+
"active": false
109+
},
110+
{
111+
"url": "http:\/\/localhost\/api\/users?page=1",
112+
"label": "1",
113+
"active": true
114+
},
115+
{
116+
"url": null,
117+
"label": "Next »",
118+
"active": false
119+
}],
120+
"next_page_url": null,
121+
"path": "http:\/\/localhost\/api\/users",
122+
"per_page": 20,
123+
"prev_page_url": null,
124+
"to": 6,
125+
"total": 6
126+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"swagger": "2.0",
3+
"host": "localhost",
4+
"basePath": "\/",
5+
"schemes": [],
6+
"paths":
7+
{
8+
"\/api\/users":
9+
{
10+
"post":
11+
{
12+
"tags": ["api"],
13+
"consumes": ["application\/x-www-form-urlencoded"],
14+
"produces": ["application\/json"],
15+
"parameters": [
16+
{
17+
"in": "body",
18+
"name": "body",
19+
"description": "",
20+
"required": true,
21+
"schema": {
22+
"$ref": "#/definitions/apiusersObject"
23+
}
24+
}
25+
],
26+
"responses":
27+
{
28+
"403":
29+
{
30+
"description": "Forbidden",
31+
"schema":
32+
{
33+
"example":
34+
{
35+
"message": "This action is unauthorized."
36+
}
37+
}
38+
}
39+
},
40+
"security": [],
41+
"description": "",
42+
"summary": "test"
43+
}
44+
}
45+
},
46+
"definitions": {
47+
"apiusersObject": {
48+
"type": "object",
49+
"properties": {
50+
"query": {
51+
"type": "string",
52+
"description": ""
53+
}
54+
},
55+
"required": {
56+
"0": "query"
57+
},
58+
"example": {
59+
"first_name": "andrey",
60+
"last_name": "voronin"
61+
}
62+
}
63+
},
64+
"info":
65+
{
66+
"description": "This is automatically collected documentation",
67+
"version": "0.0.0",
68+
"title": "Name of Your Application",
69+
"termsOfService": "",
70+
"contact":
71+
{
72+
"email": "your@email.com"
73+
}
74+
}
75+
}

tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_request_pdf.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"200": {
2929
"description": "Operation successfully done",
3030
"schema": {
31-
"example": "WwogIHsKICAgICJpZCI6IDEsCiAgICAibmFtZSI6ICJhZG1pbiIsCiAgICAidXNlcnMiOiBbCiAgICAgIHsKICAgICAgICAiaWQiOiAxLAogICAgICAgICJuYW1lIjogImFkbWluIgogICAgICB9CiAgICBdCiAgfSwKICB7CiAgICAiaWQiOiAyLAogICAgIm5hbWUiOiAiY2xpZW50IiwKICAgICJ1c2VycyI6IFsKICAgICAgewogICAgICAgICJpZCI6IDIsCiAgICAgICAgIm5hbWUiOiAiZmlyc3RfY2xpZW50IgogICAgICB9LAogICAgICB7CiAgICAgICAgImlkIjogMywKICAgICAgICAibmFtZSI6ICJzZWNvbmRfY2xpZW50IgogICAgICB9CiAgICBdCiAgfQpdCg=="
31+
"example": "WwogICAgewogICAgICAgICJpZCI6IDEsCiAgICAgICAgIm5hbWUiOiAiYWRtaW4iLAogICAgICAgICJ1c2VycyI6IFsKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgImlkIjogMSwKICAgICAgICAgICAgICAgICJuYW1lIjogImFkbWluIgogICAgICAgICAgICB9CiAgICAgICAgXQogICAgfSwKICAgIHsKICAgICAgICAiaWQiOiAyLAogICAgICAgICJuYW1lIjogImNsaWVudCIsCiAgICAgICAgInVzZXJzIjogWwogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAiaWQiOiAyLAogICAgICAgICAgICAgICAgIm5hbWUiOiAiZmlyc3RfY2xpZW50IgogICAgICAgICAgICB9LAogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAiaWQiOiAzLAogICAgICAgICAgICAgICAgIm5hbWUiOiAic2Vjb25kX2NsaWVudCIKICAgICAgICAgICAgfQogICAgICAgIF0KICAgIH0KXQ=="
3232
}
3333
}
3434
},

0 commit comments

Comments
 (0)