Skip to content

router_service is not called early enough in 2.2.1 #7555

@yanns

Description

@yanns

Describe the bug

Starting from version 2.2.1, some requests are rejected before ServiceBuilder::new().map_request in router_service is called.

To Reproduce

We're using ServiceBuilder::new().map_request in router_service to accept requests that do not have the content type application/json set.
This is working until version 2.2.0 included.
In version 2.2.1, requests are being rejected before ServiceBuilder::new().map_request is called.

Desktop (please complete the following information):

  • Version: 2.2.1

Additional context

The code:

#[derive(Debug, Default, Deserialize, JsonSchema)]
struct InsertApplicationJsonHeaderConfig {}

#[derive(Debug)]
struct InsertApplicationJsonHeaderPlugin {}

static APPLICATION_JSON: HeaderValue = HeaderValue::from_static("application/json");

#[async_trait::async_trait]
impl Plugin for InsertApplicationJsonHeaderPlugin {
    type Config = InsertApplicationJsonHeaderConfig;

    async fn new(_init: PluginInit<Self::Config>) -> Result<Self, BoxError> {
        Ok(Self {})
    }

    fn router_service(&self, service: BoxService) -> BoxService {
        ServiceBuilder::new()
            .map_request(|mut router_request: router::Request| {
                insert_application_json(&mut router_request);
                router_request
            })
            .service(service)
            .boxed()
    }
}

fn insert_application_json(router_request: &mut router::Request) {
    let headers = router_request.router_request.headers_mut();
    match headers.get(CONTENT_TYPE) {
        Some(content_type) if content_type == APPLICATION_JSON => (),
        _ => {
            headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
        }
    }
}

register_plugin!(
    "sphere",
    "insert_application_json_header",
    InsertApplicationJsonHeaderPlugin
);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions