Skip to content

Commit

Permalink
@query and @default tags support
Browse files Browse the repository at this point in the history
  • Loading branch information
romalytvynenko committed Apr 7, 2024
1 parent f368442 commit 532e0da
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Support/Helpers/ExamplesExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function make(?PhpDocNode $docNode, string $tagName = '@example')

public function extract(bool $preferString = false)
{
if (! count($examples = $this->docNode->getTagsByName($this->tagName))) {
if (! count($examples = $this->docNode?->getTagsByName($this->tagName) ?? [])) {
return [];
}

Expand Down
23 changes: 16 additions & 7 deletions src/Support/IndexBuilders/RequestParametersBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public function afterAnalyzedNode(Scope $scope, Node $node)
$parameterDefault = $parameterDefaultFromDoc;
}

$this->checkExplicitParameterPlacementInQuery($node, $parameter);

$parameter
->description($this->makeDescriptionFromComments($node))
->setSchema(Schema::fromType(
Expand Down Expand Up @@ -168,10 +170,7 @@ private function makeDescriptionFromComments(Node\Stmt\Expression $node)
* @todo: consider adding only @param annotation support,
* so when description is taken only if comment is marked with @param
*/
if ($node->getDocComment()) {
/** @var PhpDocNode $phpDoc */
$phpDoc = $node->getAttribute('parsedPhpDoc');

if ($phpDoc = $node->getAttribute('parsedPhpDoc')) {
return trim($phpDoc->getAttribute('summary').' '.$phpDoc->getAttribute('description'));
}

Expand All @@ -188,17 +187,27 @@ private function makeDescriptionFromComments(Node\Stmt\Expression $node)

private function shouldIgnoreParameter(Node\Stmt\Expression $node)
{
/** @var PhpDocNode $phpDoc */
/** @var PhpDocNode|null $phpDoc */
$phpDoc = $node->getAttribute('parsedPhpDoc');

return !! $phpDoc->getTagsByName('@ignoreParam');
return !! $phpDoc?->getTagsByName('@ignoreParam');
}

private function getParameterDefaultFromPhpDoc(Node\Stmt\Expression $node)
{
/** @var PhpDocNode $phpDoc */
/** @var PhpDocNode|null $phpDoc */
$phpDoc = $node->getAttribute('parsedPhpDoc');

return ExamplesExtractor::make($phpDoc, '@default')->extract()[0] ?? null;
}

private function checkExplicitParameterPlacementInQuery(Node\Stmt\Expression $node, Parameter $parameter)
{
/** @var PhpDocNode|null $phpDoc */
$phpDoc = $node->getAttribute('parsedPhpDoc');

if(!! $phpDoc?->getTagsByName('@query')) {
$parameter->setAttribute('isInQuery', true);
}
}
}
24 changes: 24 additions & 0 deletions tests/Support/OperationExtensions/RequestBodyExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,27 @@ public function index(Illuminate\Http\Request $request)
$request->integer('foo', 10);
}
}

it('allows explicitly specifying parameter placement in query manually in doc', function () {
$openApiDocument = generateForRoute(function () {
return RouteFacade::post('api/test', [RequestBodyExtensionTest__allows_explicitly_specifying_parameter_placement_in_query_manually_in_doc::class, 'index']);
});

expect($openApiDocument['paths']['/test']['post']['requestBody']['content']['application/json']['schema']['properties'] ?? [])
->toBeEmpty()
->and($openApiDocument['paths']['/test']['post']['parameters'])
->toBe([[
'name' => 'foo',
'in' => 'query',
'schema' => ['type' => 'integer'],
'default' => 10,
]]);
});
class RequestBodyExtensionTest__allows_explicitly_specifying_parameter_placement_in_query_manually_in_doc
{
public function index(Illuminate\Http\Request $request)
{
/** @query */
$request->integer('foo', 10);
}
}

0 comments on commit 532e0da

Please sign in to comment.