浏览代码

Support `where()` constraints in URL routes

shalvah 3 年之前
父节点
当前提交
cf2b53c16d
共有 2 个文件被更改,包括 17 次插入6 次删除
  1. 1 1
      resources/js/tryitout.js
  2. 16 5
      src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php

+ 1 - 1
resources/js/tryitout.js

@@ -191,7 +191,7 @@ async function executeTryOut(endpointId, form) {
     if (body instanceof FormData) {
         delete headers['Content-Type'];
 
-        // When using FormData with PUT or PATCH, send with post and add _method
+        // When using FormData with PUT or PATCH, use method spoofing so PHP can access the post body
         if (['PUT', 'PATCH'].includes(form.dataset.method)) {
             method = 'POST';
             setter('_method', form.dataset.method);

+ 16 - 5
src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php

@@ -27,7 +27,6 @@ class GetFromLaravelAPI extends Strategy
         foreach ($matches[1] as $match) {
             $optional = Str::endsWith($match, '?');
             $name = rtrim($match, '?');
-            $type = 'string';
             $parameters[$name] = [
                 'name' => $name,
                 'description' => $this->inferUrlParamDescription($endpointData->uri, $name),
@@ -44,6 +43,10 @@ class GetFromLaravelAPI extends Strategy
         $methodArguments = $endpointData->method->getParameters();
         foreach ($methodArguments as $argument) {
             $argumentType = $argument->getType();
+            // If there's no typehint, continue
+            if (!$argumentType) {
+                continue;
+            }
             try {
                 $argumentClassName = $argumentType->getName();
                 $argumentInstance = new $argumentClassName;
@@ -58,7 +61,13 @@ class GetFromLaravelAPI extends Strategy
 
                     $type = $this->normalizeTypeName($argumentInstance->getKeyType());
                     $parameters[$paramName]['type'] = $type;
-                    $parameters[$paramName]['example'] = $this->generateDummyValue($type);
+
+                    // If the user explicitly set a `where()` constraint, use that to refine examples
+                    $parameterRegex = $endpointData->route->wheres[$paramName] ?? null;
+                    $example = $parameterRegex
+                        ? $this->castToType($this->getFaker()->regexify($parameterRegex), $type)
+                        : $this->generateDummyValue($type);
+                    $parameters[$paramName]['example'] = $example;
                 }
             } catch (\Throwable $e) {
                 continue;
@@ -80,12 +89,14 @@ class GetFromLaravelAPI extends Strategy
                     || class_exists($class = $rootNamespace . Str::title($urlThing))) {
                     $argumentInstance = new $class;
                     $type = $this->normalizeTypeName($argumentInstance->getKeyType());
-                    $parameters[$name]['type'] = $type;
-                    $parameters[$name]['example'] = $this->generateDummyValue($type);
                 }
             }
 
-            $parameters[$name]['example'] = $this->generateDummyValue($type);
+            $parameterRegex = $endpointData->route->wheres[$name] ?? null;
+            $example = $parameterRegex
+                ? $this->castToType($this->getFaker()->regexify($parameterRegex), $type)
+                : $this->generateDummyValue($type);
+            $parameters[$name]['example'] =$example;
             $parameters[$name]['type'] = $type;
         }