Skip to content

Commit 6bc4b53

Browse files
committed
Add support for query parameters in the bash template and ensure that all query parameters are URL-encoded.
1 parent 98d0456 commit 6bc4b53

File tree

6 files changed

+67
-3
lines changed

6 files changed

+67
-3
lines changed

resources/views/partials/example-requests/bash.blade.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
```bash
2-
curl -X {{$route['methods'][0]}} {{$route['methods'][0] == 'GET' ? '-G ' : ''}}"{{ rtrim($baseUrl, '/')}}/{{ ltrim($route['boundUri'], '/') }}" @if(count($route['headers']))\
2+
curl -X {{$route['methods'][0]}} {{$route['methods'][0] == 'GET' ? '-G ' : ''}}"{{ rtrim($baseUrl, '/')}}/{{ ltrim($route['boundUri'], '/') }}@if(count($route['queryParameters']))?@foreach($route['queryParameters'] as $attribute => $parameter)
3+
{{ urlencode($attribute) }}={{ urlencode($parameter['value']) }}@if(!$loop->last)&@endif
4+
@endforeach
5+
@endif" @if(count($route['headers']))\
36
@foreach($route['headers'] as $header => $value)
47
-H "{{$header}}: {{$value}}"@if(! ($loop->last) || ($loop->last && count($route['bodyParameters']))) \
58
@endif

src/Postman/CollectionWriter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function getCollection()
5959
'url' => url($route['uri']).(collect($route['queryParameters'])->isEmpty()
6060
? ''
6161
: ('?'.implode('&', collect($route['queryParameters'])->map(function ($parameter, $key) {
62-
return $key.'='.($parameter['value'] ?? '');
62+
return urlencode($key).'='.urlencode($parameter['value'] ?? '');
6363
})->all()))),
6464
'method' => $route['methods'][0],
6565
'header' => collect($route['headers'])

tests/Fixtures/TestController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function withNonCommentedFormRequestParameter(TestNonCommentedRequest $re
7575
* @queryParam user_id required The id of the user. Example: me
7676
* @queryParam page required The page number. Example: 4
7777
* @queryParam filters The filters.
78+
* @queryParam url_encoded Used for testing that URL parameters will be URL-encoded where needed. Example: + []&=
7879
*/
7980
public function withQueryParameters()
8081
{

tests/Fixtures/collection_with_query_parameters.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
{
1515
"name": "http://localhost/api/withQueryParameters",
1616
"request": {
17-
"url": "http:\/\/localhost\/api\/withQueryParameters?location_id=consequatur&user_id=me&page=4&filters=consequatur",
17+
"url": "http:\/\/localhost\/api\/withQueryParameters?location_id=consequatur&user_id=me&page=4&filters=consequatur&url_encoded=%2B+%5B%5D%26%3D",
1818
"method": "GET",
1919
"header": [
2020
{

tests/Fixtures/index.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,65 @@ Parameter | Type | Status | Description
197197

198198
<!-- END_a25cb3b490fa579d7d77b386bbb7ec03 -->
199199

200+
<!-- START_5c545aa7f913d84b23ac4cfefc1de659 -->
201+
## api/withQueryParameters
202+
> Example request:
203+
204+
```bash
205+
curl -X GET -G "http://localhost/api/withQueryParameters?location_id=consequatur&user_id=me&page=4&filters=consequatur&url_encoded=%2B+%5B%5D%26%3D" \
206+
-H "Authorization: customAuthToken" \
207+
-H "Custom-Header: NotSoCustom"
208+
```
209+
210+
```javascript
211+
const url = new URL("http://localhost/api/withQueryParameters");
212+
213+
let params = {
214+
"location_id": "consequatur",
215+
"user_id": "me",
216+
"page": "4",
217+
"filters": "consequatur",
218+
"url_encoded": "+ []&amp;=",
219+
};
220+
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]));
221+
222+
let headers = {
223+
"Authorization": "customAuthToken",
224+
"Custom-Header": "NotSoCustom",
225+
"Accept": "application/json",
226+
"Content-Type": "application/json",
227+
}
228+
229+
fetch(url, {
230+
method: "GET",
231+
headers: headers,
232+
})
233+
.then(response => response.json())
234+
.then(json => console.log(json));
235+
```
236+
237+
238+
> Example response (200):
239+
240+
```json
241+
null
242+
```
243+
244+
### HTTP Request
245+
`GET api/withQueryParameters`
246+
247+
#### Query Parameters
248+
249+
Parameter | Status | Description
250+
--------- | ------- | ------- | -----------
251+
location_id | required | The id of the location.
252+
user_id | required | The id of the user.
253+
page | required | The page number.
254+
filters | optional | The filters.
255+
url_encoded | optional | Used for testing that URL parameters will be URL-encoded where needed.
256+
257+
<!-- END_5c545aa7f913d84b23ac4cfefc1de659 -->
258+
200259
<!-- START_5c08cc4d72b6e5830f6814c64086e197 -->
201260
## api/withAuthTag
202261
<br><small style="padding: 1px 9px 2px;font-weight: bold;white-space: nowrap;color: #ffffff;-webkit-border-radius: 9px;-moz-border-radius: 9px;border-radius: 9px;background-color: #3a87ad;">Requires authentication</small>

tests/GenerateDocumentationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ public function generated_markdown_file_is_correct()
191191
RouteFacade::get('/api/withDescription', TestController::class.'@withEndpointDescription');
192192
RouteFacade::get('/api/withResponseTag', TestController::class.'@withResponseTag');
193193
RouteFacade::get('/api/withBodyParameters', TestController::class.'@withBodyParameters');
194+
RouteFacade::get('/api/withQueryParameters', TestController::class.'@withQueryParameters');
194195
RouteFacade::get('/api/withAuthTag', TestController::class.'@withAuthenticatedTag');
195196

196197
// We want to have the same values for params each time

0 commit comments

Comments
 (0)