Skip to content

Commit

Permalink
enum inference from type support
Browse files Browse the repository at this point in the history
  • Loading branch information
romalytvynenko committed Apr 7, 2024
1 parent 8cdff9c commit aed04fa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
14 changes: 14 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\ObjectType;
use Dedoc\Scramble\Support\Type\StringType;
use Dedoc\Scramble\Support\Type\TypeHelper;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -60,6 +61,7 @@ public function afterAnalyzedNode(Scope $scope, Node $node)
'float' => $this->makeFloatParameter($scope, $methodCallNode),
'boolean' => $this->makeBooleanParameter($scope, $methodCallNode),
'string', 'str' => $this->makeStringParameter($scope, $methodCallNode),
'enum' => $this->makeEnumParameter($scope, $methodCallNode),
default => [null, null],
};

Expand Down Expand Up @@ -126,6 +128,18 @@ private function makeStringParameter(Scope $scope, Node $node)
];
}

private function makeEnumParameter(Scope $scope, Node $node)
{
if (!$className = TypeHelper::getArgType($scope, $node->args, ['default', 1])->value ?? null) {
return [null, null];
}

return [
new ObjectType($className),
null,
];
}

private function makeDescriptionFromComments(Node\Stmt\Expression $node)
{
if ($node->getComments()) {
Expand Down
34 changes: 32 additions & 2 deletions tests/Support/OperationExtensions/RequestBodyExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,39 @@ public function index(Illuminate\Http\Request $request)
$request->boolean('is_foo');

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

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

expect($properties = $openApiDocument['paths']['/test']['post']['requestBody']['content']['application/json']['schema']['properties'])
->toHaveLength(1)
->and($properties['status'])
->toBe([
'$ref' => '#/components/schemas/RequestBodyExtensionTest__Status_Params_Extraction'
])
->and($openApiDocument['components']['schemas']['RequestBodyExtensionTest__Status_Params_Extraction'])
->toBe([
'type' => 'string',
'enum' => [
'clubs',
'diamonds',
'hearts',
'spades',
],
'title' => 'RequestBodyExtensionTest__Status_Params_Extraction'
]);
});
class RequestBodyExtensionTest__extracts_parameters_from_retrieving_methods_with_enum
{
public function index(Illuminate\Http\Request $request)
{
$request->enum('status', RequestBodyExtensionTest__Status_Params_Extraction::class);

// $request->enum('status', RequestBodyExtensionTest__Status_Params_Extraction::class);
//
// $request->query('in_query');
}
}
Expand Down

0 comments on commit aed04fa

Please sign in to comment.