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

Consider request parameters when determining a GET request's query string #26

Closed
Thorn1089 opened this issue Mar 2, 2015 · 8 comments
Closed

Comments

@Thorn1089
Copy link

I have a documentation test like the following:

@Test
public void testSearchingRoutes() throws Exception {
    mockMvc.perform(get("/routes").param("startNear", "+42.297877,-71.485591").param("endNear", "+41.949915,-71.406392"))
        .andExpect(status().isOk())
        .andDo(document("search-routes-example"));
}

When I run this as part of my Maven build, a request-response.asciidoc is produced, but the sample curl request omits the query parameters.

Putting a breakpoint in the RoutesController shows me that the parameters are indeed received by the controller, so I believe I'm sending them correctly with MockMvc.

Any reason I would get a curl snippet like:
[source,bash]
----
$ curl http://localhost:8080/routes -i
----
for this request?

@wilkinsona
Copy link
Member

The relationship between request parameters and a URI's query string is a little bit tricky and, at the moment, Spring REST Docs only looks at the query string (HttpServletRequest.getQueryString()), whereas the param(key, value) method only makes them available from ServletRequest.getParameterMap(). The latter contains parameters both from the query string and from POSTed form data. Rather than trying to separate out the query string parameters, Spring REST docs just looks at the request's query string. Looks like we need to improve this and look in the parameter map when it's a GET request.

In the meantime, updating your test to look like the following should produce the desired snippet:

@Test
public void testSearchingRoutes() throws Exception {
    mockMvc.perform(get("/routes?startNear=+42.297877,-71.485591&endNear=+41.949915,-71.406392"))
        .andExpect(status().isOk())
        .andDo(document("search-routes-example"));
}

@wilkinsona wilkinsona changed the title Query parameters not being included in documentation snippet Consider request parameters when determining a GET request's query string Mar 2, 2015
@wilkinsona wilkinsona added this to the 1.0.0.M1 milestone Mar 2, 2015
@Thorn1089
Copy link
Author

I tried that first, with no luck, hence why I assumed I needed separate calls to param (hadn't used MockMvc before now).

Digging a little more into the code, I see

this.writer.print(String.format("curl %s://%s:%d%s", request.getScheme(),
                request.getRemoteHost(), request.getRemotePort(),
                request.getRequestURI()));

in the CurlRequestDocumentationAction class. Seems like the query string is omitted from the format string as it's built up. Any technical reason for this, or is it just an oversight? If the latter, I'll try to throw together a pull request.

@wilkinsona
Copy link
Member

That looks like you're missing the changes made in 6bdd60c. Perhaps you need to give Maven or Gradle a nudge to get it to refresh snapshot dependencies?

@Thorn1089
Copy link
Author

Aha, thanks!
On Mar 2, 2015 6:20 AM, "Andy Wilkinson" notifications@github.com wrote:

That looks like your missing the changes made in 6bdd60c
6bdd60c.
Perhaps you need to give Maven or Gradle a nudge to get it to refresh
snapshot dependencies?


Reply to this email directly or view it on GitHub
#26 (comment)
.

@Thorn1089
Copy link
Author

Took a look at Artifactory on repo.spring.io -- looks like that commit isn't in the most recent snapshot. Still, glad to know it'll be in the next update.

@wilkinsona
Copy link
Member

The changes should be in the latest snapshots as they're automatically published by the CI server. The artifact's coordinates changed from org.springframework.restdocs:spring-restdocs-core to org.springframework.restdocs:spring-restdocs recently. Perhaps you were looking at the old artifact? I've just deleted the old snapshots from the repo to hopefully make the name change a little more obvious.

@CamilYed
Copy link

CamilYed commented Jun 3, 2020

I have a problem whean I want to pass to my controller method a DTO object as query parameter.
In my test the parameters are not resolved , and my object have a null fields...

@wilkinsona
Copy link
Member

@KamilJedrzejuk That sounds like a problem with how you're making the request which is out of REST Docs' control. Perhaps you could ask a question on Stack Overflow that includes an example of how you're making the request and the controller that is receiving it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants