Browse Source

Support nested array/object types better when generating examples and normalising

shalvah 4 years ago
parent
commit
6f229f71d4

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

@@ -43,7 +43,7 @@
 @foreach($route['urlParameters'] as $attribute => $parameter)
 @foreach($route['urlParameters'] as $attribute => $parameter)
 @component('scribe::components.field-details', [
 @component('scribe::components.field-details', [
   'name' => $attribute,
   'name' => $attribute,
-  'type' => null,
+  'type' => $parameter['type'] ?? 'string',
   'required' => $parameter['required'] ?? true,
   'required' => $parameter['required'] ?? true,
   'description' => $parameter['description'],
   'description' => $parameter['description'],
 ])
 ])
@@ -55,7 +55,7 @@
 @foreach($route['queryParameters'] as $attribute => $parameter)
 @foreach($route['queryParameters'] as $attribute => $parameter)
 @component('scribe::components.field-details', [
 @component('scribe::components.field-details', [
   'name' => $attribute,
   'name' => $attribute,
-  'type' => null,
+  'type' => $parameter['type'] ?? 'string',
   'required' => $parameter['required'] ?? true,
   'required' => $parameter['required'] ?? true,
   'description' => $parameter['description'],
   'description' => $parameter['description'],
 ])
 ])
@@ -67,7 +67,7 @@
 @foreach($route['bodyParameters'] as $attribute => $parameter)
 @foreach($route['bodyParameters'] as $attribute => $parameter)
 @component('scribe::components.field-details', [
 @component('scribe::components.field-details', [
   'name' => $attribute,
   'name' => $attribute,
-  'type' => $parameter['type'] ?? null,
+  'type' => $parameter['type'] ?? 'string',
   'required' => $parameter['required'] ?? true,
   'required' => $parameter['required'] ?? true,
   'description' => $parameter['description'],
   'description' => $parameter['description'],
 ])
 ])

+ 20 - 17
src/Extracting/ParamHelpers.php

@@ -28,6 +28,11 @@ trait ParamHelpers
             $isListType = true;
             $isListType = true;
         }
         }
 
 
+        if ($isListType) {
+            // Return a two-array item for a list
+            return [$this->generateDummyValue($baseType), $this->generateDummyValue($baseType)];
+        }
+
         $faker = $this->getFaker();
         $faker = $this->getFaker();
 
 
         $fakeFactories = [
         $fakeFactories = [
@@ -55,10 +60,8 @@ trait ParamHelpers
         ];
         ];
 
 
         $fakeFactory = $fakeFactories[$this->normalizeParameterType($baseType)] ?? $fakeFactories['string'];
         $fakeFactory = $fakeFactories[$this->normalizeParameterType($baseType)] ?? $fakeFactories['string'];
-        $value = $fakeFactory();
 
 
-        // Return a two-array item for a list
-        return $isListType ? [$value, $this->generateDummyValue($baseType)] : $value;
+        return $fakeFactory();
     }
     }
 
 
     protected function isSupportedTypeInDocBlocks(string $type)
     protected function isSupportedTypeInDocBlocks(string $type)
@@ -139,24 +142,24 @@ trait ParamHelpers
      *
      *
      * @return string
      * @return string
      */
      */
-    protected function normalizeParameterType(string $type): string
+    protected function normalizeParameterType(?string $typeName): string
     {
     {
-        if (!$type) {
+        if (!$typeName) {
             return 'string';
             return 'string';
         }
         }
 
 
-        $typeMap = [
-            'int' => 'integer',
-            'int[]' => 'integer[]',
-            'bool' => 'boolean',
-            'bool[]' => 'boolean[]',
-            'double' => 'number',
-            'double[]' => 'number[]',
-            'float' => 'number',
-            'float[]' => 'number[]',
-        ];
-
-        return $typeMap[$type] ?? $type;
+        $base = preg_replace('/\[]/g', '', strtolower($typeName));
+        switch ($base) {
+            case 'int':
+                return preg_replace($base, 'integer', $typeName);
+            case 'float':
+            case 'double':
+                return preg_replace($base, 'number', $typeName);
+            case 'bool':
+                return preg_replace($base, 'boolean', $typeName);
+            default:
+                return $typeName;
+        }
     }
     }
 
 
     /**
     /**

+ 2 - 2
src/Extracting/Strategies/BodyParameters/GetFromBodyParamTag.php

@@ -71,8 +71,8 @@ class GetFromBodyParamTag extends Strategy
                 // Examples:
                 // Examples:
                 // @bodyParam text string required The text.
                 // @bodyParam text string required The text.
                 // @bodyParam user_id integer The ID of the user.
                 // @bodyParam user_id integer The ID of the user.
-                preg_match('/(.+?)\s+(.+?)\s+(required\s+)?([\s\S]*)/', $tag->getContent(), $content);
-                $content = preg_replace('/\s?No-example.?/', '', $content);
+                preg_match('/(.+?)\s+(.+?)\s+(required\s+)?([\s\S]*)/', trim($tag->getContent()), $content);
+                $content = preg_replace('/\s+No-example.?/', '', $content);
                 if (empty($content)) {
                 if (empty($content)) {
                     // this means only name and type were supplied
                     // this means only name and type were supplied
                     [$name, $type] = preg_split('/\s+/', $tag->getContent());
                     [$name, $type] = preg_split('/\s+/', $tag->getContent());

+ 1 - 3
src/Tools/ConsoleOutputUtils.php

@@ -31,9 +31,7 @@ class ConsoleOutputUtils
         if ($should) {
         if ($should) {
             $message .= "\nYou should $should instead.";
             $message .= "\nYou should $should instead.";
         }
         }
-        if ($link) {
-            $message .= " See $link for details";
-        }
+        $message .= $link ? " See $link for details" : " See the changelog for details";
 
 
         self::$clara->warn($message);
         self::$clara->warn($message);
     }
     }