Skip to content

Commit f5a629a

Browse files
committed
Handle form and query parameters separately
Previously, form and query parameters were handled together as request parameters. Howeer, request parameters are a server-side construct that's specific to the servlet specification. As such they're not appropriate for the client-side documentation that Spring REST Docs aims to produce. This commit replaces support for documenting request parameters with support for documenting query paramters found in the query string of the request's URI and for documenting form parameters found in the form URL encoded body of the request. Closes gh-832
1 parent b4be34b commit f5a629a

File tree

63 files changed

+1716
-1342
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1716
-1342
lines changed

Diff for: docs/src/docs/asciidoc/documenting-your-api.adoc

+53-28
Original file line numberDiff line numberDiff line change
@@ -649,85 +649,110 @@ For example, the preceding code results in a snippet named `response-fields-bene
649649

650650

651651

652-
[[documenting-your-api-request-parameters]]
653-
=== Request Parameters
652+
[[documenting-your-api-query-parameters]]
653+
=== Query Parameters
654654

655-
You can document a request's parameters by using `requestParameters`.
656-
You can include request parameters in a `GET` request's query string.
655+
You can document a request's query parameters by using `queryParameters`.
657656
The following examples show how to do so:
658657

659658
[source,java,indent=0,role="primary"]
660659
.MockMvc
661660
----
662-
include::{examples-dir}/com/example/mockmvc/RequestParameters.java[tags=request-parameters-query-string]
661+
include::{examples-dir}/com/example/mockmvc/QueryParameters.java[tags=query-parameters]
663662
----
664663
<1> Perform a `GET` request with two parameters, `page` and `per_page`, in the query string.
665-
<2> Configure Spring REST Docs to produce a snippet describing the request's parameters.
666-
Uses the static `requestParameters` method on `org.springframework.restdocs.request.RequestDocumentation`.
664+
<2> Configure Spring REST Docs to produce a snippet describing the request's query parameters.
665+
Uses the static `queryParameters` method on `org.springframework.restdocs.request.RequestDocumentation`.
667666
<3> Document the `page` parameter.
668667
Uses the static `parameterWithName` method on `org.springframework.restdocs.request.RequestDocumentation`.
669668
<4> Document the `per_page` parameter.
670669

671670
[source,java,indent=0,role="secondary"]
672671
.WebTestClient
673672
----
674-
include::{examples-dir}/com/example/webtestclient/RequestParameters.java[tags=request-parameters-query-string]
673+
include::{examples-dir}/com/example/webtestclient/QueryParameters.java[tags=query-parameters]
675674
----
676675
<1> Perform a `GET` request with two parameters, `page` and `per_page`, in the query string.
677-
<2> Configure Spring REST Docs to produce a snippet describing the request's parameters.
678-
Uses the static `requestParameters` method on `org.springframework.restdocs.request.RequestDocumentation`.
676+
<2> Configure Spring REST Docs to produce a snippet describing the request's query parameters.
677+
Uses the static `queryParameters` method on `org.springframework.restdocs.request.RequestDocumentation`.
679678
<3> Document the `page` parameter.
680679
Uses the static `parameterWithName` method on `org.springframework.restdocs.request.RequestDocumentation`.
681680
<4> Document the `per_page` parameter.
682681

683682
[source,java,indent=0,role="secondary"]
684683
.REST Assured
685684
----
686-
include::{examples-dir}/com/example/restassured/RequestParameters.java[tags=request-parameters-query-string]
685+
include::{examples-dir}/com/example/restassured/QueryParameters.java[tags=query-parameters]
687686
----
688-
<1> Configure Spring REST Docs to produce a snippet describing the request's parameters.
689-
Uses the static `requestParameters` method on `org.springframework.restdocs.request.RequestDocumentation`.
687+
<1> Configure Spring REST Docs to produce a snippet describing the request's query parameters.
688+
Uses the static `queryParameters` method on `org.springframework.restdocs.request.RequestDocumentation`.
690689
<2> Document the `page` parameter.
691690
Uses the static `parameterWithName` method on `org.springframework.restdocs.request.RequestDocumentation`.
692691
<3> Document the `per_page` parameter.
693692
<4> Perform a `GET` request with two parameters, `page` and `per_page`, in the query string.
694693

695-
You can also include request parameters as form data in the body of a POST request.
694+
When documenting query parameters, the test fails if an undocumented query parameter is used in the request's query string.
695+
Similarly, the test also fails if a documented query parameter is not found in the request's query string and the parameter has not been marked as optional.
696+
697+
If you do not want to document a query parameter, you can mark it as ignored.
698+
This prevents it from appearing in the generated snippet while avoiding the failure described above.
699+
700+
You can also document query parameters in a relaxed mode where any undocumented parameters do not cause a test failure.
701+
To do so, use the `relaxedQueryParameters` method on `org.springframework.restdocs.request.RequestDocumentation`.
702+
This can be useful when documenting a particular scenario where you only want to focus on a subset of the query parameters.
703+
704+
705+
706+
[[documenting-your-api-form-parameters]]
707+
=== Form Parameters
708+
709+
You can document a request's form parameters by using `formParameters`.
696710
The following examples show how to do so:
697711

698712
[source,java,indent=0,role="primary"]
699713
.MockMvc
700714
----
701-
include::{examples-dir}/com/example/mockmvc/RequestParameters.java[tags=request-parameters-form-data]
715+
include::{examples-dir}/com/example/mockmvc/FormParameters.java[tags=form-parameters]
702716
----
703-
<1> Perform a `POST` request with a single parameter, `username`.
717+
<1> Perform a `POST` request with a single form parameter, `username`.
718+
<2> Configure Spring REST Docs to produce a snippet describing the request's form parameters.
719+
Uses the static `formParameters` method on `org.springframework.restdocs.request.RequestDocumentation`.
720+
<3> Document the `username` parameter.
721+
Uses the static `parameterWithName` method on `org.springframework.restdocs.request.RequestDocumentation`.
704722

705723
[source,java,indent=0,role="secondary"]
706724
.WebTestClient
707725
----
708-
include::{examples-dir}/com/example/webtestclient/RequestParameters.java[tags=request-parameters-form-data]
726+
include::{examples-dir}/com/example/webtestclient/FormParameters.java[tags=form-parameters]
709727
----
710-
<1> Perform a `POST` request with a single parameter, `username`.
728+
<1> Perform a `POST` request with a single form parameter, `username`.
729+
<2> Configure Spring REST Docs to produce a snippet describing the request's form parameters.
730+
Uses the static `formParameters` method on `org.springframework.restdocs.request.RequestDocumentation`.
731+
<3> Document the `username` parameter.
732+
Uses the static `parameterWithName` method on `org.springframework.restdocs.request.RequestDocumentation`.
711733

712734
[source,java,indent=0,role="secondary"]
713735
.REST Assured
714736
----
715-
include::{examples-dir}/com/example/restassured/RequestParameters.java[tags=request-parameters-form-data]
737+
include::{examples-dir}/com/example/restassured/FormParameters.java[tags=form-parameters]
716738
----
717-
<1> Configure the `username` parameter.
718-
<2> Perform the `POST` request.
739+
<1> Configure Spring REST Docs to produce a snippet describing the request's form parameters.
740+
Uses the static `formParameters` method on `org.springframework.restdocs.request.RequestDocumentation`.
741+
<2> Document the `username` parameter.
742+
Uses the static `parameterWithName` method on `org.springframework.restdocs.request.RequestDocumentation`.
743+
<3> Perform a `POST` request with a single form parameter, `username`.
719744

720-
In all cases, the result is a snippet named `request-parameters.adoc` that contains a table describing the parameters that are supported by the resource.
745+
In all cases, the result is a snippet named `form-parameters.adoc` that contains a table describing the form parameters that are supported by the resource.
721746

722-
When documenting request parameters, the test fails if an undocumented request parameter is used in the request.
723-
Similarly, the test also fails if a documented request parameter is not found in the request and the request parameter has not been marked as optional.
747+
When documenting form parameters, the test fails if an undocumented form parameter is used in the request body.
748+
Similarly, the test also fails if a documented form parameter is not found in the request body and the form parameter has not been marked as optional.
724749

725-
If you do not want to document a request parameter, you can mark it as ignored.
750+
If you do not want to document a form parameter, you can mark it as ignored.
726751
This prevents it from appearing in the generated snippet while avoiding the failure described above.
727752

728-
You can also document request parameters in a relaxed mode where any undocumented parameters do not cause a test failure.
729-
To do so, use the `relaxedRequestParameters` method on `org.springframework.restdocs.request.RequestDocumentation`.
730-
This can be useful when documenting a particular scenario where you only want to focus on a subset of the request parameters.
753+
You can also document form parameters in a relaxed mode where any undocumented parameters do not cause a test failure.
754+
To do so, use the `relaxedFormParameters` method on `org.springframework.restdocs.request.RequestDocumentation`.
755+
This can be useful when documenting a particular scenario where you only want to focus on a subset of the form parameters.
731756

732757

733758

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2014-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.mockmvc;
18+
19+
import org.springframework.test.web.servlet.MockMvc;
20+
21+
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
22+
import static org.springframework.restdocs.request.RequestDocumentation.formParameters;
23+
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
24+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
25+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
26+
27+
public class FormParameters {
28+
29+
private MockMvc mockMvc;
30+
31+
public void postFormDataSnippet() throws Exception {
32+
// tag::form-parameters[]
33+
this.mockMvc.perform(post("/users").param("username", "Tester")) // <1>
34+
.andExpect(status().isCreated()).andDo(document("create-user", formParameters(// <2>
35+
parameterWithName("username").description("The user's username") // <3>
36+
)));
37+
// end::form-parameters[]
38+
}
39+
40+
}

Diff for: docs/src/test/java/com/example/mockmvc/RequestParameters.java renamed to docs/src/test/java/com/example/mockmvc/QueryParameters.java

+5-14
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,22 @@
2020

2121
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
2222
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
23-
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
2423
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
25-
import static org.springframework.restdocs.request.RequestDocumentation.requestParameters;
24+
import static org.springframework.restdocs.request.RequestDocumentation.queryParameters;
2625
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2726

28-
public class RequestParameters {
27+
public class QueryParameters {
2928

3029
private MockMvc mockMvc;
3130

3231
public void getQueryStringSnippet() throws Exception {
33-
// tag::request-parameters-query-string[]
32+
// tag::query-parameters[]
3433
this.mockMvc.perform(get("/users?page=2&per_page=100")) // <1>
35-
.andExpect(status().isOk()).andDo(document("users", requestParameters(// <2>
34+
.andExpect(status().isOk()).andDo(document("users", queryParameters(// <2>
3635
parameterWithName("page").description("The page to retrieve"), // <3>
3736
parameterWithName("per_page").description("Entries per page") // <4>
3837
)));
39-
// end::request-parameters-query-string[]
40-
}
41-
42-
public void postFormDataSnippet() throws Exception {
43-
// tag::request-parameters-form-data[]
44-
this.mockMvc.perform(post("/users").param("username", "Tester")) // <1>
45-
.andExpect(status().isCreated()).andDo(document("create-user",
46-
requestParameters(parameterWithName("username").description("The user's username"))));
47-
// end::request-parameters-form-data[]
38+
// end::query-parameters[]
4839
}
4940

5041
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2014-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.restassured;
18+
19+
import io.restassured.RestAssured;
20+
import io.restassured.specification.RequestSpecification;
21+
22+
import static org.hamcrest.CoreMatchers.is;
23+
import static org.springframework.restdocs.request.RequestDocumentation.formParameters;
24+
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
25+
import static org.springframework.restdocs.restassured.RestAssuredRestDocumentation.document;
26+
27+
public class FormParameters {
28+
29+
private RequestSpecification spec;
30+
31+
public void postFormDataSnippet() {
32+
// tag::form-parameters[]
33+
RestAssured.given(this.spec).filter(document("create-user", formParameters(// <1>
34+
parameterWithName("username").description("The user's username")))) // <2>
35+
.formParam("username", "Tester").when().post("/users") // <3>
36+
.then().assertThat().statusCode(is(200));
37+
// end::form-parameters[]
38+
}
39+
40+
}

Diff for: docs/src/test/java/com/example/restassured/RequestParameters.java renamed to docs/src/test/java/com/example/restassured/QueryParameters.java

+5-16
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,21 @@
2121

2222
import static org.hamcrest.CoreMatchers.is;
2323
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
24-
import static org.springframework.restdocs.request.RequestDocumentation.requestParameters;
24+
import static org.springframework.restdocs.request.RequestDocumentation.queryParameters;
2525
import static org.springframework.restdocs.restassured.RestAssuredRestDocumentation.document;
2626

27-
public class RequestParameters {
27+
public class QueryParameters {
2828

2929
private RequestSpecification spec;
3030

3131
public void getQueryStringSnippet() {
32-
// tag::request-parameters-query-string[]
33-
RestAssured.given(this.spec).filter(document("users", requestParameters(// <1>
32+
// tag::query-parameters[]
33+
RestAssured.given(this.spec).filter(document("users", queryParameters(// <1>
3434
parameterWithName("page").description("The page to retrieve"), // <2>
3535
parameterWithName("per_page").description("Entries per page")))) // <3>
3636
.when().get("/users?page=2&per_page=100") // <4>
3737
.then().assertThat().statusCode(is(200));
38-
// end::request-parameters-query-string[]
39-
}
40-
41-
public void postFormDataSnippet() {
42-
// tag::request-parameters-form-data[]
43-
RestAssured.given(this.spec)
44-
.filter(document("create-user",
45-
requestParameters(parameterWithName("username").description("The user's username"))))
46-
.formParam("username", "Tester") // <1>
47-
.when().post("/users") // <2>
48-
.then().assertThat().statusCode(is(200));
49-
// end::request-parameters-form-data[]
38+
// end::query-parameters[]
5039
}
5140

5241
}

Diff for: docs/src/test/java/com/example/webtestclient/RequestParameters.java renamed to docs/src/test/java/com/example/webtestclient/FormParameters.java

+8-19
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,26 @@
2121
import org.springframework.util.MultiValueMap;
2222
import org.springframework.web.reactive.function.BodyInserters;
2323

24+
import static org.springframework.restdocs.request.RequestDocumentation.formParameters;
2425
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
25-
import static org.springframework.restdocs.request.RequestDocumentation.requestParameters;
2626
import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.document;
2727

28-
public class RequestParameters {
28+
public class FormParameters {
2929

3030
// @formatter:off
3131

3232
private WebTestClient webTestClient;
3333

34-
public void getQueryStringSnippet() {
35-
// tag::request-parameters-query-string[]
36-
this.webTestClient.get().uri("/users?page=2&per_page=100") // <1>
37-
.exchange().expectStatus().isOk().expectBody()
38-
.consumeWith(document("users", requestParameters(// <2>
39-
parameterWithName("page").description("The page to retrieve"), // <3>
40-
parameterWithName("per_page").description("Entries per page") // <4>
41-
)));
42-
// end::request-parameters-query-string[]
43-
}
44-
4534
public void postFormDataSnippet() {
46-
// tag::request-parameters-form-data[]
35+
// tag::form-parameters[]
4736
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
4837
formData.add("username", "Tester");
4938
this.webTestClient.post().uri("/users").body(BodyInserters.fromFormData(formData)) // <1>
50-
.exchange().expectStatus().isCreated().expectBody()
51-
.consumeWith(document("create-user", requestParameters(
52-
parameterWithName("username").description("The user's username")
53-
)));
54-
// end::request-parameters-form-data[]
39+
.exchange().expectStatus().isCreated().expectBody()
40+
.consumeWith(document("create-user", formParameters(// <2>
41+
parameterWithName("username").description("The user's username") // <3>
42+
)));
43+
// end::form-parameters[]
5544
}
5645

5746
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2014-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.webtestclient;
18+
19+
import org.springframework.test.web.reactive.server.WebTestClient;
20+
21+
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
22+
import static org.springframework.restdocs.request.RequestDocumentation.queryParameters;
23+
import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.document;
24+
25+
public class QueryParameters {
26+
27+
// @formatter:off
28+
29+
private WebTestClient webTestClient;
30+
31+
public void getQueryStringSnippet() {
32+
// tag::query-parameters[]
33+
this.webTestClient.get().uri("/users?page=2&per_page=100") // <1>
34+
.exchange().expectStatus().isOk().expectBody()
35+
.consumeWith(document("users", queryParameters(// <2>
36+
parameterWithName("page").description("The page to retrieve"), // <3>
37+
parameterWithName("per_page").description("Entries per page") // <4>
38+
)));
39+
// end::query-parameters[]
40+
}
41+
42+
}

0 commit comments

Comments
 (0)