Skip to content

Commit 96f998a

Browse files
authored
Merge pull request #383 from raznerdeveloper/master
PR to close issue 344 for queryParams addition
2 parents 19a3542 + 1ced3ec commit 96f998a

File tree

5 files changed

+85
-14
lines changed

5 files changed

+85
-14
lines changed

resources/views/partials/route.blade.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
@endif
2626
@endforeach
2727
@endif
28-
@if(count($route['parameters'])) \
29-
@foreach($route['parameters'] as $attribute => $parameter)
30-
-d "{{$attribute}}"={{$parameter['value']}} @if(! ($loop->last))\
28+
29+
@if(count($route['bodyParameters'])) \
30+
@foreach($route['bodyParameters'] as $attribute => $parameter)
31+
-d "{{$attribute}}"="{{$parameter['value']}}" @if(! ($loop->last))\
3132
@endif
3233
@endforeach
3334
@endif
34-
3535
```
3636

3737
```javascript
@@ -40,8 +40,8 @@
4040
"crossDomain": true,
4141
"url": "{{ rtrim(config('app.docs_url') ?: config('app.url'), '/') }}/{{ ltrim($route['uri'], '/') }}",
4242
"method": "{{$route['methods'][0]}}",
43-
@if(count($route['parameters']))
44-
"data": {!! str_replace("\n}","\n }", str_replace(' ',' ',json_encode(array_combine(array_keys($route['parameters']), array_map(function($param){ return $param['value']; },$route['parameters'])), JSON_PRETTY_PRINT))) !!},
43+
@if(count($route['bodyParameters']))
44+
"data": {!! str_replace("\n}","\n }", str_replace(' ',' ',json_encode(array_combine(array_keys($route['bodyParameters']), array_map(function($param){ return $param['value']; },$route['bodyParameters'])), JSON_PRETTY_PRINT))) !!},
4545
@endif
4646
"headers": {
4747
"accept": "application/json",
@@ -73,14 +73,23 @@
7373
`{{$method}} {{$route['uri']}}`
7474

7575
@endforeach
76-
@if(count($route['parameters']))
77-
#### Parameters
76+
@if(count($route['bodyParameters']))
77+
#### Body Parameters
7878

7979
Parameter | Type | Status | Description
8080
--------- | ------- | ------- | ------- | -----------
81-
@foreach($route['parameters'] as $attribute => $parameter)
81+
@foreach($route['bodyParameters'] as $attribute => $parameter)
8282
{{$attribute}} | {{$parameter['type']}} | @if($parameter['required']) required @else optional @endif | {!! $parameter['description'] !!}
8383
@endforeach
8484
@endif
85+
@if(count($route['queryParameters']))
86+
#### Query Parameters
87+
88+
Parameter | Status | Description
89+
--------- | ------- | ------- | -----------
90+
@foreach($route['queryParameters'] as $attribute => $parameter)
91+
{{$attribute}} | @if($parameter['required']) required @else optional @endif | {!! implode(' ',$parameter['description']) !!}
92+
@endforeach
93+
@endif
8594

8695
<!-- END_{{$route['id']}} -->

src/Generators/Generator.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public function processRoute(Route $route, array $rulesToApply = [])
5656
'description' => $docBlock['long'],
5757
'methods' => $this->getMethods($route),
5858
'uri' => $this->getUri($route),
59-
'parameters' => $this->getParametersFromDocBlock($docBlock['tags']),
59+
'bodyParameters' => $this->getBodyParametersFromDocBlock($docBlock['tags']),
60+
'queryParameters' => $this->getQueryParametersFromDocBlock($docBlock['tags']),
6061
'authenticated' => $this->getAuthStatusFromDocBlock($docBlock['tags']),
6162
'response' => $content,
6263
'showresponse' => ! empty($content),
@@ -71,7 +72,7 @@ public function processRoute(Route $route, array $rulesToApply = [])
7172
*
7273
* @return array
7374
*/
74-
protected function getParametersFromDocBlock(array $tags)
75+
protected function getBodyParametersFromDocBlock(array $tags)
7576
{
7677
$parameters = collect($tags)
7778
->filter(function ($tag) {
@@ -103,6 +104,40 @@ protected function getParametersFromDocBlock(array $tags)
103104
return $parameters;
104105
}
105106

107+
/**
108+
* @param array $tags
109+
*
110+
* @return array
111+
*/
112+
protected function getQueryParametersFromDocBlock(array $tags)
113+
{
114+
$parameters = collect($tags)
115+
->filter(function ($tag) {
116+
return $tag instanceof Tag && $tag->getName() === 'queryParam';
117+
})
118+
->mapWithKeys(function ($tag) {
119+
preg_match('/(.+?)\s+(required\s+)?(.*)/', $tag->getContent(), $content);
120+
if (empty($content)) {
121+
// this means only name was supplied
122+
list($name) = preg_split('/\s+/', $tag->getContent());
123+
$required = false;
124+
$description = '';
125+
} else {
126+
list($_, $name, $required, $description) = $content;
127+
$description = trim($description);
128+
if ($description == 'required' && empty(trim($required))) {
129+
$required = $description;
130+
$description = '';
131+
}
132+
$required = trim($required) == 'required' ? true : false;
133+
}
134+
135+
return [$name => compact('description', 'required')];
136+
})->toArray();
137+
138+
return $parameters;
139+
}
140+
106141
/**
107142
* @param array $tags
108143
*

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
@@ -46,6 +46,15 @@ public function withBodyParameters()
4646
return '';
4747
}
4848

49+
/**
50+
* @queryParam location_id required The id of the location.
51+
* @queryParam filters The filters.
52+
*/
53+
public function withQueryParameters()
54+
{
55+
return '';
56+
}
57+
4958
/**
5059
* @authenticated
5160
*/

tests/Unit/GeneratorTestCase.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function can_parse_endpoint_description()
4444
public function 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' => [
@@ -77,7 +77,25 @@ public function can_parse_body_parameters()
7777
'required' => false,
7878
'description' => '',
7979
],
80-
], $parameters);
80+
], $bodyParameters);
81+
}
82+
83+
/** @test */
84+
public function can_parse_query_parameters()
85+
{
86+
$route = $this->createRoute('GET', '/api/test', 'withQueryParameters');
87+
$queryParameters = $this->generator->processRoute($route)['queryParameters'];
88+
89+
$this->assertArraySubset([
90+
'location_id' => [
91+
'required' => true,
92+
'description' => 'The id of the location.',
93+
],
94+
'filters' => [
95+
'required' => false,
96+
'description' => 'The filters.',
97+
],
98+
], $queryParameters);
8199
}
82100

83101
/** @test */

0 commit comments

Comments
 (0)