Bläddra i källkod

Merge pull request #627 from mpociot/postman-raw-body

Switch Postman request body data to raw.
Shalvah 5 år sedan
förälder
incheckning
c5a8343ebe

+ 3 - 10
src/Writing/PostmanCollectionWriter.php

@@ -50,7 +50,7 @@ class PostmanCollectionWriter
                     'name' => $groupName,
                     'description' => '',
                     'item' => $routes->map(function ($route) {
-                        $mode = $route['methods'][0] === 'PUT' ? 'urlencoded' : 'formdata';
+                        $mode = 'raw';
 
                         return [
                             'name' => $route['metadata']['title'] != '' ? $route['metadata']['title'] : url($route['uri']),
@@ -74,14 +74,7 @@ class PostmanCollectionWriter
                                     ->values()->all(),
                                 'body' => [
                                     'mode' => $mode,
-                                    $mode => collect($route['bodyParameters'])->map(function ($parameter, $key) {
-                                        return [
-                                            'key' => $key,
-                                            'value' => $parameter['value'] ?? '',
-                                            'type' => 'text',
-                                            'enabled' => true,
-                                        ];
-                                    })->values()->toArray(),
+                                    $mode => json_encode($route['cleanBodyParameters'], JSON_PRETTY_PRINT),
                                 ],
                                 'description' => $route['metadata']['description'],
                                 'response' => [],
@@ -92,6 +85,6 @@ class PostmanCollectionWriter
             })->values()->toArray(),
         ];
 
-        return json_encode($collection);
+        return json_encode($collection, JSON_PRETTY_PRINT);
     }
 }

+ 19 - 104
tests/Fixtures/collection.json

@@ -35,8 +35,8 @@
                             }
                         ],
                         "body": {
-                            "mode": "formdata",
-                            "formdata": []
+                            "mode": "raw",
+                            "raw": "[]"
                         },
                         "description": "This will be the long description.\nIt can also be multiple lines long.",
                         "response": []
@@ -66,8 +66,8 @@
                             }
                         ],
                         "body": {
-                            "mode": "formdata",
-                            "formdata": []
+                            "mode": "raw",
+                            "raw": "[]"
                         },
                         "description": "",
                         "response": []
@@ -77,7 +77,7 @@
                     "name": "Endpoint with body parameters.",
                     "request": {
                         "url": "http:\/\/localhost\/api\/withBodyParameters",
-                        "method": "GET",
+                        "method": "POST",
                         "header": [
                             {
                                 "key": "Authorization",
@@ -97,93 +97,8 @@
                             }
                         ],
                         "body": {
-                            "mode": "formdata",
-                            "formdata": [
-                                {
-                                    "key": "user_id",
-                                    "value": 9,
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "room_id",
-                                    "value": "consequatur",
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "forever",
-                                    "value": false,
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "another_one",
-                                    "value": 11613.31890586,
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "yet_another_param",
-                                    "value": {},
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "yet_another_param.name",
-                                    "value": "consequatur",
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "even_more_param",
-                                    "value": [],
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "even_more_param.*",
-                                    "value": 11613.31890586,
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "book.name",
-                                    "value": "consequatur",
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "book.author_id",
-                                    "value": 17,
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "book[pages_count]",
-                                    "value": 17,
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "ids.*",
-                                    "value": 17,
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "users.*.first_name",
-                                    "value": "John",
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "users.*.last_name",
-                                    "value": "Doe",
-                                    "type": "text",
-                                    "enabled": true
-                                }
-                            ]
+                            "mode": "raw",
+                            "raw": "{\n    \"user_id\": 9,\n    \"room_id\": \"consequatur\",\n    \"forever\": false,\n    \"another_one\": 11613.31890586,\n    \"yet_another_param\": {\n        \"name\": \"consequatur\"\n    },\n    \"even_more_param\": [\n        11613.31890586\n    ],\n    \"book\": {\n        \"name\": \"consequatur\",\n        \"author_id\": 17,\n        \"pages_count\": 17\n    },\n    \"ids\": [\n        17\n    ],\n    \"users\": [\n        {\n            \"first_name\": \"John\",\n            \"last_name\": \"Doe\"\n        }\n    ]\n}"
                         },
                         "description": "",
                         "response": []
@@ -213,8 +128,8 @@
                             }
                         ],
                         "body": {
-                            "mode": "formdata",
-                            "formdata": []
+                            "mode": "raw",
+                            "raw": "[]"
                         },
                         "description": "",
                         "response": []
@@ -244,8 +159,8 @@
                             }
                         ],
                         "body": {
-                            "mode": "formdata",
-                            "formdata": []
+                            "mode": "raw",
+                            "raw": "[]"
                         },
                         "description": "",
                         "response": []
@@ -275,8 +190,8 @@
                             }
                         ],
                         "body": {
-                            "mode": "formdata",
-                            "formdata": []
+                            "mode": "raw",
+                            "raw": "[]"
                         },
                         "description": "",
                         "response": []
@@ -306,8 +221,8 @@
                             }
                         ],
                         "body": {
-                            "mode": "formdata",
-                            "formdata": []
+                            "mode": "raw",
+                            "raw": "[]"
                         },
                         "description": "",
                         "response": []
@@ -343,8 +258,8 @@
                             }
                         ],
                         "body": {
-                            "mode": "formdata",
-                            "formdata": []
+                            "mode": "raw",
+                            "raw": "[]"
                         },
                         "description": "",
                         "response": []
@@ -374,8 +289,8 @@
                             }
                         ],
                         "body": {
-                            "mode": "formdata",
-                            "formdata": []
+                            "mode": "raw",
+                            "raw": "[]"
                         },
                         "description": "",
                         "response": []

+ 4 - 4
tests/Fixtures/collection_updated_url.json → tests/Fixtures/collection_custom_url.json

@@ -27,8 +27,8 @@
                             }
                         ],
                         "body": {
-                            "mode": "formdata",
-                            "formdata": []
+                            "mode": "raw",
+                            "raw": "[]"
                         },
                         "description": "This will be the long description.\nIt can also be multiple lines long.",
                         "response": []
@@ -50,8 +50,8 @@
                             }
                         ],
                         "body": {
-                            "mode": "formdata",
-                            "formdata": []
+                            "mode": "raw",
+                            "raw": "[]"
                         },
                         "description": "",
                         "response": []

+ 2 - 87
tests/Fixtures/collection_with_body_parameters.json

@@ -27,93 +27,8 @@
                             }
                         ],
                         "body": {
-                            "mode": "formdata",
-                            "formdata": [
-                                {
-                                    "key": "user_id",
-                                    "value": 9,
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "room_id",
-                                    "value": "consequatur",
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "forever",
-                                    "value": false,
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "another_one",
-                                    "value": 11613.31890586,
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "yet_another_param",
-                                    "value": {},
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "yet_another_param.name",
-                                    "value": "consequatur",
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "even_more_param",
-                                    "value": [],
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "even_more_param.*",
-                                    "value": 11613.31890586,
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "book.name",
-                                    "value": "consequatur",
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "book.author_id",
-                                    "value": 17,
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "book[pages_count]",
-                                    "value": 17,
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "ids.*",
-                                    "value": 17,
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "users.*.first_name",
-                                    "value": "John",
-                                    "type": "text",
-                                    "enabled": true
-                                },
-                                {
-                                    "key": "users.*.last_name",
-                                    "value": "Doe",
-                                    "type": "text",
-                                    "enabled": true
-                                }
-                            ]
+                            "mode": "raw",
+                            "raw": "{\n    \"user_id\": 9,\n    \"room_id\": \"consequatur\",\n    \"forever\": false,\n    \"another_one\": 11613.31890586,\n    \"yet_another_param\": {\n        \"name\": \"consequatur\"\n    },\n    \"even_more_param\": [\n        11613.31890586\n    ],\n    \"book\": {\n        \"name\": \"consequatur\",\n        \"author_id\": 17,\n        \"pages_count\": 17\n    },\n    \"ids\": [\n        17\n    ],\n    \"users\": [\n        {\n            \"first_name\": \"John\",\n            \"last_name\": \"Doe\"\n        }\n    ]\n}"
                         },
                         "description": "",
                         "response": []

+ 5 - 5
tests/Fixtures/collection_with_custom_headers.json

@@ -12,7 +12,7 @@
             "description": "",
             "item": [
                 {
-                    "name": "http://localhost/api/headers",
+                    "name": "http:\/\/localhost\/api\/headers",
                     "request": {
                         "url": "http:\/\/localhost\/api\/headers",
                         "method": "GET",
@@ -27,12 +27,12 @@
                             },
                             {
                                 "key": "Accept",
-                                "value": "application/json"
+                                "value": "application\/json"
                             }
                         ],
                         "body": {
-                            "mode": "formdata",
-                            "formdata": []
+                            "mode": "raw",
+                            "raw": "[]"
                         },
                         "description": "",
                         "response": []
@@ -41,4 +41,4 @@
             ]
         }
     ]
-}
+}

+ 2 - 2
tests/Fixtures/collection_with_query_parameters.json

@@ -27,8 +27,8 @@
                             }
                         ],
                         "body": {
-                            "mode": "formdata",
-                            "formdata": []
+                            "mode": "raw",
+                            "raw": "[]"
                         },
                         "description": "",
                         "response": []

+ 4 - 4
tests/Fixtures/collection_with_secure_url.json

@@ -27,8 +27,8 @@
                             }
                         ],
                         "body": {
-                            "mode": "formdata",
-                            "formdata": []
+                            "mode": "raw",
+                            "raw": "[]"
                         },
                         "description": "This will be the long description.\nIt can also be multiple lines long.",
                         "response": []
@@ -50,8 +50,8 @@
                             }
                         ],
                         "body": {
-                            "mode": "formdata",
-                            "formdata": []
+                            "mode": "raw",
+                            "raw": "[]"
                         },
                         "description": "",
                         "response": []

+ 8 - 8
tests/GenerateDocumentationTest.php

@@ -235,7 +235,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::post('/api/withBodyParameters', TestController::class.'@withBodyParameters');
         RouteFacade::get('/api/withQueryParameters', TestController::class.'@withQueryParameters');
         RouteFacade::get('/api/withAuthTag', TestController::class.'@withAuthenticatedTag');
         RouteFacade::get('/api/withEloquentApiResource', [TestController::class, 'withEloquentApiResource']);
@@ -261,7 +261,7 @@ class GenerateDocumentationTest extends TestCase
         // The Postman ID varies from call to call; erase it to make the test data reproducible.
         $generatedCollection['info']['_postman_id'] = '';
         $fixtureCollection = json_decode(file_get_contents(__DIR__.'/Fixtures/collection.json'), true);
-        $this->assertEquals($generatedCollection, $fixtureCollection);
+        $this->assertEquals($fixtureCollection, $generatedCollection);
     }
 
     /** @test */
@@ -292,8 +292,8 @@ class GenerateDocumentationTest extends TestCase
         $generatedCollection = json_decode(file_get_contents(__DIR__.'/../public/docs/collection.json'), true);
         // The Postman ID varies from call to call; erase it to make the test data reproducible.
         $generatedCollection['info']['_postman_id'] = '';
-        $fixtureCollection = json_decode(file_get_contents(__DIR__.'/Fixtures/collection_updated_url.json'), true);
-        $this->assertEquals($generatedCollection, $fixtureCollection);
+        $fixtureCollection = json_decode(file_get_contents(__DIR__.'/Fixtures/collection_custom_url.json'), true);
+        $this->assertEquals($fixtureCollection, $generatedCollection);
     }
 
     /** @test */
@@ -310,7 +310,7 @@ class GenerateDocumentationTest extends TestCase
         // The Postman ID varies from call to call; erase it to make the test data reproducible.
         $generatedCollection['info']['_postman_id'] = '';
         $fixtureCollection = json_decode(file_get_contents(__DIR__.'/Fixtures/collection_with_secure_url.json'), true);
-        $this->assertEquals($generatedCollection, $fixtureCollection);
+        $this->assertEquals($fixtureCollection, $generatedCollection);
     }
 
     /** @test */
@@ -330,7 +330,7 @@ class GenerateDocumentationTest extends TestCase
         // The Postman ID varies from call to call; erase it to make the test data reproducible.
         $generatedCollection['info']['_postman_id'] = '';
         $fixtureCollection = json_decode(file_get_contents(__DIR__.'/Fixtures/collection_with_custom_headers.json'), true);
-        $this->assertEquals($generatedCollection, $fixtureCollection);
+        $this->assertEquals($fixtureCollection, $generatedCollection);
     }
 
     /** @test */
@@ -346,7 +346,7 @@ class GenerateDocumentationTest extends TestCase
         // The Postman ID varies from call to call; erase it to make the test data reproducible.
         $generatedCollection['info']['_postman_id'] = '';
         $fixtureCollection = json_decode(file_get_contents(__DIR__.'/Fixtures/collection_with_query_parameters.json'), true);
-        $this->assertEquals($generatedCollection, $fixtureCollection);
+        $this->assertEquals($fixtureCollection, $generatedCollection);
     }
 
     /** @test */
@@ -362,7 +362,7 @@ class GenerateDocumentationTest extends TestCase
         // The Postman ID varies from call to call; erase it to make the test data reproducible.
         $generatedCollection['info']['_postman_id'] = '';
         $fixtureCollection = json_decode(file_get_contents(__DIR__.'/Fixtures/collection_with_body_parameters.json'), true);
-        $this->assertEquals($generatedCollection, $fixtureCollection);
+        $this->assertEquals($fixtureCollection, $generatedCollection);
     }
 
     /** @test */