From 127221adeea6b5a9ea835c68de72b76b49d12932 Mon Sep 17 00:00:00 2001 From: Hussein Alaa Date: Fri, 13 Sep 2024 12:31:20 +0300 Subject: [PATCH] support Spatie Query Builder filters --- src/Swagger.php | 51 +++++++++++++++++++++++++++++++++++++++++- src/config/swagger.php | 10 +++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/Swagger.php b/src/Swagger.php index f6ea2dc..97546ef 100644 --- a/src/Swagger.php +++ b/src/Swagger.php @@ -110,6 +110,11 @@ public function generateSwaggerJsonResponse() "class" => $controller_path, "description" => $controller_description ]; + + $params = $this->formatParams($validations, $route); + + $this->spatieSchema($params, $method); + $apiRoutes[] = [ 'prefix' => $prefix, 'method' => $method, @@ -122,7 +127,7 @@ public function generateSwaggerJsonResponse() 'action' => $action, 'middleware' => $route->middleware(), 'validations' => $validations, - 'params' => $this->formatParams($validations, $route), + 'params' => $params, 'operation_id' => $operationId, 'has_schema' => $hasSchema, 'need_token' => $needToken @@ -183,4 +188,48 @@ private function getVersion() } return $version; } + + + private function spatieSchema(&$params, $method) + { + $spatie_query_builder = config('swagger.spatie_query_builder'); + if ($method == 'GET|HEAD' && $spatie_query_builder) { + $params[] = [ + "name" => "filter", + "in" => "query", + "description" => "Filter", + "required" => false, + "style" => "deepObject", + "explode" => true, + "schema" => [ + "type" => "object", + "additionalProperties" => [ + "type" => "string", + "description" => "The dynamic filtering" + ] + ] + ]; + $params[] = [ + "name" => "sort", + "in" => "query", + "description" => "sort", + "required" => false, + "schema" => [ + "nullable" => "true", + "type" => "string", + ] + ]; + $params[] = [ + "name" => "include", + "in" => "query", + "description" => "include", + "required" => false, + "schema" => [ + "nullable" => "true", + "type" => "string", + ] + ]; + } + } + } diff --git a/src/config/swagger.php b/src/config/swagger.php index 6c9ce6f..9b51f80 100644 --- a/src/config/swagger.php +++ b/src/config/swagger.php @@ -162,6 +162,16 @@ ] ], + /* + |-------------------------------------------------------------------------- + | Spatie Query Builder + |-------------------------------------------------------------------------- + | + | Enable it if you using Spatie query builder package to add spatie filters in all GET routes. + | + */ + "spatie_query_builder" => false, + /* |--------------------------------------------------------------------------