Skip to content

Use parameter required when building path from route #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/openapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,6 @@
],
],

'clone_routes_with_optional_params' => true,

];
37 changes: 31 additions & 6 deletions src/Builders/PathsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Routing\Route;
use Illuminate\Routing\Router;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Vyuldashev\LaravelOpenApi\Attributes;
use Vyuldashev\LaravelOpenApi\Attributes\Collection as CollectionAttribute;
use Vyuldashev\LaravelOpenApi\Builders\Paths\OperationsBuilder;
Expand All @@ -15,13 +16,11 @@

class PathsBuilder
{
protected OperationsBuilder $operationsBuilder;
protected Collection $routes;

public function __construct(
OperationsBuilder $operationsBuilder
) {
$this->operationsBuilder = $operationsBuilder;
}
protected OperationsBuilder $operationsBuilder
) {}

/**
* @param string $collection
Expand Down Expand Up @@ -73,7 +72,7 @@ public function build(
protected function routes(): Collection
{
/** @noinspection CollectFunctionInCollectionInspection */
return collect(app(Router::class)->getRoutes())
$this->routes = collect(app(Router::class)->getRoutes())
->filter(static fn(Route $route) => $route->getActionName() !== 'Closure')
->map(static fn(Route $route) => RouteInformation::createFromRoute($route))
->filter(static function (RouteInformation $route) {
Expand All @@ -85,5 +84,31 @@ protected function routes(): Collection

return $pathItem && $operation;
});

if (config('openapi.clone_routes_with_optional_params', false)) {
$this->cloneRoutesWithOptionalParameters();
}

return $this->routes;
}

/**
* @todo Add support for routes with multiple optional parameters, currently only supports one optional parameter
*/
protected function cloneRoutesWithOptionalParameters(): void
{
$routes = $this->routes;
$routes->filter(
fn(RouteInformation $route) => $route->parameters->where('required', false)->isNotEmpty()
)->each(function(RouteInformation $route) {
$route = clone $route;
$uri = Str::of($route->uri);
if ($uri->substrCount('?') === 1) {
$route->uri = $uri->replaceMatches('/\{(.*?)\}/', '')->rtrim('/');
$route->parameters = collect();
$route->actionParameters = [];
$this->routes->prepend($route);
}
});
}
}