laravel-openapi
laravel-openapi copied to clipboard
Use parameter required when building path from route
Here is a small modification to support optional route parameters when generating openapi.
Only just come across your package today as I was looking for an API package which supports PHP 8. This one is very impressive on the how you have taken advantage of PHP 8 new Attributes feature. Here is my 1st PR hopefully many more to follow 😉
Sorry @vyuldashev I didn't realise this is not supported by OpenAPI https://github.com/OAI/OpenAPI-Specification/issues/93
I am currently building out an API for example one of my routes looks like this:
Route::get('services/{type?}', [IntegrationServicesController::class, 'index'])->name('integration.services');
Where type is optional.
I don't want to have to repeat the route every time in Laravel just to return all services without type. So I am thinking of perhaps updating my PR at some point later in the week where if there are optional route parameters we can automatically generate both the required path for example /services/integrated-service and also generate /services
This way we don't need to be concerned about the OpenAPI restrictions.
Furthermore I would like to update the generator to output for both JSON & YAML with the option to save to storage. Finally we should consider validating the output for example responses are also required. So perhaps we can set some optional defaults in the config?
Thats all for now. Let me know what you think about these suggestions and if this will be useful for this package? If not no problem I will continue to implement these changes in my forked version. Thanks.
I have now reverted the parameter change and instead added functionality to automatically generate routes for optional parameters. For example: api/services/{type?} now generates 2 paths for the OpenAPI spec without additional duplication in the controller or routes.
"paths": {
"\/api\/services": {
"get": {
"tags": [
"services"
],
"summary": "Return Collection of Integration Service Resource."
}
},
"\/api\/services\/{type?}": {
"get": {
"tags": [
"services"
],
"summary": "Return Collection of Integration Service Resource.",
"parameters": [
{
"name": "type",
"in": "path",
"description": "Service Type",
"required": true,
"schema": {
"type": "string"
}
}
]
}
},
I have also made some further improvements to the generator which I will make a separate PR for this one. If you require any tests for this one please let me know. Thanks.