juhaku / utoipa

Simple, Fast, Code first and Compile time generated OpenAPI documentation for Rust
Apache License 2.0
2.46k stars 192 forks source link

How do i display only the url without the UI path? #598

Closed afidegnum closed 1 month ago

afidegnum commented 1 year ago

With swagger-ui I would like to return only the openapi schema without the UI,

the following code still returns the ui interface. SwaggerUi::new("").url("/api-docs/openapi.json", openapi.clone()),

juhaku commented 1 year ago

For such case you need to serve it through an enpoint as you would do with any other content. And also with that you do not need the utoipa-swagger-ui library to be there.

async fn get_api_doc(....) -> Json<utoipa::openapi::OpenApi> {
    let doc = // .... load the doc somewhere or from fn arguments
    Json(doc)
}
afidegnum commented 1 year ago

Can you please give an example? i'm a bit confused.

juhaku commented 1 year ago

Sure,

For example if you take a look at the warp example you can find that the apidoc.json is served via regular http endpoint handler (whatever handles the incoming requests). https://github.com/juhaku/utoipa/blob/489ddd663051a578e9285f5f4b5d87c0f626d6c0/examples/todo-warp/src/main.rs#L46-L48

Or with actix_web same example might look something like this:

#[get("/api-docs/openapi.json")]
async fn get_api_doc(apidoc: Data<OpenApi>) -> impl Responder {
    let doc = apidoc.lock().unwrap();
    HttpResponse::Ok().json(doc)
}

If you want to return only the openapi schema then you need to create a custom endpoint for it according to the framework of your choise. You cannot use the SwaggerUi type without exposing the Swagger UI to the public.

Does this help? Or have I understood your question totally wrong?

afidegnum commented 1 year ago

Thanks a lot.

juhaku commented 1 month ago

Closed due inactivity.