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

[poem-openapi] Expose generated json #52

Closed
simoneromano96 opened this issue Oct 10, 2021 · 6 comments · Fixed by #58
Closed

[poem-openapi] Expose generated json #52

simoneromano96 opened this issue Oct 10, 2021 · 6 comments · Fixed by #58
Labels
enhancement New feature or request

Comments

@simoneromano96
Copy link
Contributor

Description of the feature

Hi, first of all good work with poem and poem-openapi, I'm enjoying a lot using them and I'll probably migrate some projects to it.

Anyway, I'd like to be able to get (and maybe serve) the generated .json file that contains the open api specification; it would also be nice to be able to deactivate the ui feature entirely.

This is because I prefer redoc and I'd like to inspect/edit the final open api document.

Code example (if possible)

#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
    let api_service = OpenApiService::new(Api)
        .title("Hello World")
        .server("http://localhost:3000/api");

    // Get open api spec
    let openapi_spec = api_service.get_spec();

    let listener = TcpListener::bind("127.0.0.1:3000");

    poem::Server::new(listener)
        .await?
        .run(
            Route::new()
                .nest("/api", api_service)
                .nest("/openapi.json", serve_openapi_spec(openapi_spec))
        )
        .await
}
@simoneromano96 simoneromano96 added the enhancement New feature or request label Oct 10, 2021
@sunli829
Copy link
Collaborator

Released in v1.0.2 😁

@simoneromano96
Copy link
Contributor Author

Released in v1.0.2 grin

Wonderful! What do you think of hiding swagger-ui behind a feature flag? I could open a PR for that

@sunli829
Copy link
Collaborator

Released in v1.0.2 grin

Wonderful! What do you think of hiding swagger-ui behind a feature flag? I could open a PR for that

Yes, welcome to PR.😁

@ollyde
Copy link

ollyde commented Mar 15, 2023

Is there any example of how to implement this?

@Shyrogan
Copy link

Still no example for this?

@kkettinger
Copy link

You can use the .spec() (json) or .spec_yaml() method for writing the openapi specs to a file like this:

let mut file = File::create("openapi.json")?;
file.write_all(api_service.spec().as_bytes())?;

There is also and endpoint .spec_endpoint() and .spec_endpoint_yaml():

Server::new(TcpListener::bind("0.0.0.0:3000"))
    .run(Route::new()
        .nest("/spec", api_service.spec_endpoint_yaml())
        .nest("/", ui))
    .await

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

Successfully merging a pull request may close this issue.

5 participants