Form parameters aren't available in Spring MVC

Issue #303 resolved
Emerson Farrugia created an issue

When I try to POST application/x-www-form-urlencoded with form parameter foo=aaa to a Spring MVC controller, the request handler isn’t able to find the corresponding @RequestParam. I’ve done some digging, and I suspect the ResettableRequestServletWrapper is getting in the way.

At first I thought Spring MVC’s FormContentFilter was getting in the way, but that no-ops for POSTs.

FWIW, this also happens if you set validation.request.path.missing to IGNORE and remove the request from the spec. So the problem’s probably unrelated to the OpenAPI spec itself.

I’ve created a minimal Kotlin/RestAssured/Spring Boot project to help you replicate this: https://github.com/emersonf/openapi-test. The README has more details.

Side note: I’m not sure if the custom ResettableRequestServletWrapper has benefits over org.springframework.web.util.ContentCachingRequestWrapper, but it might be worth looking into.

Comments (13)

  1. Sven Döring

    Thanks for raising the issue. I will look into it.

    The spring-mvc module started with the ContentCachingRequestWrapper. It was replaced by the custom ResettableRequestServletWrapper for several reasons:

    • slow especially if the Content-Length header is not set
    • high garbage collection pressure
    • big bodies >2.1GB are not supported

    The ContentCachingRequestWrapper purpose and strength is on caching the first n bytes of a request - e.g. for request logging.

  2. Emerson Farrugia reporter

    Thanks.

    Coincidentally I’m adding request and response validation to a system that does request and response logging. Being able to plug in and reuse the caching mechanism across filters (strategy pattern or template method pattern) would also help make this more efficient, but that would just be a nice-to-have.

  3. Sven Döring

    I found the issue. Tomcats RequestFacade merges the x-www-form-urlencoded body data into the parameters. But it does this only if the body hasn’t been read, yet… and the OpenApiValidationService does that.

    The solution will be to wrap those kind of requests into a ContentCachingRequestWrapper instead of the ResettableRequestServletWrapper - what a coincidence. 😁
    (Of course it will only wrap the request if it isn’t already wrapped into a ContentCachingRequestWrapper.)

  4. James Navin

    Thanks @Sven Döring - I will review your PR next week when I return from parental leave. Appreciate it as always!

  5. Log in to comment