Request path is null in spring mvc

Issue #218 resolved
Former user created an issue

I tried using the spring mvc library with my spring boot application. As per the examples I tried calling the validation within an interceptor. The request path showed up as null in the logs and as result the validation was not being done. I traced the error to this code in OpenApiValidationService.java in method buildRequest: final String path = servletRequest.getServletPath(); The servletpath is not set when the validation is done within the interceptor. I could make it work after changing the path to be created from the servletcontext as shown in this solution: HttpServletRequest request = (HttpServletRequest) req; String path = request.getRequestURI().substring(request.getContextPath().length()); Since the getServletPath is fallible I suggest that the request be retrieved from the context path instead. This is a major problem since there are no errors when the path is null and the validation comes back with no errors giving the impression that the validation passed.

Comments (9)

  1. Sven Döring

    Hi Former User.

    Could you please elaborate your situation?

    Which servlet container do you use? What is your applications base path? What is your OpenAPI base path in your interface definition? Which Spring Boot version you are using? Does a SpringRunner integration test work?

    I remember a similar issue with no answers. 😒

  2. Nandini Rao

    I am using spring boot starter parent 2.0..2 and WASliberty 19.0.0.5.

    Interestingly servletRequest.getPathInfo() had the correct path but the servletRequest.getServletPath() returned an empty string.

    After a lot of digging I was able to resolve this issue by adding this line to server.xml for my liberty server -

    <webContainer servletPathForDefaultMapping="true" />

    This had the effect of populating the getServletPath() but nulling the getPathInfo. So this property acts as toggle between the 2 properties.

    Here is the document specifying the usage - https://openliberty.io/docs/ref/config/#webContainer.html

    I do think that since this is so environment dependent and the entire validation fails silently if there is no request path found, the path should be set from the context as I did before for a workaround.

    Please let me know what you all think.

    I love your project and we are currently planning on incorporating the springmvc adaptor.

    Thanks for all your efforts!

  3. Sven Döring

    This approach request.getRequestURI().substring(request.getContextPath().length()); has the disadvantage of an uri-encoded servlet path. Spaces will be returned as %20, etc.

    Could you please check if this alternative code snippet works? new org.springframework.web.util.UrlPathHelper().getPathWithinApplication(servletRequest);

  4. Nandini Rao

    Hi Sven - I apologize for the delay in responding. The alternative code snippet works fine without the need for the webcontainer setting change. It would be great if you are planning to put this fix in the code!

    A different question -

    In this part of the documentation with the validation filter setting, changing the response to false does not stop the response validation.

    The false flag on request stops the request validation but the false flag on the response does not work.

      @Bean
        public Filter validationFilter() {
            return new OpenApiValidationFilter(
                    true, // enable request validation
                    true  // enable response validation
            );
        }
    

    Thanks so much for your responsiveness about the null request issue!

    Nandini

  5. Sven Döring

    The PR is up.

    The response flag false is working properly for us. There is no response validation if the flag is set to false.
    Can you isolate the problem (and open a new issue)?

  6. Nandini Rao

    Thanks so much for the super quick resolution of this issue! I was telling my team about this when I did a demo of your tool and they were really impressed!

    I will open a new issue for the response flag.

  7. Log in to comment