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

fix: diverging axum route and openapi spec #1199

Merged
merged 3 commits into from
Nov 21, 2024

Conversation

boemjay
Copy link
Contributor

@boemjay boemjay commented Nov 9, 2024

Currently there is the possibility that the swagger-ui does not work with utoipa_axum in certain conditions.
The reason for this is, that the path resolution in the .nest(router) function is different in axum and utoipa.

This pull request fixes it by applying the same logic for the OpenAPI spec generation, that is used in axum.

How to reproduce this issue

With the following minimal reproducible example, open the swagger ui on localhost:3000/docs.
The only endpoint is /a/, but when you perform the request, it returns 404 Not Found.

use std::io;
use utoipa::OpenApi;
use utoipa_axum::{router::OpenApiRouter, routes};
use utoipa_swagger_ui::SwaggerUi;

#[derive(OpenApi)]
#[openapi()]
struct ApiDoc;

#[tokio::main]
async fn main() -> Result<(), io::Error> {
    #[utoipa::path(get, path = "/")]
    async fn nested_get_all() -> String {
        String::from("nested get all")
    }

    let (router, api) = OpenApiRouter::with_openapi(ApiDoc::openapi())
        .nest("/a", OpenApiRouter::new().routes(routes!(nested_get_all)))
        .split_for_parts();

    let router = router.merge(SwaggerUi::new("/docs").url("/apidoc/openapi.json", api));

    let listener = tokio::net::TcpListener::bind(("0.0.0.0", 3000)).await?;
    axum::serve(listener, router).await
}

Cause of the issue

In Axum the .nest logic joins the prefix and nested path in a different way than utopia. E.g. for nested routes the trailing / is stripped away and other logic to prevent double // in the joined path.

Copy link
Owner

@juhaku juhaku left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty neat, 👍

However could you add a line to CHANGELOG.md for the addition at beginning of file.

## Unreleased

### Fixed

* title (http;//PR)

@juhaku
Copy link
Owner

juhaku commented Nov 21, 2024

I added the CHANGELOG markings for the PR 👌

@juhaku juhaku merged commit 7cf06e6 into juhaku:master Nov 21, 2024
12 checks passed
juhaku added a commit that referenced this pull request Dec 9, 2024
Fix axum path nesting in `OpenApiRouter` to allow empty paths and
correctly template the path for `OpenApiRouter` from colons.

Relates to #1199
@juhaku juhaku mentioned this pull request Dec 9, 2024
juhaku added a commit that referenced this pull request Dec 9, 2024
Fix axum path nesting in `OpenApiRouter` to allow empty paths and
correctly template the path for `OpenApiRouter` from colons.

Relates to #1199
juhaku added a commit that referenced this pull request Dec 9, 2024
Fix axum path nesting in `OpenApiRouter` to allow empty paths and
correctly template the path for `OpenApiRouter` from colons.

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

Successfully merging this pull request may close these issues.

2 participants