Skip to content
New issue

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

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

Already on GitHub? # to your account

How to dynamically set spec_url in RapiDoc? #805

Closed
dean-shaff opened this issue Nov 24, 2023 · 1 comment · Fixed by #867
Closed

How to dynamically set spec_url in RapiDoc? #805

dean-shaff opened this issue Nov 24, 2023 · 1 comment · Fixed by #867

Comments

@dean-shaff
Copy link

I'm wondering how I can dynamically set the spec_url for RapiDoc:

This works fine:

use actix_web::{HttpServer, App}; 
use utoipa_rapidoc::Rapidoc;

HttpServer::new(move || {
    App::new()
        .service(RapidDoc::new("/api-docs/openapi.json"))
})
...

But what if I want to do the following?:

use actix_web::App; 
use utoipa_rapidoc::Rapidoc;

let base_url = "/some/path";
let spec_url = format!("{}/api-docs/openapi.json", base_url);

HttpServer::new(move || {
    App::new()
        .service(RapiDoc::new(&spec_url));
})

The fact that I have to pass a &str to RapiDoc::new makes this impossible!

simongoricar added a commit to simongoricar/utoipa that referenced this issue Feb 14, 2024
Made the fields match the existing example in `utoipa_swagger_ui::SwaggerUi` (field `path`).

Fixes juhaku#805
@simongoricar
Copy link
Contributor

A bit late to the party, but I can also reproduce this. Even if we do spec_url.clone().as_str(), we get a temporary value dropped while borrowed. It seems to me we'd need a 'static str because of the move closure, not just any str borrow.

let base_url = "/some/path";
let spec_url = format!("{}/api-docs/openapi.json", base_url);

HttpServer::new(move || App::new().service(RapiDoc::new(spec_url.clone().as_str())));
error[E0716]: temporary value dropped while borrowed
   --> ...\src\main.rs:147:61
    |
147 |     HttpServer::new(move || App::new().service(RapiDoc::new(spec_url.clone().as_str())));
    |                                                -------------^^^^^^^^^^^^^^^^----------- temporary value is freed at the end of this statement
    |                                                |            |
    |                                                |            creates a temporary value which is freed while still in use
    |                                                argument requires that borrow lasts for `'static`

Unless there's something I'm missing, this has a very simple fix: simply have the path field of RapiDoc take a String instead of a borrowed str, or even better, a Cow<'static, str>, like utoipa_swagger_ui.

I'll submit a PR.

juhaku pushed a commit that referenced this issue Feb 18, 2024
#867)

Made the fields match the existing example in `utoipa_swagger_ui::SwaggerUi` (field `path`).

Fixes #805
juhaku pushed a commit that referenced this issue Feb 19, 2024
#869)

* Refactor RapiDoc to take `Cow<'static, str>` instead of borrowed `str`

Made the fields match the existing example in `utoipa_swagger_ui::SwaggerUi` (field `path`).

Fixes #805

* Refactor `ReDoc` to take `Cow<'static, str>` instead of borrowed `str`
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants