GREsau / okapi

OpenAPI (AKA Swagger) document generation for Rust projects
MIT License
578 stars 103 forks source link

why the okapi only works with root url / #99

Closed jiangxiaoqiang closed 2 years ago

jiangxiaoqiang commented 2 years ago

I am follow the official document to add open api it works, seems like this:

async fn rocket() -> _ {
     rocket::build()
        .mount(
            "/",
            openapi_get_routes![
                get_all_users,
                get_user,
                get_user_by_name,
                create_user,
                hidden,
                create_post_by_query,
            ],
        )
}

but when I change the route path / to /login, the UI shows that :


Fetch error
Not Found ../openapi.json

why did this happen? what should I do to fix it? my full route look like this:

fn build_rocket() -> Rocket<Build> {
    rocket::build()
        .mount(
            "/",
            openapi_get_routes![
                bill_controller::get_all_users,
                bill_controller::add
            ],
        )
        .mount("/actuator", openapi_get_routes![
            health_controller::health,
            health_controller::liveness
        ])
        .mount("/fortune/contents", openapi_get_routes![
            contents_controller::tree,
            contents_controller::fetch_available_contents
        ])
        .mount("/fortune/template", openapi_get_routes![
            bill_book_template_controller::list,
        ])
        .mount("/fortune/bill", openapi_get_routes![
            bill_controller::add,
        ])
        .mount(
            "/swagger-ui/",
            make_swagger_ui(&SwaggerUIConfig {
                url: "../openapi.json".to_owned(),
                ..Default::default()
            }),
        )
        .mount(
            "/rapidoc/",
            make_rapidoc(&RapiDocConfig {
                general: GeneralConfig {
                    spec_urls: vec![UrlObject::new("General", "../openapi.json")],
                    ..Default::default()
                },
                hide_show: HideShowConfig {
                    allow_spec_url_load: false,
                    allow_spec_file_load: false,
                    ..Default::default()
                },
                ..Default::default()
            }),
        )

}
ralpha commented 2 years ago

There are a few ways to solve this. But it also depends what you want. If you want to have 1 openapi.json with all the routes in the one file/documentation. Look at this example: custom_schema

https://github.com/GREsau/okapi/blob/65244f0037a4625f22a64cc45ef8179cdb984745/examples/custom_schema/src/main.rs#L51-L56

If you want separate openapi.json file for each path (1 for /actuator and 1 for /fortune/contents). Then you have to do it like you are currently doing. Then the files will be located at /actuator/openapi.json and /fortune/contents/openapi.json.

If you have more questions, just ask.