Skip to content

Commit

Permalink
Merge pull request #409 from YannikFirre/patch-2
Browse files Browse the repository at this point in the history
Fetch URL parameter examples from database
  • Loading branch information
shalvah authored Jan 31, 2022
2 parents f45ad07 + b431bc6 commit 887d895
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,20 @@ 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);
// Try to fetch an example ID from the database
try {
$example = $argumentInstance::first()->id ?? null;
} catch (\Throwable $e) {
$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
? $this->castToType($this->getFaker()->regexify($parameterRegex), $type)
: $this->generateDummyValue($type);
}
$parameters[$paramName]['example'] = $example;
}
} catch (\Throwable $e) {
Expand Down

0 comments on commit 887d895

Please sign in to comment.