فهرست منبع

Add support for query parameters in the bash template and ensure that all query parameters are URL-encoded.

Daniel Alm 5 سال پیش
والد
کامیت
6bc4b53f8c

+ 4 - 1
resources/views/partials/example-requests/bash.blade.php

@@ -1,5 +1,8 @@
 ```bash
-curl -X {{$route['methods'][0]}} {{$route['methods'][0] == 'GET' ? '-G ' : ''}}"{{ rtrim($baseUrl, '/')}}/{{ ltrim($route['boundUri'], '/') }}" @if(count($route['headers']))\
+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)
+{{ urlencode($attribute) }}={{ urlencode($parameter['value']) }}@if(!$loop->last)&@endif
+@endforeach
+@endif" @if(count($route['headers']))\
 @foreach($route['headers'] as $header => $value)
     -H "{{$header}}: {{$value}}"@if(! ($loop->last) || ($loop->last && count($route['bodyParameters']))) \
 @endif

+ 1 - 1
src/Postman/CollectionWriter.php

@@ -59,7 +59,7 @@ class CollectionWriter
                                 'url' => url($route['uri']).(collect($route['queryParameters'])->isEmpty()
                                     ? ''
                                     : ('?'.implode('&', collect($route['queryParameters'])->map(function ($parameter, $key) {
-                                        return $key.'='.($parameter['value'] ?? '');
+                                        return urlencode($key).'='.urlencode($parameter['value'] ?? '');
                                     })->all()))),
                                 'method' => $route['methods'][0],
                                 'header' => collect($route['headers'])

+ 1 - 0
tests/Fixtures/TestController.php

@@ -75,6 +75,7 @@ class TestController extends Controller
      * @queryParam user_id required The id of the user. Example: me
      * @queryParam page required The page number. Example: 4
      * @queryParam filters  The filters.
+     * @queryParam url_encoded  Used for testing that URL parameters will be URL-encoded where needed. Example: + []&=
      */
     public function withQueryParameters()
     {

+ 1 - 1
tests/Fixtures/collection_with_query_parameters.json

@@ -14,7 +14,7 @@
                 {
                     "name": "http://localhost/api/withQueryParameters",
                     "request": {
-                        "url": "http:\/\/localhost\/api\/withQueryParameters?location_id=consequatur&user_id=me&page=4&filters=consequatur",
+                        "url": "http:\/\/localhost\/api\/withQueryParameters?location_id=consequatur&user_id=me&page=4&filters=consequatur&url_encoded=%2B+%5B%5D%26%3D",
                         "method": "GET",
                         "header": [
                             {

+ 59 - 0
tests/Fixtures/index.md

@@ -197,6 +197,65 @@ Parameter | Type | Status | Description
 
 <!-- END_a25cb3b490fa579d7d77b386bbb7ec03 -->
 
+<!-- START_5c545aa7f913d84b23ac4cfefc1de659 -->
+## api/withQueryParameters
+> Example request:
+
+```bash
+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" \
+    -H "Authorization: customAuthToken" \
+    -H "Custom-Header: NotSoCustom"
+```
+
+```javascript
+const url = new URL("http://localhost/api/withQueryParameters");
+
+    let params = {
+            "location_id": "consequatur",
+            "user_id": "me",
+            "page": "4",
+            "filters": "consequatur",
+            "url_encoded": "+ []&amp;=",
+        };
+    Object.keys(params).forEach(key => url.searchParams.append(key, params[key]));
+
+let headers = {
+    "Authorization": "customAuthToken",
+    "Custom-Header": "NotSoCustom",
+    "Accept": "application/json",
+    "Content-Type": "application/json",
+}
+
+fetch(url, {
+    method: "GET",
+    headers: headers,
+})
+    .then(response => response.json())
+    .then(json => console.log(json));
+```
+
+
+> Example response (200):
+
+```json
+null
+```
+
+### HTTP Request
+`GET api/withQueryParameters`
+
+#### Query Parameters
+
+Parameter | Status | Description
+--------- | ------- | ------- | -----------
+    location_id |  required  | The id of the location.
+    user_id |  required  | The id of the user.
+    page |  required  | The page number.
+    filters |  optional  | The filters.
+    url_encoded |  optional  | Used for testing that URL parameters will be URL-encoded where needed.
+
+<!-- END_5c545aa7f913d84b23ac4cfefc1de659 -->
+
 <!-- START_5c08cc4d72b6e5830f6814c64086e197 -->
 ## api/withAuthTag
 <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>

+ 1 - 0
tests/GenerateDocumentationTest.php

@@ -191,6 +191,7 @@ class GenerateDocumentationTest extends TestCase
         RouteFacade::get('/api/withDescription', TestController::class.'@withEndpointDescription');
         RouteFacade::get('/api/withResponseTag', TestController::class.'@withResponseTag');
         RouteFacade::get('/api/withBodyParameters', TestController::class.'@withBodyParameters');
+        RouteFacade::get('/api/withQueryParameters', TestController::class.'@withQueryParameters');
         RouteFacade::get('/api/withAuthTag', TestController::class.'@withAuthenticatedTag');
 
         // We want to have the same values for params each time