Browse Source

Fix query param rendering for Bash

shalvah 5 years ago
parent
commit
a16d9bef31
2 changed files with 25 additions and 11 deletions
  1. 1 7
      resources/views/partials/example-requests/bash.blade.php
  2. 24 4
      src/Tools/Utils.php

+ 1 - 7
resources/views/partials/example-requests/bash.blade.php

@@ -1,12 +1,6 @@
 ```bash
 curl -X {{$route['methods'][0]}} \
-    {{$route['methods'][0] == 'GET' ? '-G ' : ''}}"{{ rtrim($baseUrl, '/')}}/{{ ltrim($route['boundUri'], '/') }}@if(count($route['cleanQueryParameters']))?@foreach($route['cleanQueryParameters'] as $parameter => $value)
-@if (is_array($value))
-{{ urlencode($parameter) }}[]={{ urlencode(array_keys($value)[0]) }}@if(! $loop->last)&@endif
-@else
-{{ urlencode($parameter) }}={{ urlencode($value) }}@if(!$loop->last)&@endif
-@endif
-@endforeach
+    {{$route['methods'][0] == 'GET' ? '-G ' : ''}}"{{ rtrim($baseUrl, '/')}}/{{ ltrim($route['boundUri'], '/') }}@if(count($route['cleanQueryParameters']))?{!! \Mpociot\ApiDoc\Tools\Utils::printQueryParamsAsString($route['cleanQueryParameters']) !!}
 @endif" @if(count($route['headers']))\
 @foreach($route['headers'] as $header => $value)
     -H "{{$header}}: {{ addslashes($value) }}"@if(! ($loop->last) || ($loop->last && count($route['bodyParameters']))) \

+ 24 - 4
src/Tools/Utils.php

@@ -54,7 +54,7 @@ class Utils
     public static function replaceUrlParameterPlaceholdersWithValues(string $uri, array $urlParameters)
     {
         $matches = preg_match_all('/{.+?}/i', $uri, $parameterPaths);
-        if (! $matches) {
+        if (!$matches) {
             return $uri;
         }
 
@@ -89,7 +89,7 @@ class Utils
 
     public static function deleteDirectoryAndContents($dir)
     {
-        $adapter = new Local(realpath(__DIR__.'/../../'));
+        $adapter = new Local(realpath(__DIR__ . '/../../'));
         $fs = new Filesystem($adapter);
         $fs->deleteDir($dir);
     }
@@ -98,9 +98,9 @@ class Utils
      * @param mixed $value
      * @param int $indentationLevel
      *
+     * @return string
      * @throws \Symfony\Component\VarExporter\Exception\ExceptionInterface
      *
-     * @return string
      */
     public static function printPhpValue($value, $indentationLevel = 0)
     {
@@ -110,9 +110,29 @@ class Utils
         $result = '';
         $padWith = str_repeat(' ', $indentationLevel);
         foreach ($split as $index => $line) {
-            $result .= ($index == 0 ? '' : "\n$padWith").$line;
+            $result .= ($index == 0 ? '' : "\n$padWith") . $line;
         }
 
         return $result;
     }
+
+    public static function printQueryParamsAsString(array $cleanQueryParams)
+    {
+        $qs = "";
+        foreach ($cleanQueryParams as $parameter => $value) {
+            $paramName = urlencode($parameter);
+
+            if (!is_array($value)) {
+                $qs .= "$paramName=" . urlencode($value)."&";
+            } else {
+                // Query param is array - only one level of nesting supported
+                foreach ($value as $item => $itemValue) {
+                    $qs .= "$paramName" . "[" . urlencode($item) . "]=" . urlencode($itemValue)."&";
+                }
+
+            }
+        }
+
+        return rtrim($qs, '&');
+    }
 }