From 46130e2e52a07115ac5ec63e79a928dc61faf392 Mon Sep 17 00:00:00 2001 From: Yannik Firre <3316758+YannikFirre@users.noreply.github.com> Date: Thu, 20 Jan 2022 11:15:11 +0100 Subject: [PATCH 1/3] ADDED - search id in DB when model instance --- .../Strategies/UrlParameters/GetFromLaravelAPI.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php b/src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php index 0667966a..95080891 100644 --- a/src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php +++ b/src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php @@ -69,11 +69,15 @@ public function __invoke(ExtractedEndpointData $endpointData, array $routeRules) $type = $this->normalizeTypeName($typeName); $parameters[$paramName]['type'] = $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); + $example = optional($argumentInstance::first())->id; + + if($example === null) { + // 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) { From acfb1ceba38a318e68416ffb4017af33fe858e26 Mon Sep 17 00:00:00 2001 From: Yannik Firre <3316758+YannikFirre@users.noreply.github.com> Date: Mon, 24 Jan 2022 18:18:50 +0100 Subject: [PATCH 2/3] Update GetFromLaravelAPI.php --- src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php b/src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php index 95080891..3df05f0b 100644 --- a/src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php +++ b/src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php @@ -69,7 +69,7 @@ public function __invoke(ExtractedEndpointData $endpointData, array $routeRules) $type = $this->normalizeTypeName($typeName); $parameters[$paramName]['type'] = $type; - $example = optional($argumentInstance::first())->id; + $example = $argumentInstance::first()->id ?? null; if($example === null) { // If the user explicitly set a `where()` constraint, use that to refine examples From b431bc62e180715c2acf7b770153e4efb5d15a48 Mon Sep 17 00:00:00 2001 From: shalvah Date: Mon, 31 Jan 2022 10:01:46 +0100 Subject: [PATCH 3/3] Wrap in try/catch --- .../Strategies/UrlParameters/GetFromLaravelAPI.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php b/src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php index 3df05f0b..0b897509 100644 --- a/src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php +++ b/src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php @@ -69,9 +69,14 @@ public function __invoke(ExtractedEndpointData $endpointData, array $routeRules) $type = $this->normalizeTypeName($typeName); $parameters[$paramName]['type'] = $type; - $example = $argumentInstance::first()->id ?? null; + // Try to fetch an example ID from the database + try { + $example = $argumentInstance::first()->id ?? null; + } catch (\Throwable $e) { + $example = null; + } - if($example === null) { + if ($example === null) { // If the user explicitly set a `where()` constraint, use that to refine examples $parameterRegex = $endpointData->route->wheres[$paramName] ?? null; $example = $parameterRegex