Sfoglia il codice sorgente

Fix

- Ensure dot-noted parameters are transformed into proper arrays for requests
- Ensure url query are transformed into proper arrays for requests
- Display correct request body parameters for curl and javascript
Ezra Obiwale 6 anni fa
parent
commit
54a4fe5dd5

+ 3 - 8
resources/views/partials/route.blade.php

@@ -18,10 +18,9 @@ curl -X {{$route['methods'][0]}} {{$route['methods'][0] == 'GET' ? '-G ' : ''}}"
 @endif
 @endforeach
 @endif
-@foreach($route['bodyParameters'] as $attribute => $parameter)
-    -d "{{$attribute}}"="{{$parameter['value'] === false ? "false" : $parameter['value']}}" @if(! ($loop->last))\
+@if(count($route['cleanBodyParameters']))
+    -d '{!! json_encode($route['cleanBodyParameters']) !!}'
 @endif
-@endforeach
 
 ```
 
@@ -50,11 +49,7 @@ let headers = {
 }
 @if(count($route['bodyParameters']))
 
-let body = JSON.stringify({
-@foreach($route['bodyParameters'] as $attribute => $parameter)
-    "{{ $attribute }}": "{{ $parameter['value'] }}",
-@endforeach
-})
+let body = {!! json_encode($route['cleanBodyParameters'], JSON_PRETTY_PRINT) !!}
 @endif
 
 fetch(url, {

+ 4 - 0
src/Tools/Generator.php

@@ -8,9 +8,12 @@ use ReflectionMethod;
 use Illuminate\Routing\Route;
 use Mpociot\Reflection\DocBlock;
 use Mpociot\Reflection\DocBlock\Tag;
+use Mpociot\ApiDoc\Tools\Traits\ParamHelpers;
 
 class Generator
 {
+    use ParamHelpers;
+
     /**
      * @param Route $route
      *
@@ -62,6 +65,7 @@ class Generator
             'methods' => $this->getMethods($route),
             'uri' => $this->getUri($route),
             'bodyParameters' => $bodyParameters,
+            'cleanBodyParameters' => $this->cleanParams($bodyParameters),
             'queryParameters' => $queryParameters,
             'authenticated' => $this->getAuthStatusFromDocBlock($docBlock['tags']),
             'response' => $content,

+ 5 - 2
src/Tools/ResponseStrategies/ResponseCallStrategy.php

@@ -6,12 +6,15 @@ use Dingo\Api\Dispatcher;
 use Illuminate\Http\Request;
 use Illuminate\Http\Response;
 use Illuminate\Routing\Route;
+use Mpociot\ApiDoc\Tools\Traits\ParamHelpers;
 
 /**
  * Make a call to the route and retrieve its response.
  */
 class ResponseCallStrategy
 {
+    use ParamHelpers;
+
     /**
      * @param Route $route
      * @param array $tags
@@ -68,8 +71,8 @@ class ResponseCallStrategy
         $request = $this->addHeaders($request, $route, $rulesToApply['headers'] ?? []);
 
         // Mix in parsed parameters with manually specified parameters.
-        $queryParams = collect($queryParams)->map->value->merge($rulesToApply['query'] ?? [])->toArray();
-        $bodyParams = collect($bodyParams)->map->value->merge($rulesToApply['body'] ?? [])->toArray();
+        $queryParams = collect($this->cleanParams($queryParams))->merge($rulesToApply['query'] ?? [])->toArray();
+        $bodyParams = collect($this->cleanParams($bodyParams))->merge($rulesToApply['body'] ?? [])->toArray();
 
         $request = $this->addQueryParameters($request, $queryParams);
         $request = $this->addBodyParameters($request, $bodyParams);

+ 37 - 0
src/Tools/Traits/ParamHelpers.php

@@ -0,0 +1,37 @@
+<?php
+
+namespace Mpociot\ApiDoc\Tools\Traits;
+
+trait ParamHelpers {
+
+    /**
+     * Create proper arrays from dot-noted parameter names
+     *
+     * @param array $params
+     * @return array
+     */
+    protected function cleanParams(array $params)
+    {
+        $values = [];
+        foreach ($params as $name => $description) {
+            $this->cleanValueFrom($name, $description['value'], $values);
+        }
+        return $values;
+    }
+
+    /**
+     * Converts dot notation names to arrays and sets the value at the right depth
+     *
+     * @param string $name
+     * @param mixed $value
+     * @param array $values The array that holds the result
+     * @return void
+     */
+    protected function cleanValueFrom($name, $value, array &$values = [])
+    {
+        if (str_contains($name, '[')) {
+            $name = str_replace(['][', '[', ']', '..'], ['.', '.', '', '.*.'], $name);
+        }
+        array_set($values, str_replace('.*', '.0', $name), $value);
+    }
+}