Browse Source

Merge pull request #497 from Khaled-Farhat/fix-url-params-type

Fixed two bugs in inferring the URL parameter type
Shalvah 2 years ago
parent
commit
8dcfcd3cc3
1 changed files with 13 additions and 3 deletions
  1. 13 3
      src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php

+ 13 - 3
src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php

@@ -101,11 +101,14 @@ class GetFromLaravelAPI extends Strategy
             $urlThing = $this->getNameOfUrlThing($endpointData->uri, $name);
             $urlThing = $this->getNameOfUrlThing($endpointData->uri, $name);
             if ($urlThing) {
             if ($urlThing) {
                 $rootNamespace = app()->getNamespace();
                 $rootNamespace = app()->getNamespace();
-                if (class_exists($class = "{$rootNamespace}Models\\" . Str::title($urlThing))
+                $className = $this->urlThingToClassName($urlThing);
+                if (class_exists($class = "{$rootNamespace}Models\\" . $className)
                     // For the heathens that don't use a Models\ directory
                     // For the heathens that don't use a Models\ directory
-                    || class_exists($class = $rootNamespace . Str::title($urlThing))) {
+                    || class_exists($class = $rootNamespace . $className)) {
                     $argumentInstance = new $class;
                     $argumentInstance = new $class;
-                    $type = $this->normalizeTypeName($argumentInstance->getKeyType());
+                    if ($argumentInstance->getKeyName() === $argumentInstance->getRouteKeyName()) {
+                        $type = $this->normalizeTypeName($argumentInstance->getKeyType());
+                    }
                 }
                 }
             }
             }
 
 
@@ -158,4 +161,11 @@ class GetFromLaravelAPI extends Strategy
             return null;
             return null;
         }
         }
     }
     }
+
+    protected function urlThingToClassName(string $urlThing): string
+    {
+        $className = Str::title($urlThing);
+        $className = str_replace(['-', '_'], '', $className);
+        return $className;
+    }
 }
 }