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

Feature: Convert $PodeContext.server.routes to Swagger/OpenAPI #218

Closed
LLIT opened this issue Apr 2, 2019 · 11 comments
Closed

Feature: Convert $PodeContext.server.routes to Swagger/OpenAPI #218

LLIT opened this issue Apr 2, 2019 · 11 comments

Comments

@LLIT
Copy link

LLIT commented Apr 2, 2019

Do you have any idea how to convert all the routes to Swagger?

@Badgerati
Copy link
Owner

I've not had much experience with Swagger/OpenAPI before, so I'll have to investigate properly to see if's possible. From an initial look it appears as though it should just be a case of generating a YAML definition from the routes 🤔

I'll keep digging when I get chance to.

@LLIT
Copy link
Author

LLIT commented Apr 3, 2019

I am on it as well, and will keep you informed.
It could require some "standards" for how to define the POST body.

In Swagger you can use JSON, that is what i normally do.

@Badgerati
Copy link
Owner

First commit has basic support for OpenAPI and Swagger. You can enable just OpenAPI, or enable Swagger with it:

Enable-PodeOpenApiRoute -Title 'OpenAPI Example' -Filter '/api/'
Enable-PodeSwaggerRoute

OpenAPI definition by default can be found at /openapi, and Swagger at /swagger.

Going to tackle Responses next.

@Badgerati
Copy link
Owner

At present, this is a simple example of setting response codes:

Add-PodeRoute -Method Get -Path "/api/resources" -ScriptBlock {
    Set-PodeResponseStatus -Code 200
} -PassThru |
    Set-PodeOpenApiRouteMetaData -Summary 'A cool summary' -Tags 'Resources' -PassThru |
    Add-PodeOpenApiRouteResponse -StatusCode 200 -PassThru |
    Add-PodeOpenApiRouteResponse -StatusCode 404

and this is a more complicated example for request path parameters, and response schema:

Add-PodeRoute -Method Get -Path '/api/users/:userId' -ScriptBlock {
    param($e)
    Write-PodeJsonResponse -Value @{ Name = 'Rick'; UserId = $e.Parameters['userId'] }
} -PassThru |
    Set-PodeOpenApiRouteMetaData -Summary 'A cool summary' -Tags 'Users' -PassThru |
    Set-PodeOpenApiRouteRequest -Parameters @(
        New-PodeOpenApiRouteRequestParameter -Integer -Name 'userId' -In Path -Required
    ) -PassThru |
    Add-PodeOpenApiRouteResponse -StatusCode 200 -Description 'A list of users' -Schemas @(
        New-PodeOpenApiSchema -Object -ContentType 'application/json' -Properties @(
            New-PodeOpenApiSchemaProperty -Name 'Name' -String
            New-PodeOpenApiSchemaProperty -Name 'UserId' -Integer
        )
    )

@Badgerati
Copy link
Owner

Things left to look at:

  • Authentication
  • Possibly have a -RestrictRoutes switch on enabling OpenApi, so it only shows routes available for the current URI being used
  • Maybe a dark mode on the inbuilt swagger? 👀 @Fraham

@Fraham
Copy link
Contributor

Fraham commented Jan 17, 2020

👀

@ArieHein
Copy link
Sponsor Contributor

Would these help in anyway ?

https://github.com/beatcracker/POSH-swagger-codegen
and the AutoRest one:

https://github.com/Azure/autorest

perhaps checking the results or getting ideas from

@Badgerati
Copy link
Owner

AutoRest I'm aware of, was going to use it during testing 😃

They're obviously a little different, as they're consuming an OpenAPI definition; building one is a whole other nightmare, haha 😂

I currently have pretty much all of the basics working. I'm just working on a refactor to make RequestBody/Response schemas simpler - then onto testing!

@Badgerati
Copy link
Owner

Last commit is a big refactor on simplifying setting RequestBody and Response schemas.

Next is to tackle Component Schemas, so we can reference generic schemas.

@Badgerati
Copy link
Owner

Added in ReDoc support on top of the Swagger support

@majkinetor
Copy link
Sponsor

Nice. Redoc rocks.

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

No branches or pull requests

5 participants