Skip to content

Commit

Permalink
default and query support when provided in validation rules
Browse files Browse the repository at this point in the history
  • Loading branch information
romalytvynenko committed Apr 7, 2024
1 parent 7ccb054 commit d71f07a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ private function applyDocsInfo(Parameter $parameter)
$parameter->example($examples[0]);
}

if ($default = ExamplesExtractor::make($this->docNode, '@default')->extract(preferString: $parameter->schema->type instanceof StringType)) {
$parameter->default($default[0]);
}

if ($this->docNode->getTagsByName('@query')) {
$parameter->setAttribute('isInQuery', true);
}

return $parameter;
}

Expand Down
34 changes: 34 additions & 0 deletions tests/Support/OperationExtensions/RequestBodyExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,37 @@ public function index(Illuminate\Http\Request $request)
$request->integer('foo', 10);
}
}

it('allows specifying query position and default for params inferred from validation rules using validate method', function () {
$openApiDocument = generateForRoute(function () {
return RouteFacade::post('api/test', [RequestBodyExtensionTest__allows_specifying_query_position_and_default_for_params_inferred_from_validation_rules_using_validate_method::class, 'index']);
});

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

0 comments on commit d71f07a

Please sign in to comment.