Browse Source

Fix for setting examples of object[]

shalvah 4 years ago
parent
commit
2146219f30
2 changed files with 13 additions and 18 deletions
  1. 11 8
      src/Extracting/Generator.php
  2. 2 10
      src/Extracting/ParamHelpers.php

+ 11 - 8
src/Extracting/Generator.php

@@ -109,6 +109,7 @@ class Generator
         $bodyParameters = $this->fetchBodyParameters($controller, $method, $route, $routeRules, $parsedRoute);
         $parsedRoute['bodyParameters'] = $bodyParameters;
         $parsedRoute['cleanBodyParameters'] = self::cleanParams($bodyParameters);
+
         if (count($parsedRoute['cleanBodyParameters']) && !isset($parsedRoute['headers']['Content-Type'])) {
             // Set content type if the user forgot to set it
             $parsedRoute['headers']['Content-Type'] = 'application/json';
@@ -285,7 +286,7 @@ class Generator
             }
 
             if (Str::contains($paramName, '.')) { // Object field (or array of objects)
-                self::setObject($cleanParameters, $paramName, $details['value'], $parameters);
+                self::setObject($cleanParameters, $paramName, $details['value'], $parameters, ($details['required'] ?? false));
             } else {
                 $cleanParameters[$paramName] = $details['value'];
             }
@@ -294,7 +295,7 @@ class Generator
         return $cleanParameters;
     }
 
-    public static function setObject(array &$results, string $path, $value, array $source)
+    public static function setObject(array &$results, string $path, $value, array $source, bool $isRequired)
     {
         $parts = array_reverse(explode('.', $path));
 
@@ -302,24 +303,26 @@ class Generator
 
         $baseName = join('.', array_reverse($parts));
         // The type should be indicated in the source object by now; we don't need it in the name
-        $normalisedBaseName = str_replace('[]', '', $baseName);
+        $normalisedBaseName = Str::replaceLast('[]', '', $baseName);
 
         $parentData = Arr::get($source, $normalisedBaseName);
         if ($parentData) {
             // Path we use for data_set
             $dotPath = str_replace('[]', '.0', $path);
-            $noValue = new \stdClass();
             if ($parentData['type'] === 'object') {
-                if (Arr::get($results, $dotPath, $noValue) === $noValue) {
+                if (!Arr::has($results, $dotPath)) {
                     Arr::set($results, $dotPath, $value);
                 }
             } else if ($parentData['type'] === 'object[]') {
-                if (Arr::get($results, $dotPath, $noValue) === $noValue) {
+                if (!Arr::has($results, $dotPath)) {
                     Arr::set($results, $dotPath, $value);
                 }
                 // If there's a second item in the array, set for that too.
-                if ($value !== null && Arr::get($results, str_replace('[]', '.1', $baseName), $noValue) !== $noValue) {
-                    Arr::set($results, str_replace('.0', '.1', $dotPath), $value);
+                if ($value !== null && Arr::has($results, Str::replaceLast('[]', '.1', $baseName))) {
+                    // If value is optional, toss a coin on whether to set or not
+                    if ($isRequired || array_rand([true, false], 1)) {
+                        Arr::set($results, Str::replaceLast('.0', '.1', $dotPath), $value);
+                    }
                 }
             }
         }

+ 2 - 10
src/Extracting/ParamHelpers.php

@@ -48,9 +48,6 @@ trait ParamHelpers
             'string' => function () use ($faker) {
                 return $faker->word;
             },
-            'array' => function () {
-                return [];
-            },
             'object' => function () {
                 return [];
             },
@@ -75,8 +72,6 @@ trait ParamHelpers
             'boolean',
             'bool',
             'string',
-            'list', // todo remove this
-            'array', // todo remove this
             'object',
         ];
         return in_array(str_replace('[]', '', $type), $types);
@@ -103,11 +98,8 @@ trait ParamHelpers
             }, $value) : json_decode($value);
         }
 
-        if ($type === 'array' && is_string($value)) {
-            $value = trim($value);
-            if ($value[0] == '[' && $value[strlen($value) - 1] == ']') {
-                return json_decode($value, true);
-            }
+        if ($type === 'object') {
+            return is_array($value) ? $value : json_decode($value, true);
         }
 
         $casts = [