Skip to content

Commit e60d275

Browse files
author
Lawrence Agbani
committed
Added feature 344 to include @QueryParam annotation for query parameters
1 parent 7a4602b commit e60d275

File tree

5 files changed

+71
-12
lines changed

5 files changed

+71
-12
lines changed

resources/views/partials/route.blade.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
@endif
1818
@endforeach
1919
@endif
20-
@if(count($parsedRoute['parameters'])) \
21-
@foreach($parsedRoute['parameters'] as $attribute => $parameter)
20+
@if(count($parsedRoute['bodyParameters'])) \
21+
@foreach($parsedRoute['bodyParameters'] as $attribute => $parameter)
2222
-d "{{$attribute}}"="{{$parameter['value']}}" @if(! ($loop->last))\
2323
@endif
2424
@endforeach
@@ -32,8 +32,8 @@
3232
"crossDomain": true,
3333
"url": "{{ rtrim(config('app.docs_url') ?: config('app.url'), '/') }}/{{ ltrim($parsedRoute['uri'], '/') }}",
3434
"method": "{{$parsedRoute['methods'][0]}}",
35-
@if(count($parsedRoute['parameters']))
36-
"data": {!! str_replace("\n}","\n }", str_replace(' ',' ',json_encode(array_combine(array_keys($parsedRoute['parameters']), array_map(function($param){ return $param['value']; },$parsedRoute['parameters'])), JSON_PRETTY_PRINT))) !!},
35+
@if(count($parsedRoute['bodyParameters']))
36+
"data": {!! str_replace("\n}","\n }", str_replace(' ',' ',json_encode(array_combine(array_keys($parsedRoute['bodyParameters']), array_map(function($param){ return $param['value']; },$parsedRoute['bodyParameters'])), JSON_PRETTY_PRINT))) !!},
3737
@endif
3838
"headers": {
3939
"accept": "application/json",
@@ -65,14 +65,23 @@
6565
`{{$method}} {{$parsedRoute['uri']}}`
6666

6767
@endforeach
68-
@if(count($parsedRoute['parameters']))
69-
#### Parameters
68+
@if(count($parsedRoute['bodyParameters']))
69+
#### Body Parameters
7070

7171
Parameter | Type | Status | Description
7272
--------- | ------- | ------- | ------- | -----------
73-
@foreach($parsedRoute['parameters'] as $attribute => $parameter)
73+
@foreach($parsedRoute['bodyParameters'] as $attribute => $parameter)
7474
{{$attribute}} | {{$parameter['type']}} | @if($parameter['required']) required @else optional @endif | {!! implode(' ',$parameter['description']) !!}
7575
@endforeach
7676
@endif
77+
@if(count($parsedRoute['queryParameters']))
78+
#### Query Parameters
79+
80+
Parameter | Status | Description
81+
--------- | ------- | ------- | -----------
82+
@foreach($parsedRoute['queryParameters'] as $attribute => $parameter)
83+
{{$attribute}} | @if($parameter['required']) required @else optional @endif | {!! implode(' ',$parameter['description']) !!}
84+
@endforeach
85+
@endif
7786

7887
<!-- END_{{$parsedRoute['id']}} -->

src/Generators/AbstractGenerator.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public function processRoute($route)
6363
'description' => $docBlock['long'],
6464
'methods' => $this->getMethods($route),
6565
'uri' => $this->getUri($route),
66-
'parameters' => $this->getParametersFromDocBlock($docBlock['tags']),
66+
'bodyParameters' => $this->getBodyParametersFromDocBlock($docBlock['tags']),
67+
'queryParameters' => $this->getQueryParametersFromDocBlock($docBlock['tags']),
6768
'response' => $content,
6869
'showresponse' => ! empty($content),
6970
];
@@ -103,7 +104,7 @@ protected function getDocblockResponse($tags)
103104
*
104105
* @return array
105106
*/
106-
protected function getParametersFromDocBlock($tags)
107+
protected function getBodyParametersFromDocBlock($tags)
107108
{
108109
$parameters = collect($tags)
109110
->filter(function ($tag) {
@@ -121,6 +122,28 @@ protected function getParametersFromDocBlock($tags)
121122
return $parameters;
122123
}
123124

125+
/**
126+
* @param array $tags
127+
*
128+
* @return array
129+
*/
130+
protected function getQueryParametersFromDocBlock($tags)
131+
{
132+
$parameters = collect($tags)
133+
->filter(function ($tag) {
134+
return $tag instanceof Tag && $tag->getName() === 'queryParam';
135+
})
136+
->mapWithKeys(function ($tag) {
137+
preg_match('/(.+?)\s+(required\s+)?(.+)/', $tag->getContent(), $content);
138+
list($_, $name, $required, $description) = $content;
139+
$required = trim($required) == 'required' ? true : false;
140+
141+
return [$name => compact('description', 'required')];
142+
})->toArray();
143+
144+
return $parameters;
145+
}
146+
124147
/**
125148
* @param $route
126149
* @param $bindings

src/Postman/CollectionWriter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function getCollection()
4444
'method' => $route['methods'][0],
4545
'body' => [
4646
'mode' => 'formdata',
47-
'formdata' => collect($route['parameters'])->map(function ($parameter, $key) {
47+
'formdata' => collect($route['bodyParameters'])->map(function ($parameter, $key) {
4848
return [
4949
'key' => $key,
5050
'value' => isset($parameter['value']) ? $parameter['value'] : '',

tests/Fixtures/TestController.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ public function withBodyParameters()
3131
return '';
3232
}
3333

34+
/**
35+
* @queryParam location_id required The id of the location.
36+
* @queryParam filters The filters.
37+
*/
38+
public function withQueryParameters()
39+
{
40+
return '';
41+
}
42+
3443
public function checkCustomHeaders(Request $request)
3544
{
3645
return $request->headers->all();

tests/Unit/GeneratorTestCase.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function test_can_parse_endpoint_description()
4444
public function test_can_parse_body_parameters()
4545
{
4646
$route = $this->createRoute('GET', '/api/test', 'withBodyParameters');
47-
$parameters = $this->generator->processRoute($route)['parameters'];
47+
$bodyParameters = $this->generator->processRoute($route)['bodyParameters'];
4848

4949
$this->assertArraySubset([
5050
'user_id' => [
@@ -57,7 +57,25 @@ public function test_can_parse_body_parameters()
5757
'required' => false,
5858
'description' => 'The id of the room.',
5959
],
60-
], $parameters);
60+
], $bodyParameters);
61+
}
62+
63+
/** @test */
64+
public function test_can_parse_query_parameters()
65+
{
66+
$route = $this->createRoute('GET', '/api/test', 'withQueryParameters');
67+
$queryParameters = $this->generator->processRoute($route)['queryParameters'];
68+
69+
$this->assertArraySubset([
70+
'location_id' => [
71+
'required' => true,
72+
'description' => 'The id of the location.',
73+
],
74+
'filters' => [
75+
'required' => false,
76+
'description' => 'The filters.',
77+
],
78+
], $queryParameters);
6179
}
6280

6381
/** @test */

0 commit comments

Comments
 (0)