Skip to content

Commit

Permalink
added query method support
Browse files Browse the repository at this point in the history
  • Loading branch information
romalytvynenko committed Apr 7, 2024
1 parent aed04fa commit 7f8748e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/Support/Generator/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class Parameter
{
use WithAttributes;

public string $name;

/**
Expand Down
12 changes: 12 additions & 0 deletions src/Support/IndexBuilders/RequestParametersBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Dedoc\Scramble\Support\Type\Literal\LiteralFloatType;
use Dedoc\Scramble\Support\Type\Literal\LiteralIntegerType;
use Dedoc\Scramble\Support\Type\Literal\LiteralStringType;
use Dedoc\Scramble\Support\Type\MixedType;
use Dedoc\Scramble\Support\Type\ObjectType;
use Dedoc\Scramble\Support\Type\StringType;
use Dedoc\Scramble\Support\Type\TypeHelper;
Expand Down Expand Up @@ -62,6 +63,7 @@ public function afterAnalyzedNode(Scope $scope, Node $node)
'boolean' => $this->makeBooleanParameter($scope, $methodCallNode),
'string', 'str' => $this->makeStringParameter($scope, $methodCallNode),
'enum' => $this->makeEnumParameter($scope, $methodCallNode),
'query' => $this->makeQueryParameter($scope, $methodCallNode, $parameter),
default => [null, null],
};

Expand Down Expand Up @@ -140,6 +142,16 @@ private function makeEnumParameter(Scope $scope, Node $node)
];
}

private function makeQueryParameter(Scope $scope, Node $node, Parameter $parameter)
{
$parameter->setAttribute('isInQuery', true);

return [
new MixedType,
TypeHelper::getArgType($scope, $node->args, ['default', 1])->value ?? null,
];
}

private function makeDescriptionFromComments(Node\Stmt\Expression $node)
{
if ($node->getComments()) {
Expand Down
13 changes: 10 additions & 3 deletions src/Support/OperationExtensions/RequestBodyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,25 @@ public function handle(Operation $operation, RouteInfo $routeInfo)
try {
$bodyParams = $this->extractParamsFromRequestValidationRules($routeInfo->route, $routeInfo->methodNode());

$bodyParams = [...$bodyParams, ...array_values($routeInfo->requestParametersFromCalls->data)];
$allParams = [...$bodyParams, ...array_values($routeInfo->requestParametersFromCalls->data)];
[$queryParams, $bodyParams] = collect($allParams)
->partition(function (Parameter $parameter) {
return $parameter->getAttribute('isInQuery');
});
$queryParams = $queryParams->toArray();
$bodyParams = $bodyParams->toArray();

$mediaType = $this->getMediaType($operation, $routeInfo, $bodyParams);
$mediaType = $this->getMediaType($operation, $routeInfo, $allParams);

if (count($bodyParams)) {
if (count($allParams)) {
if (! in_array($operation->method, static::HTTP_METHODS_WITHOUT_REQUEST_BODY)) {
$operation->addRequestBodyObject(
RequestBodyObject::make()->setContent($mediaType, Schema::createFromParameters($bodyParams))
);
} else {
$operation->addParameters($bodyParams);
}
$operation->addParameters($queryParams);
} elseif (! in_array($operation->method, static::HTTP_METHODS_WITHOUT_REQUEST_BODY)) {
$operation
->addRequestBodyObject(
Expand Down
26 changes: 25 additions & 1 deletion tests/Support/OperationExtensions/RequestBodyExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ public function index(Illuminate\Http\Request $request)
$request->boolean('is_foo');

$request->string('name', 'John Doe');
// $request->query('in_query');
}
}

Expand Down Expand Up @@ -165,3 +164,28 @@ enum RequestBodyExtensionTest__Status_Params_Extraction: string {
case Hearts = 'hearts';
case Spades = 'spades';
}


it('extracts parameters, their defaults, and descriptions from calling request parameters retrieving methods with query', function () {
$openApiDocument = generateForRoute(function () {
return RouteFacade::post('api/test', [RequestBodyExtensionTest__extracts_parameters_from_retrieving_methods_with_query::class, 'index']);
});

expect($openApiDocument['paths']['/test']['post']['parameters'])
->toHaveLength(1)
->toBe([[
'name' => 'in_query',
'in' => 'query',
'schema' => [
'type' => 'string',
],
'default' => 'foo',
]]);
});
class RequestBodyExtensionTest__extracts_parameters_from_retrieving_methods_with_query
{
public function index(Illuminate\Http\Request $request)
{
$request->query('in_query', 'foo');
}
}

0 comments on commit 7f8748e

Please sign in to comment.