Browse Source

Remove apply.response_calls.headers in favour of apply.headers

shalvah 5 years ago
parent
commit
6d1834324b

+ 2 - 9
config/apidoc.php

@@ -103,6 +103,8 @@ return [
                  * Specify headers to be added to the example requests
                  */
                 'headers' => [
+                    'Content-Type' => 'application/json',
+                    'Accept' => 'application/json',
                     // 'Authorization' => 'Bearer {token}',
                     // 'Api-Version' => 'v2',
                 ],
@@ -131,15 +133,6 @@ return [
                         // 'service.key' => 'value',
                     ],
 
-                    /*
-                     * Headers which should be sent with the API call.
-                     */
-                    'headers' => [
-                        'Content-Type' => 'application/json',
-                        'Accept' => 'application/json',
-                        // 'key' => 'value',
-                    ],
-
                     /*
                      * Cookies which should be sent with the API call.
                      */

+ 2 - 1
src/Commands/GenerateDocumentation.php

@@ -113,6 +113,7 @@ class GenerateDocumentation extends Command
         $parsedRouteOutput = $parsedRoutes->map(function ($routeGroup) use ($settings) {
             return $routeGroup->map(function ($route) use ($settings) {
                 if (count($route['cleanBodyParameters']) && ! isset($route['headers']['Content-Type'])) {
+                    // Set content type if the user forgot to set it
                     $route['headers']['Content-Type'] = 'application/json';
                 }
                 $route['output'] = (string) view('apidoc::partials.route')
@@ -230,7 +231,7 @@ class GenerateDocumentation extends Command
             $route = $routeItem['route'];
             /** @var Route $route */
             if ($this->isValidRoute($route) && $this->isRouteVisibleForDocumentation($route->getAction())) {
-                $parsedRoutes[] = $generator->processRoute($route, $routeItem['apply']);
+                $parsedRoutes[] = $generator->processRoute($route, $routeItem['apply'] ?? []);
                 $this->info('Processed route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route));
             } else {
                 $this->warn('Skipping route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route));

+ 5 - 4
src/Strategies/Responses/ResponseCalls.php

@@ -41,7 +41,7 @@ class ResponseCalls extends Strategy
         $bodyParameters = array_merge($context['cleanBodyParameters'], $rulesToApply['body'] ?? []);
         $queryParameters = array_merge($context['cleanQueryParameters'], $rulesToApply['query'] ?? []);
         $urlParameters = $context['cleanUrlParameters'];
-        $request = $this->prepareRequest($route, $rulesToApply, $urlParameters, $bodyParameters, $queryParameters);
+        $request = $this->prepareRequest($route, $rulesToApply, $urlParameters, $bodyParameters, $queryParameters, $routeRules['headers'] ?? []);
 
         try {
             $response = $this->makeApiCall($request);
@@ -81,14 +81,15 @@ class ResponseCalls extends Strategy
      *
      * @return Request
      */
-    protected function prepareRequest(Route $route, array $rulesToApply, array $urlParams, array $bodyParams, array $queryParams)
+    protected function prepareRequest(Route $route, array $rulesToApply, array $urlParams, array $bodyParams, array $queryParams, array $headers)
     {
         $uri = Utils::getFullUrl($route, $urlParams);
         $routeMethods = $this->getMethods($route);
         $method = array_shift($routeMethods);
         $cookies = isset($rulesToApply['cookies']) ? $rulesToApply['cookies'] : [];
-        $request = Request::create($uri, $method, [], $cookies, [], $this->transformHeadersToServerVars($rulesToApply['headers'] ?? []));
-        $request = $this->addHeaders($request, $route, $rulesToApply['headers'] ?? []);
+        $request = Request::create($uri, $method, [], $cookies, [], $this->transformHeadersToServerVars($headers));
+        // Doing it again to catch any ones we didn't transform properly.
+        $request = $this->addHeaders($request, $route, $headers);
 
         $request = $this->addQueryParameters($request, $queryParams);
         $request = $this->addBodyParameters($request, $bodyParams);

+ 8 - 8
src/Tools/Generator.php

@@ -43,11 +43,11 @@ class Generator
 
     /**
      * @param \Illuminate\Routing\Route $route
-     * @param array $rulesToApply Rules to apply when generating documentation for this route
+     * @param array $routeRules Rules to apply when generating documentation for this route
      *
      * @return array
      */
-    public function processRoute(Route $route, array $rulesToApply = [])
+    public function processRoute(Route $route, array $routeRules = [])
     {
         list($controllerName, $methodName) = Utils::getRouteClassAndMethodNames($route->getAction());
         $controller = new ReflectionClass($controllerName);
@@ -58,27 +58,27 @@ class Generator
             'methods' => $this->getMethods($route),
             'uri' => $this->getUri($route),
         ];
-        $metadata = $this->fetchMetadata($controller, $method, $route, $rulesToApply, $parsedRoute);
+        $metadata = $this->fetchMetadata($controller, $method, $route, $routeRules, $parsedRoute);
         $parsedRoute['metadata'] = $metadata;
 
-        $urlParameters = $this->fetchUrlParameters($controller, $method, $route, $rulesToApply, $parsedRoute);
+        $urlParameters = $this->fetchUrlParameters($controller, $method, $route, $routeRules, $parsedRoute);
         $parsedRoute['urlParameters'] = $urlParameters;
         $parsedRoute['cleanUrlParameters'] = $this->cleanParams($urlParameters);
         $parsedRoute['boundUri'] = Utils::getFullUrl($route, $parsedRoute['cleanUrlParameters']);
 
-        $queryParameters = $this->fetchQueryParameters($controller, $method, $route, $rulesToApply, $parsedRoute);
+        $queryParameters = $this->fetchQueryParameters($controller, $method, $route, $routeRules, $parsedRoute);
         $parsedRoute['queryParameters'] = $queryParameters;
         $parsedRoute['cleanQueryParameters'] = $this->cleanParams($queryParameters);
 
-        $bodyParameters = $this->fetchBodyParameters($controller, $method, $route, $rulesToApply, $parsedRoute);
+        $bodyParameters = $this->fetchBodyParameters($controller, $method, $route, $routeRules, $parsedRoute);
         $parsedRoute['bodyParameters'] = $bodyParameters;
         $parsedRoute['cleanBodyParameters'] = $this->cleanParams($bodyParameters);
 
-        $responses = $this->fetchResponses($controller, $method, $route, $rulesToApply, $parsedRoute);
+        $responses = $this->fetchResponses($controller, $method, $route, $routeRules, $parsedRoute);
         $parsedRoute['response'] = $responses;
         $parsedRoute['showresponse'] = ! empty($responses);
 
-        $parsedRoute['headers'] = $rulesToApply['headers'] ?? [];
+        $parsedRoute['headers'] = $routeRules['headers'] ?? [];
 
         $parsedRoute += $metadata;
 

+ 6 - 2
tests/Fixtures/TestController.php

@@ -153,7 +153,9 @@ class TestController extends Controller
      */
     public function withEloquentApiResourceCollection()
     {
-        return TestUserApiResource::collection(factory(TestUser::class)->make(['id' => 0]));
+        return TestUserApiResource::collection(
+            collect([factory(TestUser::class)->make(['id' => 0])])
+        );
     }
 
     /**
@@ -164,7 +166,9 @@ class TestController extends Controller
      */
     public function withEloquentApiResourceCollectionClass()
     {
-        return new TestUserApiResourceCollection(factory(TestUser::class)->make(['id' => 0]));
+        return new TestUserApiResourceCollection(
+            collect([factory(TestUser::class)->make(['id' => 0])])
+        );
     }
 
     public function checkCustomHeaders(Request $request)

+ 36 - 0
tests/Fixtures/collection.json

@@ -28,6 +28,10 @@
                             {
                                 "key": "Accept",
                                 "value": "application\/json"
+                            },
+                            {
+                                "key": "Content-Type",
+                                "value": "application\/json"
                             }
                         ],
                         "body": {
@@ -55,6 +59,10 @@
                             {
                                 "key": "Accept",
                                 "value": "application\/json"
+                            },
+                            {
+                                "key": "Content-Type",
+                                "value": "application\/json"
                             }
                         ],
                         "body": {
@@ -82,6 +90,10 @@
                             {
                                 "key": "Accept",
                                 "value": "application\/json"
+                            },
+                            {
+                                "key": "Content-Type",
+                                "value": "application\/json"
                             }
                         ],
                         "body": {
@@ -194,6 +206,10 @@
                             {
                                 "key": "Accept",
                                 "value": "application\/json"
+                            },
+                            {
+                                "key": "Content-Type",
+                                "value": "application\/json"
                             }
                         ],
                         "body": {
@@ -221,6 +237,10 @@
                             {
                                 "key": "Accept",
                                 "value": "application\/json"
+                            },
+                            {
+                                "key": "Content-Type",
+                                "value": "application\/json"
                             }
                         ],
                         "body": {
@@ -248,6 +268,10 @@
                             {
                                 "key": "Accept",
                                 "value": "application\/json"
+                            },
+                            {
+                                "key": "Content-Type",
+                                "value": "application\/json"
                             }
                         ],
                         "body": {
@@ -275,6 +299,10 @@
                             {
                                 "key": "Accept",
                                 "value": "application\/json"
+                            },
+                            {
+                                "key": "Content-Type",
+                                "value": "application\/json"
                             }
                         ],
                         "body": {
@@ -308,6 +336,10 @@
                             {
                                 "key": "Accept",
                                 "value": "application\/json"
+                            },
+                            {
+                                "key": "Content-Type",
+                                "value": "application\/json"
                             }
                         ],
                         "body": {
@@ -335,6 +367,10 @@
                             {
                                 "key": "Accept",
                                 "value": "application\/json"
+                            },
+                            {
+                                "key": "Content-Type",
+                                "value": "application\/json"
                             }
                         ],
                         "body": {

+ 11 - 3
tests/Fixtures/collection_updated_url.json

@@ -17,9 +17,13 @@
                         "url": "http:\/\/yourapp.app\/api\/test",
                         "method": "GET",
                         "header": [
+                            {
+                                "key": "Content-Type",
+                                "value": "application\/json"
+                            },
                             {
                                 "key": "Accept",
-                                "value": "application/json"
+                                "value": "application\/json"
                             }
                         ],
                         "body": {
@@ -36,9 +40,13 @@
                         "url": "http:\/\/yourapp.app\/api\/responseTag",
                         "method": "POST",
                         "header": [
+                            {
+                                "key": "Content-Type",
+                                "value": "application\/json"
+                            },
                             {
                                 "key": "Accept",
-                                "value": "application/json"
+                                "value": "application\/json"
                             }
                         ],
                         "body": {
@@ -52,4 +60,4 @@
             ]
         }
     ]
-}
+}

+ 4 - 0
tests/Fixtures/collection_with_body_parameters.json

@@ -17,6 +17,10 @@
                         "url": "http:\/\/localhost\/api\/withBodyParameters",
                         "method": "GET",
                         "header": [
+                            {
+                                "key": "Content-Type",
+                                "value": "application\/json"
+                            },
                             {
                                 "key": "Accept",
                                 "value": "application\/json"

+ 7 - 3
tests/Fixtures/collection_with_query_parameters.json

@@ -12,14 +12,18 @@
             "description": "",
             "item": [
                 {
-                    "name": "http://localhost/api/withQueryParameters",
+                    "name": "http:\/\/localhost\/api\/withQueryParameters",
                     "request": {
                         "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": [
+                            {
+                                "key": "Content-Type",
+                                "value": "application\/json"
+                            },
                             {
                                 "key": "Accept",
-                                "value": "application/json"
+                                "value": "application\/json"
                             }
                         ],
                         "body": {
@@ -33,4 +37,4 @@
             ]
         }
     ]
-}
+}

+ 11 - 3
tests/Fixtures/collection_with_secure_url.json

@@ -17,9 +17,13 @@
                         "url": "https:\/\/yourapp.app\/api\/test",
                         "method": "GET",
                         "header": [
+                            {
+                                "key": "Content-Type",
+                                "value": "application\/json"
+                            },
                             {
                                 "key": "Accept",
-                                "value": "application/json"
+                                "value": "application\/json"
                             }
                         ],
                         "body": {
@@ -36,9 +40,13 @@
                         "url": "https:\/\/yourapp.app\/api\/responseTag",
                         "method": "POST",
                         "header": [
+                            {
+                                "key": "Content-Type",
+                                "value": "application\/json"
+                            },
                             {
                                 "key": "Accept",
-                                "value": "application/json"
+                                "value": "application\/json"
                             }
                         ],
                         "body": {
@@ -52,4 +60,4 @@
             ]
         }
     ]
-}
+}

+ 34 - 29
tests/Fixtures/index.md

@@ -35,7 +35,9 @@ It can also be multiple lines long.
 curl -X GET \
     -G "http://localhost/api/withDescription" \
     -H "Authorization: customAuthToken" \
-    -H "Custom-Header: NotSoCustom"
+    -H "Custom-Header: NotSoCustom" \
+    -H "Content-Type: application/json" \
+    -H "Accept: application/json"
 ```
 
 ```javascript
@@ -46,8 +48,8 @@ const url = new URL(
 let headers = {
     "Authorization": "customAuthToken",
     "Custom-Header": "NotSoCustom",
-    "Accept": "application/json",
     "Content-Type": "application/json",
+    "Accept": "application/json",
 };
 
 fetch(url, {
@@ -79,7 +81,9 @@ null
 curl -X GET \
     -G "http://localhost/api/withResponseTag" \
     -H "Authorization: customAuthToken" \
-    -H "Custom-Header: NotSoCustom"
+    -H "Custom-Header: NotSoCustom" \
+    -H "Content-Type: application/json" \
+    -H "Accept: application/json"
 ```
 
 ```javascript
@@ -90,8 +94,8 @@ const url = new URL(
 let headers = {
     "Authorization": "customAuthToken",
     "Custom-Header": "NotSoCustom",
-    "Accept": "application/json",
     "Content-Type": "application/json",
+    "Accept": "application/json",
 };
 
 fetch(url, {
@@ -133,6 +137,7 @@ curl -X GET \
     -H "Authorization: customAuthToken" \
     -H "Custom-Header: NotSoCustom" \
     -H "Content-Type: application/json" \
+    -H "Accept: application/json" \
     -d '{"user_id":9,"room_id":"consequatur","forever":false,"another_one":11613.31890586,"yet_another_param":{"name":"consequatur"},"even_more_param":[11613.31890586],"book":{"name":"consequatur","author_id":17,"pages_count":17},"ids":[17],"users":[{"first_name":"John","last_name":"Doe"}]}'
 
 ```
@@ -223,7 +228,9 @@ Parameter | Type | Status | Description
 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"
+    -H "Custom-Header: NotSoCustom" \
+    -H "Content-Type: application/json" \
+    -H "Accept: application/json"
 ```
 
 ```javascript
@@ -244,8 +251,8 @@ Object.keys(params)
 let headers = {
     "Authorization": "customAuthToken",
     "Custom-Header": "NotSoCustom",
-    "Accept": "application/json",
     "Content-Type": "application/json",
+    "Accept": "application/json",
 };
 
 fetch(url, {
@@ -287,7 +294,9 @@ Parameter | Status | Description
 curl -X GET \
     -G "http://localhost/api/withAuthTag" \
     -H "Authorization: customAuthToken" \
-    -H "Custom-Header: NotSoCustom"
+    -H "Custom-Header: NotSoCustom" \
+    -H "Content-Type: application/json" \
+    -H "Accept: application/json"
 ```
 
 ```javascript
@@ -298,8 +307,8 @@ const url = new URL(
 let headers = {
     "Authorization": "customAuthToken",
     "Custom-Header": "NotSoCustom",
-    "Accept": "application/json",
     "Content-Type": "application/json",
+    "Accept": "application/json",
 };
 
 fetch(url, {
@@ -331,7 +340,9 @@ null
 curl -X GET \
     -G "http://localhost/api/withEloquentApiResource" \
     -H "Authorization: customAuthToken" \
-    -H "Custom-Header: NotSoCustom"
+    -H "Custom-Header: NotSoCustom" \
+    -H "Content-Type: application/json" \
+    -H "Accept: application/json"
 ```
 
 ```javascript
@@ -342,8 +353,8 @@ const url = new URL(
 let headers = {
     "Authorization": "customAuthToken",
     "Custom-Header": "NotSoCustom",
-    "Accept": "application/json",
     "Content-Type": "application/json",
+    "Accept": "application/json",
 };
 
 fetch(url, {
@@ -381,7 +392,9 @@ fetch(url, {
 curl -X POST \
     "http://localhost/api/withMultipleResponseTagsAndStatusCode" \
     -H "Authorization: customAuthToken" \
-    -H "Custom-Header: NotSoCustom"
+    -H "Custom-Header: NotSoCustom" \
+    -H "Content-Type: application/json" \
+    -H "Accept: application/json"
 ```
 
 ```javascript
@@ -392,8 +405,8 @@ const url = new URL(
 let headers = {
     "Authorization": "customAuthToken",
     "Custom-Header": "NotSoCustom",
-    "Accept": "application/json",
     "Content-Type": "application/json",
+    "Accept": "application/json",
 };
 
 fetch(url, {
@@ -442,7 +455,9 @@ fetch(url, {
 curl -X GET \
     -G "http://localhost/api/withEloquentApiResourceCollectionClass" \
     -H "Authorization: customAuthToken" \
-    -H "Custom-Header: NotSoCustom"
+    -H "Custom-Header: NotSoCustom" \
+    -H "Content-Type: application/json" \
+    -H "Accept: application/json"
 ```
 
 ```javascript
@@ -453,8 +468,8 @@ const url = new URL(
 let headers = {
     "Authorization": "customAuthToken",
     "Custom-Header": "NotSoCustom",
-    "Accept": "application/json",
     "Content-Type": "application/json",
+    "Accept": "application/json",
 };
 
 fetch(url, {
@@ -472,12 +487,7 @@ fetch(url, {
 {
     "data": [
         {
-            "id": 4,
-            "name": "Tested Again",
-            "email": "a@b.com"
-        },
-        {
-            "id": 4,
+            "id": 0,
             "name": "Tested Again",
             "email": "a@b.com"
         }
@@ -487,13 +497,6 @@ fetch(url, {
     }
 }
 ```
-> Example response (500):
-
-```json
-{
-    "message": "Server Error"
-}
-```
 
 ### HTTP Request
 `GET api/withEloquentApiResourceCollectionClass`
@@ -509,7 +512,9 @@ fetch(url, {
 curl -X GET \
     -G "http://localhost/api/echoesUrlParameters/4-consequatur/?something=consequatur" \
     -H "Authorization: customAuthToken" \
-    -H "Custom-Header: NotSoCustom"
+    -H "Custom-Header: NotSoCustom" \
+    -H "Content-Type: application/json" \
+    -H "Accept: application/json"
 ```
 
 ```javascript
@@ -526,8 +531,8 @@ Object.keys(params)
 let headers = {
     "Authorization": "customAuthToken",
     "Custom-Header": "NotSoCustom",
-    "Accept": "application/json",
     "Content-Type": "application/json",
+    "Accept": "application/json",
 };
 
 fetch(url, {

+ 5 - 1
tests/GenerateDocumentationTest.php

@@ -37,7 +37,7 @@ class GenerateDocumentationTest extends TestCase
 
     public function tearDown(): void
     {
-        Utils::deleteDirectoryAndContents('/public/docs');
+  //      Utils::deleteDirectoryAndContents('/public/docs');
     }
 
     /**
@@ -196,6 +196,8 @@ class GenerateDocumentationTest extends TestCase
             'apidoc.routes.0.apply.headers' => [
                 'Authorization' => 'customAuthToken',
                 'Custom-Header' => 'NotSoCustom',
+                'Content-Type' => 'application/json',
+                'Accept' => 'application/json',
             ],
         ]);
         $this->artisan('apidoc:generate');
@@ -249,6 +251,8 @@ class GenerateDocumentationTest extends TestCase
             'apidoc.routes.0.apply.headers' => [
                 'Authorization' => 'customAuthToken',
                 'Custom-Header' => 'NotSoCustom',
+                'Accept' => 'application/json',
+                'Content-Type' => 'application/json',
             ],
         ]);
 

+ 12 - 12
tests/Unit/GeneratorTestCase.php

@@ -647,12 +647,12 @@ abstract class GeneratorTestCase extends TestCase
         $route = $this->createRoute('POST', '/shouldFetchRouteResponse', 'shouldFetchRouteResponse', true);
 
         $rules = [
+            'headers' => [
+                'Content-Type' => 'application/json',
+                'Accept' => 'application/json',
+            ],
             'response_calls' => [
                 'methods' => ['*'],
-                'headers' => [
-                    'Content-Type' => 'application/json',
-                    'Accept' => 'application/json',
-                ],
             ],
         ];
         $parsed = $this->generator->processRoute($route, $rules);
@@ -849,13 +849,13 @@ abstract class GeneratorTestCase extends TestCase
         $route = $this->createRoute('PUT', '/echo/{id}', 'shouldFetchRouteResponseWithEchoedSettings', true);
 
         $rules = [
+            'headers' => [
+                'Content-Type' => 'application/json',
+                'Accept' => 'application/json',
+                'header' => 'value',
+            ],
             'response_calls' => [
                 'methods' => ['*'],
-                'headers' => [
-                    'Content-Type' => 'application/json',
-                    'Accept' => 'application/json',
-                    'header' => 'value',
-                ],
                 'query' => [
                     'queryParam' => 'queryValue',
                 ],
@@ -894,11 +894,11 @@ abstract class GeneratorTestCase extends TestCase
     {
         $route = $this->createRoute('GET', '/api/indexResource', 'index', true, TestResourceController::class);
         $rules = [
+            'headers' => [
+                'Accept' => 'application/json',
+            ],
             'response_calls' => [
                 'methods' => ['*'],
-                'headers' => [
-                    'Accept' => 'application/json',
-                ],
             ],
         ];