Explorar el Código

Write array/object query parameters properly in Postman collection

shalvah hace 4 años
padre
commit
524828b583

+ 25 - 10
src/Writing/PostmanCollectionWriter.php

@@ -57,7 +57,7 @@ class PostmanCollectionWriter
                     'key' => 'baseUrl',
                     'type' => 'string',
                     'name' => 'string',
-                    'value' => parse_url($this->baseUrl, PHP_URL_HOST),
+                    'value' => parse_url($this->baseUrl, PHP_URL_HOST) ?: $this->baseUrl, // if there's no protocol, parse_url might fail
                 ],
             ],
             'info' => [
@@ -218,25 +218,40 @@ class PostmanCollectionWriter
             'path' => preg_replace_callback('/\{(\w+)\??}/', function ($matches) {
                 return ':' . $matches[1];
             }, $route['uri']),
-            'query' => collect($route['queryParameters'] ?? [])->map(function ($parameterData, $key) {
-                // TODO remove: unneeded with new syntax
-                $key = rtrim($key, ".*");
-                return [
-                    'key' => $key,
+        ];
+
+        $query = [];
+        foreach ($route['queryParameters'] ?? [] as $name => $parameterData) {
+            if (Str::endsWith($parameterData['type'], '[]')) {
+                $values = empty($parameterData['value']) ? [] : $parameterData['value'];
+                foreach ($values as $index => $value) {
+                    $query[] = [
+                        'key' => "{$name}[$index]",
+                        'value' => urlencode($value),
+                        'description' => strip_tags($parameterData['description']),
+                        // Default query params to disabled if they aren't required and have empty values
+                        'disabled' => !($parameterData['required'] ?? false) && empty($parameterData['value']),
+                    ];
+                }
+            } else {
+                $query[] = [
+                    'key' => $name,
                     'value' => urlencode($parameterData['value']),
                     'description' => strip_tags($parameterData['description']),
                     // Default query params to disabled if they aren't required and have empty values
                     'disabled' => !($parameterData['required'] ?? false) && empty($parameterData['value']),
                 ];
-            })->values()->toArray(),
-        ];
+            }
+        }
+
+        $base['query'] = $query;
 
         // Create raw url-parameter (Insomnia uses this on import)
-        $query = collect($base['query'] ?? [])->map(function ($queryParamData) {
+        $queryString = collect($base['query'] ?? [])->map(function ($queryParamData) {
             return $queryParamData['key'] . '=' . $queryParamData['value'];
         })->implode('&');
         $base['raw'] = sprintf('%s://%s/%s%s',
-            $base['protocol'], $base['host'], $base['path'], $query ? '?' . $query : null
+            $base['protocol'], $base['host'], $base['path'], $queryString ? "?{$queryString}" : null
         );
 
         // If there aren't any url parameters described then return what we've got

+ 21 - 13
tests/Fixtures/collection.json

@@ -1,5 +1,13 @@
 {
-    "variable": [],
+    "variable": [
+        {
+            "id": "baseUrl",
+            "key": "baseUrl",
+            "type": "string",
+            "name": "string",
+            "value": "localhost"
+        }
+    ],
     "info": {
         "name": "GREAT API!",
         "_postman_id": "",
@@ -17,10 +25,10 @@
                     "request": {
                         "url": {
                             "protocol": "http",
-                            "host": "localhost",
+                            "host": "{{baseUrl}}",
                             "path": "api\/withDescription",
                             "query": [],
-                            "raw": "http:\/\/localhost\/api\/withDescription"
+                            "raw": "http:\/\/{{baseUrl}}\/api\/withDescription"
                         },
                         "method": "GET",
                         "header": [
@@ -46,10 +54,10 @@
                     "request": {
                         "url": {
                             "protocol": "http",
-                            "host": "localhost",
+                            "host": "{{baseUrl}}",
                             "path": "api\/withFormDataParams",
                             "query": [],
-                            "raw": "http:\/\/localhost\/api\/withFormDataParams"
+                            "raw": "http:\/\/{{baseUrl}}\/api\/withFormDataParams"
                         },
                         "method": "POST",
                         "header": [
@@ -93,10 +101,10 @@
                     "request": {
                         "url": {
                             "protocol": "http",
-                            "host": "localhost",
+                            "host": "{{baseUrl}}",
                             "path": "api\/withBodyParameters",
                             "query": [],
-                            "raw": "http:\/\/localhost\/api\/withBodyParameters"
+                            "raw": "http:\/\/{{baseUrl}}\/api\/withBodyParameters"
                         },
                         "method": "POST",
                         "header": [
@@ -129,7 +137,7 @@
                     "request": {
                         "url": {
                             "protocol": "http",
-                            "host": "localhost",
+                            "host": "{{baseUrl}}",
                             "path": "api\/withQueryParameters",
                             "query": [
                                 {
@@ -163,7 +171,7 @@
                                     "disabled": false
                                 }
                             ],
-                            "raw": "http:\/\/localhost\/api\/withQueryParameters?location_id=consequatur&user_id=me&page=4&filters=consequatur&url_encoded=%2B+%5B%5D%26%3D"
+                            "raw": "http:\/\/{{baseUrl}}\/api\/withQueryParameters?location_id=consequatur&user_id=me&page=4&filters=consequatur&url_encoded=%2B+%5B%5D%26%3D"
                         },
                         "method": "GET",
                         "header": [
@@ -189,10 +197,10 @@
                     "request": {
                         "url": {
                             "protocol": "http",
-                            "host": "localhost",
+                            "host": "{{baseUrl}}",
                             "path": "api\/withAuthTag",
                             "query": [],
-                            "raw": "http:\/\/localhost\/api\/withAuthTag"
+                            "raw": "http:\/\/{{baseUrl}}\/api\/withAuthTag"
                         },
                         "method": "GET",
                         "header": [
@@ -222,7 +230,7 @@
                     "request": {
                         "url": {
                             "protocol": "http",
-                            "host": "localhost",
+                            "host": "{{baseUrl}}",
                             "path": "api\/echoesUrlParameters\/:param-:param2\/:param3",
                             "query": [
                                 {
@@ -232,7 +240,7 @@
                                     "disabled": false
                                 }
                             ],
-                            "raw": "http:\/\/localhost\/api\/echoesUrlParameters\/:param-:param2\/:param3?something=consequatur",
+                            "raw": "http:\/\/{{baseUrl}}\/api\/echoesUrlParameters\/:param-:param2\/:param3?something=consequatur",
                             "variable": [
                                 {
                                     "id": "param",

+ 18 - 9
tests/Unit/PostmanCollectionWriterTest.php

@@ -15,9 +15,7 @@ class PostmanCollectionWriterTest extends TestCase
     public function testCorrectStructureIsFollowed()
     {
         \Config::set('scribe.title', 'Test API');
-        \Config::set('scribe.postman', [
-            'description' => 'A fake description',
-        ]);
+        \Config::set('scribe.description', 'A fake description');
 
         $writer = new PostmanCollectionWriter();
         $collection = $writer->generatePostmanCollection(new Collection());
@@ -54,7 +52,8 @@ class PostmanCollectionWriterTest extends TestCase
         $item = data_get($collection, 'item.0.item.0');
         $this->assertSame('some/path', $item['name'], 'Name defaults to path');
         $this->assertSame('http', data_get($item, 'request.url.protocol'), 'Protocol defaults to http');
-        $this->assertSame('fake.localhost', data_get($item, 'request.url.host'), 'Host uses what\'s given');
+        $this->assertSame('fake.localhost', data_get($collection, 'variable.0.value'));
+        $this->assertSame('{{baseUrl}}', data_get($item, 'request.url.host'));
         $this->assertSame('some/path', data_get($item, 'request.url.path'), 'Path is set correctly');
         $this->assertEmpty(data_get($item, 'request.url.query'), 'Query parameters are empty');
         $this->assertSame('GET', data_get($item, 'request.method'), 'Method is correctly resolved');
@@ -117,14 +116,16 @@ class PostmanCollectionWriterTest extends TestCase
 
         $fakeRoute['queryParameters'] = [
             'limit' => [
+                'type' => 'integer',
                 'description' => 'A fake limit for my fake endpoint',
                 'required' => true,
                 'value' => 5,
             ],
-            'filters.*' => [
+            'filters' => [
+                'type' => 'integer[]',
                 'description' => 'Filters',
                 'required' => true,
-                'value' => '34,12',
+                'value' => [34, 12],
             ],
         ];
         $fakeRoute['cleanQueryParameters'] = Generator::cleanParams($fakeRoute['queryParameters']);
@@ -136,7 +137,7 @@ class PostmanCollectionWriterTest extends TestCase
 
         $variableData = data_get($collection, 'item.0.item.0.request.url.query');
 
-        $this->assertCount(2, $variableData);
+        $this->assertCount(3, $variableData);
         $this->assertEquals([
             'key' => 'limit',
             'value' => '5',
@@ -144,11 +145,17 @@ class PostmanCollectionWriterTest extends TestCase
             'disabled' => false,
         ], $variableData[0]);
         $this->assertEquals([
-            'key' => 'filters',
-            'value' => urlencode("34,12"),
+            'key' => 'filters[0]',
+            'value' => 34,
             'description' => 'Filters',
             'disabled' => false,
         ], $variableData[1]);
+        $this->assertEquals([
+            'key' => 'filters[1]',
+            'value' => 12,
+            'description' => 'Filters',
+            'disabled' => false,
+        ], $variableData[2]);
     }
 
     public function testUrlParametersAreNotIncludedIfMissingFromPath()
@@ -177,11 +184,13 @@ class PostmanCollectionWriterTest extends TestCase
         $fakeRoute = $this->createMockRouteData('fake/path');
         $fakeRoute['queryParameters'] = [
             'required' => [
+                'type' => 'string',
                 'description' => 'A required param with a null value',
                 'required' => true,
                 'value' => null,
             ],
             'not_required' => [
+                'type' => 'string',
                 'description' => 'A not required param with a null value',
                 'required' => false,
                 'value' => null,