Skip to content

Commit

Permalink
Merge pull request #13 from ErnestStaug/master
Browse files Browse the repository at this point in the history
form-data support
  • Loading branch information
shalvah authored May 20, 2020
2 parents dd9356f + 0309886 commit a4bf992
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 25 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
},
"config": {
"preferred-install": "dist",
"sort-packages": true
"sort-packages": true,
"process-timeout": 600
},
"replace": {
"mpociot/laravel-apidoc-generator": "*"
Expand Down
54 changes: 46 additions & 8 deletions src/Writing/PostmanCollectionWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,66 @@ public function makePostmanCollection()
return json_encode($collection, JSON_PRETTY_PRINT);
}

protected function generateEndpointItem($route)
protected function generateEndpointItem($route): array
{
$mode = 'raw';

$method = $route['methods'][0];

return [
'name' => $route['metadata']['title'] != '' ? $route['metadata']['title'] : $route['uri'],
'name' => $route['metadata']['title'] !== '' ? $route['metadata']['title'] : $route['uri'],
'request' => [
'url' => $this->makeUrlData($route),
'method' => $method,
'header' => $this->resolveHeadersForRoute($route),
'body' => [
'mode' => $mode,
$mode => json_encode($route['cleanBodyParameters'], JSON_PRETTY_PRINT),
],
'body' => $this->getBodyData($route),
'description' => $route['metadata']['description'] ?? null,
'response' => [],
],
];
}

protected function getBodyData(array $route): array
{

$body = [];
$contentType = $route['headers']['Content-Type'] ?? null;
switch ($contentType) {
case 'multipart/form-data':
$mode = 'formdata';
break;
case 'application/json':
default:
$mode = 'raw';
}
$body['mode'] = $mode;

switch ($mode) {
case 'formdata':
foreach ($route['cleanBodyParameters'] as $key => $value) {
$params = [
'key' => $key,
'value' => $value,
'type' => 'text'
];
$body[$mode][] = $params;
}
foreach ($route['fileParameters'] as $key => $value) {
$params = [
'key' => $key,
'src' => [],
'type' => 'file'
];
$body[$mode][] = $params;
}
break;
case 'raw':
default:
$body[$mode] = json_encode($route['cleanBodyParameters'], JSON_PRETTY_PRINT);
$body['options'][$mode]['language'] = 'json';
}
return $body;
}


protected function resolveHeadersForRoute($route)
{
$headers = collect($route['headers']);
Expand Down
11 changes: 11 additions & 0 deletions tests/Fixtures/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ public function withBodyParameters()
return '';
}

/**
* Endpoint with body form data parameters.
*
* @bodyParam name string required Name of image. Example: cat.jpg
* @bodyParam image file required The image.
*/
public function withFormDataParams()
{
return '';
}

/**
* Endpoint with body parameters as array.
*
Expand Down
63 changes: 54 additions & 9 deletions tests/Fixtures/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@
],
"body": {
"mode": "raw",
"raw": "[]"
"raw": "[]",
"options": {
"raw": {
"language": "json"
}
}
},
"description": "This will be the long description.\nIt can also be multiple lines long.",
"response": []
Expand Down Expand Up @@ -77,7 +82,12 @@
],
"body": {
"mode": "raw",
"raw": "[]"
"raw": "[]",
"options": {
"raw": {
"language": "json"
}
}
},
"description": "",
"response": []
Expand Down Expand Up @@ -113,7 +123,12 @@
],
"body": {
"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}"
"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}",
"options": {
"raw": {
"language": "json"
}
}
},
"description": "",
"response": []
Expand Down Expand Up @@ -180,7 +195,12 @@
],
"body": {
"mode": "raw",
"raw": "[]"
"raw": "[]",
"options": {
"raw": {
"language": "json"
}
}
},
"description": "",
"response": []
Expand Down Expand Up @@ -216,7 +236,12 @@
],
"body": {
"mode": "raw",
"raw": "[]"
"raw": "[]",
"options": {
"raw": {
"language": "json"
}
}
},
"description": "",
"response": []
Expand Down Expand Up @@ -252,7 +277,12 @@
],
"body": {
"mode": "raw",
"raw": "[]"
"raw": "[]",
"options": {
"raw": {
"language": "json"
}
}
},
"description": "",
"response": []
Expand Down Expand Up @@ -288,7 +318,12 @@
],
"body": {
"mode": "raw",
"raw": "[]"
"raw": "[]",
"options": {
"raw": {
"language": "json"
}
}
},
"description": "",
"response": []
Expand Down Expand Up @@ -330,7 +365,12 @@
],
"body": {
"mode": "raw",
"raw": "[]"
"raw": "[]",
"options": {
"raw": {
"language": "json"
}
}
},
"description": "",
"response": []
Expand Down Expand Up @@ -387,7 +427,12 @@
],
"body": {
"mode": "raw",
"raw": "[]"
"raw": "[]",
"options": {
"raw": {
"language": "json"
}
}
},
"description": "",
"response": []
Expand Down
14 changes: 12 additions & 2 deletions tests/Fixtures/collection_custom_url.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@
],
"body": {
"mode": "raw",
"raw": "[]"
"raw": "[]",
"options": {
"raw": {
"language": "json"
}
}
},
"description": "This will be the long description.\nIt can also be multiple lines long.",
"response": []
Expand Down Expand Up @@ -61,7 +66,12 @@
],
"body": {
"mode": "raw",
"raw": "[]"
"raw": "[]",
"options": {
"raw": {
"language": "json"
}
}
},
"description": "",
"response": []
Expand Down
7 changes: 6 additions & 1 deletion tests/Fixtures/collection_with_body_parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@
],
"body": {
"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}"
"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}",
"options": {
"raw": {
"language": "json"
}
}
},
"description": "",
"response": []
Expand Down
7 changes: 6 additions & 1 deletion tests/Fixtures/collection_with_custom_headers.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@
],
"body": {
"mode": "raw",
"raw": "[]"
"raw": "[]",
"options": {
"raw": {
"language": "json"
}
}
},
"description": "",
"response": []
Expand Down
56 changes: 56 additions & 0 deletions tests/Fixtures/collection_with_form_data_parameters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"variables": [],
"info": {
"name": "Laravel API",
"_postman_id": "",
"description": "",
"schema": "https:\/\/schema.getpostman.com\/json\/collection\/v2.0.0\/collection.json"
},
"item": [
{
"name": "Group A",
"description": "",
"item": [
{
"name": "Endpoint with body form data parameters.",
"request": {
"url": {
"protocol": "http",
"host": "localhost",
"path": "api\/withFormDataParams",
"query": []
},
"method": "GET",
"header": [
{
"key": "Content-Type",
"value": "multipart\/form-data"
},
{
"key": "Accept",
"value": "application\/json"
}
],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "name",
"value": "cat.jpg",
"type": "text"
},
{
"key": "image",
"src": [],
"type": "file"
}
]
},
"description": "",
"response": []
}
}
]
}
]
}
7 changes: 6 additions & 1 deletion tests/Fixtures/collection_with_query_parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@
],
"body": {
"mode": "raw",
"raw": "[]"
"raw": "[]",
"options": {
"raw": {
"language": "json"
}
}
},
"description": "",
"response": []
Expand Down
14 changes: 12 additions & 2 deletions tests/Fixtures/collection_with_secure_url.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@
],
"body": {
"mode": "raw",
"raw": "[]"
"raw": "[]",
"options": {
"raw": {
"language": "json"
}
}
},
"description": "This will be the long description.\nIt can also be multiple lines long.",
"response": []
Expand Down Expand Up @@ -61,7 +66,12 @@
],
"body": {
"mode": "raw",
"raw": "[]"
"raw": "[]",
"options": {
"raw": {
"language": "json"
}
}
},
"description": "",
"response": []
Expand Down
Loading

0 comments on commit a4bf992

Please sign in to comment.