Skip to content

Commit ff32d9b

Browse files
authored
Merge pull request #123 from RonasIT/description-for-route-parameters
Description for route parameters
2 parents 37ed94f + 0fee66a commit ff32d9b

File tree

4 files changed

+212
-7
lines changed

4 files changed

+212
-7
lines changed

src/Services/SwaggerService.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ protected function getPathParams(): array
240240
$result[] = [
241241
'in' => 'path',
242242
'name' => $key,
243-
'description' => '',
243+
'description' => $this->generatePathDescription($key),
244244
'required' => true,
245245
'type' => 'string'
246246
];
@@ -249,6 +249,25 @@ protected function getPathParams(): array
249249
return $result;
250250
}
251251

252+
protected function generatePathDescription(string $key): string
253+
{
254+
$expression = Arr::get($this->request->route()->wheres, $key);
255+
256+
if (empty($expression)) {
257+
return '';
258+
}
259+
260+
$exploded = explode('|', $expression);
261+
262+
foreach ($exploded as $value) {
263+
if (!preg_match('/^[a-zA-Z0-9\.]+$/', $value)) {
264+
return "regexp: {$expression}";
265+
}
266+
}
267+
268+
return 'in: ' . implode(',', $exploded);
269+
}
270+
252271
protected function parseRequest()
253272
{
254273
$this->saveConsume();

tests/SwaggerServiceTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ public function testAddDataWithoutBoundContract()
690690

691691
$service = app(SwaggerService::class);
692692

693-
$request = $this->generateRequest('get', '/api/users', [], [], [], 'testRequestWithContract');
693+
$request = $this->generateRequest('get', '/api/users', [], [], [], [], 'testRequestWithContract');
694694

695695
$response = $this->generateResponse('example_success_users_response.json', 200, [
696696
'Content-type' => 'application/json'
@@ -731,4 +731,44 @@ public function testSaveProductionData()
731731

732732
app(SwaggerService::class)->saveProductionData();
733733
}
734+
735+
public function testAddDataDescriptionForRouteConditionals()
736+
{
737+
$this->mockDriverGetEmptyAndSaveTpmData(
738+
$this->getJsonFixture('tmp_data_get_route_parameters_description')
739+
);
740+
741+
$request = $this->generateRequest(
742+
type: 'get',
743+
uri: 'v{versions}/users/{id}/{some_string}/{uuid}/{withoutConditional}',
744+
routeConditions: [
745+
[
746+
'method' => 'whereNumber',
747+
'pathParam' => 'id',
748+
],
749+
[
750+
'method' => 'whereIn',
751+
'pathParam' => 'some_string',
752+
'values' => [
753+
'first|second|last'
754+
]
755+
],
756+
[
757+
'method' => 'whereUuid',
758+
'pathParam' => 'uuid',
759+
],
760+
[
761+
'method' => 'whereIn',
762+
'pathParam' => 'versions',
763+
'values' => [
764+
'0.2|1|3.1'
765+
]
766+
],
767+
],
768+
);
769+
770+
$response = $this->generateResponse('example_success_user_response.json');
771+
772+
app(SwaggerService::class)->addData($request, $response);
773+
}
734774
}

tests/TestCase.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,25 @@ protected function clearDirectory($dirPath, $exceptPaths = [])
114114
}
115115
}
116116

117-
protected function generateRequest($type, $uri, $data = [], $pathParams = [], $headers = [], $method = 'test'): Request
117+
protected function generateRequest($type, $uri, $data = [], $pathParams = [], $headers = [], $routeConditions = [], $controllerMethod = 'test'): Request
118118
{
119119
$request = $this->getBaseRequest($type, $uri, $data, $pathParams, $headers);
120120

121-
return $request->setRouteResolver(function () use ($uri, $request, $method) {
122-
return Route::get($uri)
123-
->setAction(['controller' => TestController::class . '@' . $method])
121+
return $request->setRouteResolver(function () use ($uri, $request, $controllerMethod, $routeConditions) {
122+
$route = Route::get($uri)
123+
->setAction(['controller' => TestController::class . '@' . $controllerMethod])
124124
->bind($request);
125+
126+
foreach ($routeConditions as $condition) {
127+
$controllerMethod = $condition['method'];
128+
129+
$route = match ($controllerMethod) {
130+
'whereIn' => $route->whereIn($condition['pathParam'], $condition['values']),
131+
default => $route->{$controllerMethod}($condition['pathParam']),
132+
};
133+
}
134+
135+
return $route;
125136
});
126137
}
127138

@@ -131,7 +142,7 @@ protected function generateGetRolesRequest($method = 'test'): Request
131142
'with' => ['users']
132143
], [], [
133144
'Content-type' => 'application/json'
134-
], $method);
145+
], [], $method);
135146
}
136147

137148
protected function generateResponse($fixture, int $status = 200, array $headers = []): Response
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
{
2+
"swagger": "2.0",
3+
"host": "localhost",
4+
"basePath": "/",
5+
"schemes": [],
6+
"paths": {
7+
"/v{versions}/users/{id}/{some_string}/{uuid}/{withoutConditional}": {
8+
"get": {
9+
"tags": [
10+
"v{versions}"
11+
],
12+
"consumes": [],
13+
"produces": [
14+
"application/json"
15+
],
16+
"parameters": [
17+
{
18+
"in": "path",
19+
"name": "versions",
20+
"description": "in: 0.2,1,3.1",
21+
"required": true,
22+
"type": "string"
23+
},
24+
{
25+
"in": "path",
26+
"name": "id",
27+
"description": "regexp: [0-9]+",
28+
"required": true,
29+
"type": "string"
30+
},
31+
{
32+
"in": "path",
33+
"name": "some_string",
34+
"description": "in: first,second,last",
35+
"required": true,
36+
"type": "string"
37+
},
38+
{
39+
"in": "path",
40+
"name": "uuid",
41+
"description": "regexp: [\\da-fA-F]{8}-[\\da-fA-F]{4}-[\\da-fA-F]{4}-[\\da-fA-F]{4}-[\\da-fA-F]{12}",
42+
"required": true,
43+
"type": "string"
44+
},
45+
{
46+
"in": "path",
47+
"name": "withoutConditional",
48+
"description": "",
49+
"required": true,
50+
"type": "string"
51+
},
52+
{
53+
"in": "query",
54+
"name": "query",
55+
"description": "string, required",
56+
"required": true,
57+
"type": "string"
58+
},
59+
{
60+
"in": "query",
61+
"name": "user_id",
62+
"description": "integer, with_to_array_rule_string_name",
63+
"type": "integer"
64+
},
65+
{
66+
"in": "query",
67+
"name": "is_email_enabled",
68+
"description": "test_rule_without_to_string",
69+
"type": "string"
70+
}
71+
],
72+
"responses": {
73+
"200": {
74+
"description": "Operation successfully done",
75+
"schema": {
76+
"example": {
77+
"id": 2,
78+
"name": "first_client",
79+
"likes_count": 23,
80+
"role": {
81+
"id": 2,
82+
"name": "client"
83+
},
84+
"type": "reader"
85+
},
86+
"$ref": "#/definitions/getV{versions}users{id}{someString}{uuid}{withoutConditional}200ResponseObject"
87+
}
88+
}
89+
},
90+
"security": [],
91+
"description": "",
92+
"deprecated": false,
93+
"summary": "test"
94+
}
95+
}
96+
},
97+
"definitions": {
98+
"getV{versions}users{id}{someString}{uuid}{withoutConditional}200ResponseObject": {
99+
"type": "object",
100+
"properties": {
101+
"id": {
102+
"type": "integer"
103+
},
104+
"name": {
105+
"type": "string"
106+
},
107+
"likes_count": {
108+
"type": "integer"
109+
},
110+
"role": {
111+
"type": "array"
112+
},
113+
"type": {
114+
"type": "string"
115+
}
116+
}
117+
}
118+
},
119+
"info": {
120+
"description": "This is automatically collected documentation",
121+
"version": "0.0.0",
122+
"title": "Name of Your Application",
123+
"termsOfService": "",
124+
"contact": {
125+
"email": "your@email.com"
126+
}
127+
},
128+
"securityDefinitions": {
129+
"jwt": {
130+
"type": "apiKey",
131+
"name": "authorization",
132+
"in": "header"
133+
}
134+
}
135+
}

0 commit comments

Comments
 (0)