poem icon indicating copy to clipboard operation
poem copied to clipboard

Regex routing: byte index 14 is out of bounds of `/api/clients`

Open michaljanocko opened this issue 2 years ago • 4 comments

Expected Behavior

Route successfully

Actual Behavior

Router panics with byte index 14 is out of bounds of /api/clients

Steps to Reproduce the Problem

  1. Create a route and then .nest_no_strip("/<(api|oauth)>", service)
  2. Send a request for example to /api/clients

I'm using an OpenApi service for service. I have other stuff on route / and that's why I wanted to do this.

Specifications

  • Version: 1.3.52
  • Platform: Arch Linux

michaljanocko avatar Jan 24 '23 10:01 michaljanocko

I can't reproduce this issue:

let app = Route::new().nest_no_strip(
            "/<(api|oauth)>",
            make_sync(|req| req.uri().path().to_string()),
        );

        let resp = app
            .get_response(
                Request::builder()
                    .uri(Uri::from_static("/api/clients"))
                    .finish(),
            )
            .await;

        assert_eq!(resp.status(), StatusCode::OK);
        assert_eq!(
            resp.into_body().into_string().await.unwrap(),
            "/api/clients"
        );

sunli829 avatar Jan 30 '23 12:01 sunli829

Seems like I provided too little detail. I managed to reproduce with this:

use poem::{
    http::{StatusCode, Uri},
    Endpoint, Request, Route,
};
use poem_openapi::{payload::Json, OpenApi, OpenApiService};

struct ClientApi;

#[OpenApi(prefix_path = "/api/clients")]
impl ClientApi {
    #[oai(path = "/", method = "get")]
    async fn list(&self) -> Json<String> {
        Json("/api/clients".to_string())
    }
}

#[tokio::main]
async fn main() {
    let service = OpenApiService::new(ClientApi, "Clients", env!("CARGO_PKG_VERSION"));

    let app = Route::new().nest_no_strip("/<(api|oauth)>", service);

    let resp = app
        .get_response(
            Request::builder()
                .uri(Uri::from_static("/api/clients"))
                .finish(),
        )
        .await;

    assert_eq!(resp.status(), StatusCode::OK);
    assert_eq!(
        resp.into_body().into_string().await.unwrap(),
        "/api/clients"
    );
}

michaljanocko avatar Jan 30 '23 13:01 michaljanocko

regex on nested paths are not currently supported, you can do it like below:

let app = Route::new().nest_no_strip("/api", service)
    .nest_no_strip("/api", service)
    .nest_no_strip("/oauth", service);

sunli829 avatar Jan 31 '23 01:01 sunli829

Sorry, I'm unsure how to go about this, since the OpenAPI service doesn't implement either Copy or Clone.

michaljanocko avatar Jan 31 '23 19:01 michaljanocko