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

Document that the query string should be provided in the URL or by using queryParam when using MockMvc #926

Closed
nick-shevchenko opened this issue Apr 19, 2024 · 1 comment
Labels
type: documentation Changes to the documentation
Milestone

Comments

@nick-shevchenko
Copy link

Greetings!

Query parameters are ignored when using param builder:

mockMvc.perform(
            delete("/v1/event/configs/delete")
                .param("eventName", "CART2")
                .param("name", "available")
            )
            .andExpect(status().isOk)
            .andDo(document("delete-event-config"))

However, this one works:

mockMvc.perform(
            delete("/v1/event/configs/delete?eventName=CART2&name=available"))
            .andExpect(status().isOk)
            .andDo(document("delete-event-config"))

Using:
org.springframework.boot:spring-boot:3.2.3
org.springframework.restdocs:spring-restdocs-mockmvc:3.0.1
org.springframework.restdocs:spring-restdocs-asciidoctor:3.0.1

@wilkinsona
Copy link
Member

wilkinsona commented May 3, 2024

Thanks for the report.

The problem here is that the param method is ambiguous. It adds the parameter directly to the parameter map that's a server-side construct that can be populated from either the query string of the HTTP request's URI or from the body of the HTTP request if it's an application/x-www-form-urlencoded request. In your example above, you've used the param method and haven't specific a content type so REST Docs has to guess your intent. It's getting that wrong for your usage and incorrectly assuming that you intended the parameters to be included in the request's body.

You can avoid the ambiguity described above, either by including the query parameters directly in the URL as you have done above, or by using the queryParam method instead of the param method.

REST Docs' documentation could certainly be improved in this area. Beyond that, I'm not sure what can be done as changing the current behavior risks breaking someone's existing usage. One option is to make some changes in Framework so that it's harder to get into this situation. I've opened spring-projects/spring-framework#32757 to discuss that with the Framework team.

@wilkinsona wilkinsona changed the title Query parameters ignored for HTTP DELETE Document that the query string should be provided in the URL or by using queryParam when using MockMvc May 3, 2024
@wilkinsona wilkinsona added type: documentation Changes to the documentation and removed status: waiting-for-triage Untriaged issue labels May 3, 2024
@wilkinsona wilkinsona added this to the 3.0.x milestone May 3, 2024
@wilkinsona wilkinsona modified the milestones: 3.0.x, 3.0.2 May 7, 2024
natedanner pushed a commit to natedanner/spring-projects__spring-restdocs that referenced this issue May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation Changes to the documentation
Projects
None yet
Development

No branches or pull requests

3 participants