Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use parameter required when building path from route #49

Closed
wants to merge 4 commits into from

Conversation

adam-code-labx
Copy link

Here is a small modification to support optional route parameters when generating openapi.

@adam-code-labx
Copy link
Author

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 😉

@adam-code-labx
Copy link
Author

Sorry @vyuldashev I didn't realise this is not supported by OpenAPI
OAI/OpenAPI-Specification#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.

@adam-code-labx
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant