Browse Source

Misc fixes

shalvah 4 years ago
parent
commit
4d496008c1

+ 3 - 1
resources/views/index.blade.php

@@ -6,8 +6,10 @@
 
 {!! $introText !!}
 
+@if($isInteractive)
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
 <script>
     var baseUrl = "{{ $baseUrl }}";
 </script>
-<script src="js/tryitout.js"></script>
+<script src="js/tryitout.js"></script>
+@endif

+ 1 - 1
resources/views/partials/endpoint.blade.php

@@ -47,7 +47,7 @@
     <button type="button" style="background-color: #8fbcd4; padding: 5px 10px; border-radius 5px; border-width: thin;" id="btn-tryout-{{ $endpointId }}" onclick="tryItOut('{{ $endpointId }}');">Try it out ⚡</button>
     @endif
     <button type="button" style="background-color: #c97a7e; padding: 5px 10px; border-radius 5px; border-width: thin;" id="btn-canceltryout-{{ $endpointId }}" onclick="cancelTryOut('{{ $endpointId }}');" hidden>Cancel</button>&nbsp;&nbsp;
-    <button type="submit" style="background-color: #6ac174; padding: 5px 10px; border-radius 5px; border-width: thin;" id="btn-executetryout-{{ $endpointId }}" hidden>Execute</button>
+    <button type="submit" style="background-color: #6ac174; padding: 5px 10px; border-radius 5px; border-width: thin;" id="btn-executetryout-{{ $endpointId }}" hidden>Send Request 💥</button>
 </h3>
 @foreach($route['methods'] as $method)
 <p>

+ 1 - 0
src/Extracting/Generator.php

@@ -353,6 +353,7 @@ class Generator
                 $parsedRoute['auth'] = "cleanQueryParameters.$parameterName." . ($valueToUse ?: $token);
                 $parsedRoute['queryParameters'][$parameterName] = [
                     'name' => $parameterName,
+                    'type' => 'string',
                     'value' => $valueToDisplay ?: $token,
                     'description' => 'Authentication key.',
                     'required' => true,

+ 10 - 1
src/Extracting/Strategies/BodyParameters/GetFromFormRequest.php

@@ -256,7 +256,7 @@ class GetFromFormRequest extends Strategy
                 $parameterData['setter'] = function () {
                     return [$this->generateDummyValue('string')];
                 };
-                $parameterData['type'] = $rule;
+                $parameterData['type'] = 'array'; // The cleanup code in normaliseArrayAndObjectParameters() will set this to a valid type (x[] or object)
                 break;
             case 'file':
                 $parameterData['type'] = 'file';
@@ -422,6 +422,15 @@ class GetFromFormRequest extends Strategy
     {
         $results = [];
         foreach ($bodyParametersFromValidationRules as $name => $details) {
+            if (isset($results[$name])) {
+                continue;
+            }
+            if ($details['type'] === 'array') {
+                // Generic array type. If a child item exists,
+                // this will be overwritten with the correct type (such as object or object[]) by the code below
+                $details['type'] = 'string[]';
+            }
+
             // Change cars.*.dogs.things.*.* with type X to cars.*.dogs.things with type X[][]
             while (Str::endsWith($name, '.*')) {
                 $details['type'] .= '[]';

+ 2 - 1
src/Tools/ErrorHandlingUtils.php

@@ -23,7 +23,7 @@ class ErrorHandlingUtils
             $message = $e->getMessage();
             $message = "$exceptionType in $file at line $line: $message";
             ConsoleOutputUtils::warn($message);
-            ConsoleOutputUtils::warn('Run again with --verbose for a full stacktrace');
+            ConsoleOutputUtils::warn('Run this again with the --verbose flag to see the full stack trace.');
         }
 
     }
@@ -35,6 +35,7 @@ class ErrorHandlingUtils
             $handler = new \NunoMaduro\Collision\Handler(new \NunoMaduro\Collision\Writer(null, $output));
         } catch (\Exception $e) {
             // Version 3 used a different API
+            // todo remove when Laravel 7 is minimum supported
             $handler = new \NunoMaduro\Collision\Handler(new \NunoMaduro\Collision\Writer($output));
         }
         $handler->setInspector(new \Whoops\Exception\Inspector($e));

+ 34 - 4
src/Writing/PostmanCollectionWriter.php

@@ -8,7 +8,6 @@ use Illuminate\Support\Str;
 use Knuckles\Scribe\Tools\DocumentationConfig;
 use Ramsey\Uuid\Uuid;
 use ReflectionMethod;
-use Knuckles\Scribe\Tools\ConsoleOutputUtils as c;
 
 class PostmanCollectionWriter
 {
@@ -98,7 +97,7 @@ class PostmanCollectionWriter
 
     protected function generateEndpointItem($endpoint): array
     {
-        return [
+        $endpointItem = [
             'name' => $endpoint['metadata']['title'] !== '' ? $endpoint['metadata']['title'] : $endpoint['uri'],
             'request' => [
                 'url' => $this->generateUrlObject($endpoint),
@@ -106,10 +105,16 @@ class PostmanCollectionWriter
                 'header' => $this->resolveHeadersForEndpoint($endpoint),
                 'body' => empty($endpoint['bodyParameters']) ? null : $this->getBodyData($endpoint),
                 'description' => $endpoint['metadata']['description'] ?? null,
-                'auth' => ($endpoint['metadata']['authenticated'] ?? false) ? null : ['type' => 'noauth'],
             ],
             'response' => [],
         ];
+
+
+        if (($endpoint['metadata']['authenticated'] ?? false) === false) {
+            $endpointItem['request']['auth'] = ['type' => 'noauth'];
+        }
+
+        return $endpointItem;
     }
 
     protected function getBodyData(array $endpoint): array
@@ -155,9 +160,14 @@ class PostmanCollectionWriter
 
     protected function resolveHeadersForEndpoint($route)
     {
+        [$where, $authParam] = $this->getAuthParamToExclude();
+
         $headers = collect($route['headers']);
+        if ($where === 'header') {
+            unset($headers[$authParam]);
+        }
 
-        return $headers
+        $headers = $headers
             ->union([
                 'Accept' => 'application/json',
             ])
@@ -172,6 +182,8 @@ class PostmanCollectionWriter
             })
             ->values()
             ->all();
+
+        return $headers;
     }
 
     protected function generateUrlObject($route)
@@ -192,7 +204,12 @@ class PostmanCollectionWriter
         ];
 
         $query = [];
+        [$where, $authParam] = $this->getAuthParamToExclude();
         foreach ($route['queryParameters'] ?? [] as $name => $parameterData) {
+            if ($where === 'query' && $authParam === $name) {
+                continue;
+            }
+
             if (Str::endsWith($parameterData['type'], '[]')) {
                 $values = empty($parameterData['value']) ? [] : $parameterData['value'];
                 foreach ($values as $index => $value) {
@@ -262,4 +279,17 @@ class PostmanCollectionWriter
             return $baseUrl;
         }
     }
+
+    private function getAuthParamToExclude(): array
+    {
+        if (!$this->config->get('auth.enabled')) {
+            return [null, null];
+        }
+
+        if (in_array($this->config->get('auth.in'), ['bearer', 'basic'])) {
+            return ['header', 'Authorization'];
+        } else {
+            return [$this->config->get('auth.in'), $this->config->get('auth.name')];
+        }
+    }
 }

+ 3 - 2
src/Writing/Writer.php

@@ -122,7 +122,7 @@ class Writer
             'logo' => $this->config->get('logo'),
             'title' => $this->config->get('title', config('app.name', '') . ' API Documentation'),
             'auth' => $this->config->get('auth'),
-            'interactive' => $this->config->get('output.interactive', true)
+            'interactive' => $this->config->get('interactive', true)
         ];
 
         ConsoleOutputUtils::info('Writing source Markdown files to: ' . $this->sourceOutputPath);
@@ -322,7 +322,8 @@ class Writer
         $introMarkdown = view('scribe::index')
             ->with('frontmatter', $frontmatter)
             ->with('introText', $introText)
-            ->with('baseUrl', $this->baseUrl);
+            ->with('baseUrl', $this->baseUrl)
+            ->with('isInteractive', $this->config->get('interactive', true));
         $this->writeFile($indexMarkdownFile, $introMarkdown);
     }