From 90aa3df523b5e2c8a0482261d761d419b5161e0b Mon Sep 17 00:00:00 2001 From: Jay Bryant Date: Wed, 6 Feb 2019 13:10:30 -0600 Subject: [PATCH 1/2] Editing pass I edited for spelling, punctuation, grammar, usage, and corporate voice. Andy is a pretty good writer, but I caught a few "Britishisms" and made things consistent with our whole family of docs. --- docs/src/docs/asciidoc/configuration.adoc | 99 +-- docs/src/docs/asciidoc/contributing.adoc | 11 +- .../customizing-requests-and-responses.adoc | 89 +-- .../docs/asciidoc/documenting-your-api.adoc | 668 ++++++++++-------- docs/src/docs/asciidoc/getting-started.adoc | 176 +++-- docs/src/docs/asciidoc/index.adoc | 4 +- docs/src/docs/asciidoc/introduction.adoc | 18 +- .../asciidoc/working-with-asciidoctor.adoc | 124 ++-- .../docs/asciidoc/working-with-markdown.adoc | 8 +- 9 files changed, 674 insertions(+), 523 deletions(-) diff --git a/docs/src/docs/asciidoc/configuration.adoc b/docs/src/docs/asciidoc/configuration.adoc index 9dad1faf3..8fb04bda3 100644 --- a/docs/src/docs/asciidoc/configuration.adoc +++ b/docs/src/docs/asciidoc/configuration.adoc @@ -1,17 +1,18 @@ [[configuration]] == Configuration - +This section covers how to configure Spring REST Docs. [[configuration-uris]] === Documented URIs - +This section covers configuring documented URIs. [[configuration-uris-mockmvc]] -==== MockMvc URI customization +==== MockMvc URI Customization -When using MockMvc, the default configuration for URIs documented by Spring REST Docs is: +When using MockMvc, the default configuration for URIs documented by Spring REST Docs is +as follows: |=== |Setting |Default @@ -27,15 +28,18 @@ When using MockMvc, the default configuration for URIs documented by Spring REST |=== This configuration is applied by `MockMvcRestDocumentationConfigurer`. You can use its API -to change one or more of the defaults to suit your needs: +to change one or more of the defaults to suit your needs. The following example shows how +to do so: +==== [source,java,indent=0] ---- include::{examples-dir}/com/example/mockmvc/CustomUriConfiguration.java[tags=custom-uri-configuration] ---- +==== NOTE: If the port is set to the default for the configured scheme (port 80 for HTTP or -port 443 for HTTPS), it will be omitted from any URIs in the generated snippets. +port 443 for HTTPS), it is omitted from any URIs in the generated snippets. TIP: To configure a request's context path, use the `contextPath` method on `MockHttpServletRequestBuilder`. @@ -43,37 +47,43 @@ TIP: To configure a request's context path, use the `contextPath` method on [[configuration-uris-rest-assured]] -==== REST Assured URI customization +==== REST Assured URI Customization REST Assured tests a service by making actual HTTP requests. As a result, URIs must be customized once the operation on the service has been performed but before it is -documented. A <> is provided for this purpose. +documented. A +<> is provided for this purpose. [[configuration-uris-webtestclient]] -==== WebTestClient URI customization +==== WebTestClient URI Customization When using WebTestClient, the default base for URIs documented by Spring REST -Docs is `http://localhost:8080`. This base can be customized using the +Docs is `http://localhost:8080`. You can customize this base by using the {spring-framework-api}/org/springframework/test/web/reactive/server/WebTestClient.Builder.html#baseUrl-java.lang.String-[ -`baseUrl(String)` method on `WebTestClient.Builder`]: +`baseUrl(String)` method on `WebTestClient.Builder`]. The following example shows how to +do so: +==== [source,java,indent=0] ---- include::{examples-dir}/com/example/webtestclient/CustomUriConfiguration.java[tags=custom-uri-configuration] ---- <1> Configure the base of documented URIs to be `https://api.example.com`. +==== [[configuration-snippet-encoding]] -=== Snippet encoding +=== Snippet Encoding -The default snippet encoding is `UTF-8`. You can change the default snippet encoding -using the `RestDocumentationConfigurer` API. For example, to use `ISO-8859-1`: +The default snippet encoding is `UTF-8`. You can change the default snippet encoding by +using the `RestDocumentationConfigurer` API. For example, the following examples use +`ISO-8859-1`: +==== [source,java,indent=0,role="primary"] .MockMvc ---- @@ -91,20 +101,23 @@ include::{examples-dir}/com/example/webtestclient/CustomEncoding.java[tags=custo ---- include::{examples-dir}/com/example/restassured/CustomEncoding.java[tags=custom-encoding] ---- +==== -TIP: When Spring REST Docs converts a request or response's content to a String, the -`charset` specified in the `Content-Type` header will be used if it is available. In its -absence, the JVM's default `Charset` will be used. The JVM's default `Charset` can be -configured using the `file.encoding` system property. +TIP: When Spring REST Docs converts the content of a request or a response to a `String`. +The `charset` specified in the `Content-Type` header is used if it is available. In its +absence, the JVM's default `Charset` is used. You can configure the JVM's default +`Charset` byusing the `file.encoding` system property. [[configuration-snippet-template-format]] -=== Snippet template format +=== Snippet Template Format The default snippet template format is Asciidoctor. Markdown is also supported out of the -box. You can change the default format using the `RestDocumentationConfigurer` API: +box. You can change the default format by using the `RestDocumentationConfigurer` API. +The following examples show how to do so: +==== [source,java,indent=0,role="primary"] .MockMvc ---- @@ -122,25 +135,27 @@ include::{examples-dir}/com/example/webtestclient/CustomFormat.java[tags=custom- ---- include::{examples-dir}/com/example/restassured/CustomFormat.java[tags=custom-format] ---- +==== [[configuration-default-snippets]] -=== Default snippets +=== Default Snippets Six snippets are produced by default: -- `curl-request` -- `http-request` -- `http-response` -- `httpie-request` -- `request-body` -- `response-body` +* `curl-request` +* `http-request` +* `http-response` +* `httpie-request` +* `request-body` +* `response-body` -You can change the default snippet configuration during setup using the -`RestDocumentationConfigurer` API. For example, to only produce the `curl-request` +You can change the default snippet configuration during setup by using the +`RestDocumentationConfigurer` API. The following examples produce only the `curl-request` snippet by default: +==== [source,java,indent=0,role="primary"] .MockMvc ---- @@ -158,35 +173,37 @@ include::{examples-dir}/com/example/webtestclient/CustomDefaultSnippets.java[tag ---- include::{examples-dir}/com/example/restassured/CustomDefaultSnippets.java[tags=custom-default-snippets] ---- +==== [[configuration-default-preprocessors]] -=== Default operation preprocessors +=== Default Operation Preprocessors -You can configure default request and response preprocessors during setup using the -`RestDocumentationConfigurer` API. For example, to remove the `Foo` headers from all requests -and pretty print all responses: +You can configure default request and response preprocessors during setup by using the +`RestDocumentationConfigurer` API. The following examples remove the `Foo` headers from +all requests and pretty print all responses: +==== [source,java,indent=0,role="primary"] .MockMvc ---- include::{examples-dir}/com/example/mockmvc/CustomDefaultOperationPreprocessors.java[tags=custom-default-operation-preprocessors] ---- -<1> Apply a request preprocessor that will remove the header named `Foo`. -<2> Apply a response preprocessor that will pretty print its content. +<1> Apply a request preprocessor that removes the header named `Foo`. +<2> Apply a response preprocessor that pretty prints its content. [source,java,indent=0,role="secondary"] .WebTestClient ---- include::{examples-dir}/com/example/webtestclient/CustomDefaultOperationPreprocessors.java[tags=custom-default-operation-preprocessors] ---- -<1> Apply a request preprocessor that will remove the header named `Foo`. -<2> Apply a response preprocessor that will pretty print its content. +<1> Apply a request preprocessor that removes the header named `Foo`. +<2> Apply a response preprocessor that pretty prints its content. [source,java,indent=0,role="secondary"] .REST Assured ---- include::{examples-dir}/com/example/restassured/CustomDefaultOperationPreprocessors.java[tags=custom-default-operation-preprocessors] ---- -<1> Apply a request preprocessor that will remove the header named `Foo`. -<2> Apply a response preprocessor that will pretty print its content. - +<1> Apply a request preprocessor that removes the header named `Foo`. +<2> Apply a response preprocessor that pretty prints its content. +==== diff --git a/docs/src/docs/asciidoc/contributing.adoc b/docs/src/docs/asciidoc/contributing.adoc index c5c36bff6..870b138d6 100644 --- a/docs/src/docs/asciidoc/contributing.adoc +++ b/docs/src/docs/asciidoc/contributing.adoc @@ -2,15 +2,16 @@ == Contributing Spring REST Docs is intended to make it easy for you to produce high-quality documentation -for your RESTful services. However, we can't achieve that goal without your contributions. +for your RESTful services. However, we cannot achieve that goal without your +contributions. [[contributing-questions]] === Questions -You can ask questions about Spring REST Docs on http://stackoverflow.com[StackOverflow] -using the `spring-restdocs` tag. Similarly, we encourage you to help your fellow +You can ask questions about Spring REST Docs on http://stackoverflow.com[Stack Overflow] +by using the `spring-restdocs` tag. Similarly, we encourage you to help your fellow Spring REST Docs users by answering questions. @@ -28,9 +29,9 @@ ideally, includes a test that reproduces it. [[contributing-enhancements]] === Enhancements -If you'd like an enhancement to be made to Spring REST Docs, pull requests are most +If you want an enhancement to be made to Spring REST Docs, pull requests are most welcome. The source code is on {github}[GitHub]. You may want to search the {github}/issues?q=is%3Aissue[existing issues] and {github}/pulls?q=is%3Apr[pull requests] -to see if the enhancement is already being worked on. You may also want to +to see if the enhancement has already been proprosed. You may also want to {github}/issues/new[open a new issue] to discuss a possible enhancement before work on it begins. diff --git a/docs/src/docs/asciidoc/customizing-requests-and-responses.adoc b/docs/src/docs/asciidoc/customizing-requests-and-responses.adoc index f47b9fd02..d2a3c09c8 100644 --- a/docs/src/docs/asciidoc/customizing-requests-and-responses.adoc +++ b/docs/src/docs/asciidoc/customizing-requests-and-responses.adoc @@ -3,68 +3,74 @@ There may be situations where you do not want to document a request exactly as it was sent or a response exactly as it was received. Spring REST Docs provides a number of -preprocessors that can be used to modify a request or response before it's documented. +preprocessors that can be used to modify a request or response before it is documented. -Preprocessing is configured by calling `document` with an `OperationRequestPreprocessor`, -and/or an `OperationResponsePreprocessor`. Instances can be obtained using the -static `preprocessRequest` and `preprocessResponse` methods on `Preprocessors`. For -example: +Preprocessing is configured by calling `document` with an `OperationRequestPreprocessor` +or an `OperationResponsePreprocessor`. You an obtain instances by using the +static `preprocessRequest` and `preprocessResponse` methods on `Preprocessors`. The +following examples show how to do so: +==== [source,java,indent=0,role="primary"] .MockMvc ---- include::{examples-dir}/com/example/mockmvc/PerTestPreprocessing.java[tags=preprocessing] ---- -<1> Apply a request preprocessor that will remove the header named `Foo`. -<2> Apply a response preprocessor that will pretty print its content. +<1> Apply a request preprocessor that removes the header named `Foo`. +<2> Apply a response preprocessor that pretty prints its content. [source,java,indent=0,role="secondary"] .WebTestClient ---- include::{examples-dir}/com/example/webtestclient/PerTestPreprocessing.java[tags=preprocessing] ---- -<1> Apply a request preprocessor that will remove the header named `Foo`. -<2> Apply a response preprocessor that will pretty print its content. +<1> Apply a request preprocessor that removes the header named `Foo`. +<2> Apply a response preprocessor that pretty prints its content. [source,java,indent=0,role="secondary"] .REST Assured ---- include::{examples-dir}/com/example/restassured/PerTestPreprocessing.java[tags=preprocessing] ---- -<1> Apply a request preprocessor that will remove the header named `Foo`. -<2> Apply a response preprocessor that will pretty print its content. +<1> Apply a request preprocessor that removes the header named `Foo`. +<2> Apply a response preprocessor that pretty prints its content. +==== Alternatively, you may want to apply the same preprocessors to every test. You can do -so by configuring the preprocessors using the `RestDocumentationConfigurer` API in your +so by configuring the preprocessors to use the `RestDocumentationConfigurer` API in your `@Before` method. For example to remove the `Foo` header from all requests and pretty print -all responses: +all responses, you could do one of the following (depending on your testing environment): +==== [source,java,indent=0,role="primary"] .MockMvc ---- include::{examples-dir}/com/example/mockmvc/EveryTestPreprocessing.java[tags=setup] ---- -<1> Apply a request preprocessor that will remove the header named `Foo`. -<2> Apply a response preprocessor that will pretty print its content. +<1> Apply a request preprocessor that removes the header named `Foo`. +<2> Apply a response preprocessor that pretty prints its content. [source,java,indent=0,role="secondary"] .WebTestClient ---- include::{examples-dir}/com/example/webtestclient/EveryTestPreprocessing.java[tags=setup] ---- -<1> Apply a request preprocessor that will remove the header named `Foo`. -<2> Apply a response preprocessor that will pretty print its content. +<1> Apply a request preprocessor that removes the header named `Foo`. +<2> Apply a response preprocessor that pretty prints its content. [source,java,indent=0,role="secondary"] .REST Assured ---- include::{examples-dir}/com/example/restassured/EveryTestPreprocessing.java[tags=setup] ---- -<1> Apply a request preprocessor that will remove the header named `Foo`. -<2> Apply a response preprocessor that will pretty print its content. +<1> Apply a request preprocessor that removes the header named `Foo`. +<2> Apply a response preprocessor that pretty prints its content. +==== -Then, in each test, any configuration specific to that test can be performed. For example: +Then, in each test, you can perform any configuration specific to that test. The +following examples show how to do so: +==== [source,java,indent=0,role="primary"] .MockMvc ---- @@ -82,9 +88,10 @@ include::{examples-dir}/com/example/webtestclient/EveryTestPreprocessing.java[ta ---- include::{examples-dir}/com/example/restassured/EveryTestPreprocessing.java[tags=use] ---- +==== -Various built in preprocessors, including those illustrated above, are available via the -static methods on `Preprocessors`. See <> for further details. +Various built-in preprocessors, including those illustrated above, are available through +the static methods on `Preprocessors`. See <> for further details. @@ -94,7 +101,7 @@ static methods on `Preprocessors`. See <> for further deta [[customizing-requests-and-responses-preprocessors-pretty-print]] -==== Pretty printing +==== Pretty Printing `prettyPrint` on `Preprocessors` formats the content of the request or response to make it easier to read. @@ -102,18 +109,18 @@ to make it easier to read. [[customizing-requests-and-responses-preprocessors-mask-links]] -==== Masking links +==== Masking Links -If you're documenting a Hypermedia-based API, you may want to encourage clients to -navigate the API using links rather than through the use of hard coded URIs. One way to do -this is to limit the use of URIs in the documentation. `maskLinks` on -`Preprocessors` replaces the `href` of any links in the response with `...`. A -different replacement can also be specified if you wish. +If you are documenting a hypermedia-based API, you may want to encourage clients to +navigate the API by using links rather than through the use of hard coded URIs. One way to +do so is to limit the use of URIs in the documentation. `maskLinks` on +`Preprocessors` replaces the `href` of any links in the response with `...`. You can also +specify a different replacement if you wish. [[customizing-requests-and-responses-preprocessors-remove-headers]] -==== Removing headers +==== Removing Headers `removeHeaders` on `Preprocessors` removes any headers from the request or response where the name is equal to any of the given header names. @@ -124,18 +131,18 @@ response where the name matches any of the given regular expression patterns. [[customizing-requests-and-responses-preprocessors-replace-patterns]] -==== Replacing patterns +==== Replacing Patterns `replacePattern` on `Preprocessors` provides a general purpose mechanism for -replacing content in a request or response. Any occurrences of a regular expression are -replaced. +replacing content in a request or response. Any occurrences that match a regular +expression are replaced. [[customizing-requests-and-responses-preprocessors-modify-request-parameters]] -==== Modifying request parameters +==== Modifying Request Parameters -`modifyParameters` on `Preprocessors` can be used to add, set, and remove request +You can use `modifyParameters` on `Preprocessors` to add, set, and remove request parameters. @@ -143,23 +150,23 @@ parameters. [[customizing-requests-and-responses-preprocessors-modify-uris]] ==== Modifying URIs -TIP: If you are using MockMvc or a WebTestClient that is not bound to a server, -URIs should be customized by <>. +TIP: If you use MockMvc or a WebTestClient that is not bound to a server, +you should customize URIs by <>. -`modifyUris` on `Preprocessors` can be used to modify any URIs in a request +You can use `modifyUris` on `Preprocessors` to modify any URIs in a request or a response. When using REST Assured or WebTestClient bound to a server, this -allows you to customize the URIs that appear in the documentation while testing a +lets you customize the URIs that appear in the documentation while testing a local instance of the service. [[customizing-requests-and-responses-preprocessors-writing-your-own]] -==== Writing your own preprocessor +==== Writing Your Own Preprocessor If one of the built-in preprocessors does not meet your needs, you can write your own by implementing the `OperationPreprocessor` interface. You can then use your custom preprocessor in exactly the same way as any of the built-in preprocessors. -If you only want to modify the content (body) of a request or response, consider +If you want to modify only the content (body) of a request or response, consider implementing the `ContentModifier` interface and using it with the built-in `ContentModifyingOperationPreprocessor`. diff --git a/docs/src/docs/asciidoc/documenting-your-api.adoc b/docs/src/docs/asciidoc/documenting-your-api.adoc index 770af56df..5092f81d1 100644 --- a/docs/src/docs/asciidoc/documenting-your-api.adoc +++ b/docs/src/docs/asciidoc/documenting-your-api.adoc @@ -9,8 +9,10 @@ This section provides more details about using Spring REST Docs to document your === Hypermedia Spring REST Docs provides support for documenting the links in a -https://en.wikipedia.org/wiki/HATEOAS[Hypermedia-based] API: +https://en.wikipedia.org/wiki/HATEOAS[hypermedia-based] API. +The following examples show how to use it: +==== [source,java,indent=0,role="primary"] .MockMvc ---- @@ -19,9 +21,9 @@ include::{examples-dir}/com/example/mockmvc/Hypermedia.java[tag=links] <1> Configure Spring REST docs to produce a snippet describing the response's links. Uses the static `links` method on `org.springframework.restdocs.hypermedia.HypermediaDocumentation`. -<2> Expect a link whose rel is `alpha`. Uses the static `linkWithRel` method on +<2> Expect a link whose `rel` is `alpha`. Uses the static `linkWithRel` method on `org.springframework.restdocs.hypermedia.HypermediaDocumentation`. -<3> Expect a link whose rel is `bravo`. +<3> Expect a link whose `rel` is `bravo`. [source,java,indent=0,role="secondary"] .WebTestClient @@ -31,9 +33,9 @@ include::{examples-dir}/com/example/webtestclient/Hypermedia.java[tag=links] <1> Configure Spring REST docs to produce a snippet describing the response's links. Uses the static `links` method on `org.springframework.restdocs.hypermedia.HypermediaDocumentation`. -<2> Expect a link whose rel is `alpha`. Uses the static `linkWithRel` method on +<2> Expect a link whose `rel` is `alpha`. Uses the static `linkWithRel` method on `org.springframework.restdocs.hypermedia.HypermediaDocumentation`. -<3> Expect a link whose rel is `bravo`. +<3> Expect a link whose `rel` is `bravo`. [source,java,indent=0,role="secondary"] .REST Assured @@ -43,25 +45,26 @@ include::{examples-dir}/com/example/restassured/Hypermedia.java[tag=links] <1> Configure Spring REST docs to produce a snippet describing the response's links. Uses the static `links` method on `org.springframework.restdocs.hypermedia.HypermediaDocumentation`. -<2> Expect a link whose rel is `alpha`. Uses the static `linkWithRel` method on +<2> Expect a link whose `rel` is `alpha`. Uses the static `linkWithRel` method on `org.springframework.restdocs.hypermedia.HypermediaDocumentation`. -<3> Expect a link whose rel is `bravo`. +<3> Expect a link whose `rel` is `bravo`. +==== The result is a snippet named `links.adoc` that contains a table describing the resource's links. -TIP: If a link in the response has a `title`, the description can be omitted from its -descriptor and the `title` will be used. If you omit the description and the link does -not have a `title` a failure will occur. +TIP: If a link in the response has a `title`, you can omit the description from its +descriptor and the `title` is used. If you omit the description and the link does +not have a `title`, a failure occurs. -When documenting links, the test will fail if an undocumented link is found in the -response. Similarly, the test will also fail if a documented link is not found in the +When documenting links, the test fails if an undocumented link is found in the +response. Similarly, the test also fails if a documented link is not found in the response and the link has not been marked as optional. -If you do not want to document a link, you can mark it as ignored. This will prevent it +If you do not want to document a link, you can mark it as ignored. Doing so prevents it from appearing in the generated snippet while avoiding the failure described above. -Links can also be documented in a relaxed mode where any undocumented links will not cause +You can also document links in a relaxed mode, where any undocumented links do not cause a test failure. To do so, use the `relaxedLinks` method on `org.springframework.restdocs.hypermedia.HypermediaDocumentation`. This can be useful when documenting a particular scenario where you only want to focus on a subset of the links. @@ -69,18 +72,20 @@ documenting a particular scenario where you only want to focus on a subset of th [[documenting-your-api-hypermedia-link-formats]] -==== Hypermedia link formats +==== Hypermedia Link Formats Two link formats are understood by default: - * Atom – links are expected to be in an array named `links`. Used by default when the - content type of the response is compatible with `application/json`. - * HAL – links are expected to be in a map named `_links`. Used by default when the - content type of the response is compatible with `application/hal+json`. +* Atom: Links are expected to be in an array named `links`. This is used by default when + the content type of the response is compatible with `application/json`. +* HAL: Links are expected to be in a map named `_links`. This is used by default when the + content type of the response is compatible with `application/hal+json`. -If you are using Atom or HAL-format links but with a different content type you can -provide one of the built-in `LinkExtractor` implementations to `links`. For example: +If you use Atom- or HAL-format links but with a different content type, you can +provide one of the built-in `LinkExtractor` implementations to `links`. The following +examples show how to do so: +==== [source,java,indent=0,role="primary"] .MockMvc ---- @@ -104,6 +109,7 @@ include::{examples-dir}/com/example/restassured/Hypermedia.java[tag=explicit-ext ---- <1> Indicate that the links are in HAL format. Uses the static `halLinks` method on `org.springframework.restdocs.hypermedia.HypermediaDocumentation`. +==== If your API represents its links in a format other than Atom or HAL, you can provide your own implementation of the `LinkExtractor` interface to extract the links from the @@ -112,42 +118,46 @@ response. [[documenting-your-api-hypermedia-ignoring-common-links]] -==== Ignoring common links +==== Ignoring Common Links Rather than documenting links that are common to every response, such as `self` and `curies` when using HAL, you may want to document them once in an overview section and then ignore them in the rest of your API's documentation. To do so, you can build on the <> to add link -descriptors to a snippet that's preconfigured to ignore certain links. For example: +descriptors to a snippet that is preconfigured to ignore certain links. The following +example shows how to do so: +==== [source,java,indent=0] ---- include::{examples-dir}/com/example/Hypermedia.java[tags=ignore-links] ---- +==== [[documenting-your-api-request-response-payloads]] -=== Request and response payloads +=== Request and Response Payloads In addition to the hypermedia-specific support <>, support for general documentation of request and response payloads is also +earlier>>, support for general documentation of request and response payloads is also provided. -By default, Spring REST Docs will automatically generate snippets for the body of the +By default, Spring REST Docs automatically generates snippets for the body of the request and the body of the response. These snippets are named `request-body.adoc` and `response-body.adoc` respectively. [[documenting-your-api-request-response-payloads-fields]] -==== Request and response fields +==== Request and Response Fields To provide more detailed documentation of a request or response payload, support for documenting the payload's fields is provided. Consider the following payload: +==== [source,json,indent=0] ---- { @@ -157,16 +167,18 @@ Consider the following payload: } } ---- +==== -Its fields can be documented like this: +You can document the previous example's fields as follows: +==== [source,java,indent=0,role="primary"] .MockMvc ---- include::{examples-dir}/com/example/mockmvc/Payload.java[tags=response] ---- <1> Configure Spring REST docs to produce a snippet describing the fields in the response - payload. To document a request `requestFields` can be used. Both are static methods on + payload. To document a request, you can use `requestFields`. Both are static methods on `org.springframework.restdocs.payload.PayloadDocumentation`. <2> Expect a field with the path `contact.email`. Uses the static `fieldWithPath` method on `org.springframework.restdocs.payload.PayloadDocumentation`. @@ -178,7 +190,7 @@ include::{examples-dir}/com/example/mockmvc/Payload.java[tags=response] include::{examples-dir}/com/example/webtestclient/Payload.java[tags=response] ---- <1> Configure Spring REST docs to produce a snippet describing the fields in the response - payload. To document a request `requestFields` can be used. Both are static methods on + payload. To document a request, you can use `requestFields`. Both are static methods on `org.springframework.restdocs.payload.PayloadDocumentation`. <2> Expect a field with the path `contact.email`. Uses the static `fieldWithPath` method on `org.springframework.restdocs.payload.PayloadDocumentation`. @@ -190,23 +202,25 @@ include::{examples-dir}/com/example/webtestclient/Payload.java[tags=response] include::{examples-dir}/com/example/restassured/Payload.java[tags=response] ---- <1> Configure Spring REST docs to produce a snippet describing the fields in the response - payload. To document a request `requestFields` can be used. Both are static methods on + payload. To document a request, you can use `requestFields`. Both are static methods on `org.springframework.restdocs.payload.PayloadDocumentation`. <2> Expect a field with the path `contact.email`. Uses the static `fieldWithPath` method on `org.springframework.restdocs.payload.PayloadDocumentation`. <3> Expect a field with the path `contact.name`. +==== -The result is a snippet that contains a table describing the fields. For requests this -snippet is named `request-fields.adoc`. For responses this snippet is named +The result is a snippet that contains a table describing the fields. For requests, this +snippet is named `request-fields.adoc`. For responses, this snippet is named `response-fields.adoc`. -When documenting fields, the test will fail if an undocumented field is found in the -payload. Similarly, the test will also fail if a documented field is not found in the +When documenting fields, the test fails if an undocumented field is found in the +payload. Similarly, the test also fails if a documented field is not found in the payload and the field has not been marked as optional. -If you don't want to provide detailed documentation for all of the fields, an entire -subsection of a payload can be documented. For example: +If you do not want to provide detailed documentation for all of the fields, an entire +subsection of a payload can be documented. The following examples show how to do so: +==== [source,java,indent=0,role="primary"] .MockMvc ---- @@ -233,40 +247,44 @@ include::{examples-dir}/com/example/restassured/Payload.java[tags=subsection] <1> Document the subsection with the path `contact`. `contact.email` and `contact.name` are now seen as having also been documented. Uses the static `subsectionWithPath` method on `org.springframework.restdocs.payload.PayloadDocumentation`. +==== `subsectionWithPath` can be useful for providing a high-level overview of a particular -section of a payload. Separate, more detailed documentation for a subsection can then -<>. +section of a payload. You can then produce separate, more detailed documentation for a +subsection. See <>. If you do not want to document a field or subsection at all, you can mark it as ignored. -This will prevent it from appearing in the generated snippet while avoiding the failure -described above. +This prevents it from appearing in the generated snippet while avoiding the failure +described earlier. -Fields can also be documented in a relaxed mode where any undocumented fields will not +You can also document fields in a relaxed mode, where any undocumented fields do not cause a test failure. To do so, use the `relaxedRequestFields` and `relaxedResponseFields` methods on `org.springframework.restdocs.payload.PayloadDocumentation`. This can be useful when -documenting a particular scenario where you only want to focus on a subset of the payload. +documenting a particular scenario where you want to focus only on a subset of the payload. -TIP: By default, Spring REST Docs will assume that the payload you are documenting is -JSON. If you want to document an XML payload the content type of the request or response +TIP: By default, Spring REST Docs assume that the payload you are documenting is +JSON. If you want to document an XML payload, the content type of the request or response must be compatible with `application/xml`. [[documenting-your-api-request-response-payloads-fields-json]] -===== Fields in JSON payloads +===== Fields in JSON Payloads + +This section descries how to work with fields in JSON payloads. [[documenting-your-api-request-response-payloads-fields-json-field-paths]] -====== JSON field paths +====== JSON Field Paths JSON field paths use either dot notation or bracket notation. Dot notation uses '.' to -separate each key in the path; `a.b`, for example. Bracket notation wraps each key in -square brackets and single quotes; `['a']['b']`, for example. In either case, `[]` is used -to identify an array. Dot notation is more concise, but using bracket notation enables the -use of `.` within a key name; `['a.b']`, for example. The two different notations can be -used in the same path; `a['b']`, for example. +separate each key in the path (for example, `a.b`). Bracket notation wraps each key in +square brackets and single quotatuon marks; `['a']['b']`, for example. In either case, +`[]` is used to identify an array. Dot notation is more concise, but using bracket +notation enables the use of `.` within a key name (for example, `['a.b']`). The two +different notations can be used in the same path (for example, `a['b']`). -With this JSON payload: +Consider the following JSON payload: +==== [source,json,indent=0] ---- { @@ -286,8 +304,9 @@ With this JSON payload: } } ---- +==== -The following paths are all present: +In the preceding JSON payload, the following paths are all present: [cols="1,3"] |=== @@ -325,11 +344,12 @@ The following paths are all present: |=== -A payload that uses an array at its root can also be documented. The path `[]` will refer +You can also document a payload that uses an array at its root. The path `[]` refers to the entire array. You can then use bracket or dot notation to identify fields within the array's entries. For example, `[].id` corresponds to the `id` field of every object found in the following array: +==== [source,json,indent=0] ---- [ @@ -341,10 +361,12 @@ found in the following array: } ] ---- +==== You can use `\*` as a wildcard to match fields with different names. For example, `users.*.role` could be used to document the role of every user in the following JSON: +==== [source,json,indent=0] ---- { @@ -358,44 +380,46 @@ You can use `\*` as a wildcard to match fields with different names. For example } } ---- +==== [[documenting-your-api-request-response-payloads-fields-json-field-types]] -====== JSON field types +====== JSON Field Types -When a field is documented, Spring REST Docs will attempt to determine its type by +When a field is documented, Spring REST Docs tries to determine its type by examining the payload. Seven different types are supported: [cols="1,3"] |=== | Type | Description -| array -| The value of each occurrence of the field is an array +| `array` +| The value of each occurrence of the field is an array. -| boolean -| The value of each occurrence of the field is a boolean (`true` or `false`) +| `boolean` +| The value of each occurrence of the field is a boolean (`true` or `false`). -| object -| The value of each occurrence of the field is an object +| `object` +| The value of each occurrence of the field is an object. -| number -| The value of each occurrence of the field is a number +| `number` +| The value of each occurrence of the field is a number. -| null -| The value of each occurrence of the field is `null` +| `null` +| The value of each occurrence of the field is `null`. -| string -| The value of each occurrence of the field is a string +| `string` +| The value of each occurrence of the field is a string. -| varies -| The field occurs multiple times in the payload with a variety of different types +| `varies` +| The field occurs multiple times in the payload with a variety of different types. |=== -The type can also be set explicitly using the `type(Object)` method on -`FieldDescriptor`. The result of the supplied ``Object``'s `toString` method will be used -in the documentation. Typically, one of the values enumerated by `JsonFieldType` will be -used: +You can also explicitly set the type by using the `type(Object)` method on +`FieldDescriptor`. The result of the supplied `toString` method of the `Object` is used +in the documentation. Typically, one of the values enumerated by `JsonFieldType` is +used. The following example show how to do so: +==== [source,java,indent=0,role="primary"] .MockMvc ---- @@ -416,37 +440,41 @@ include::{examples-dir}/com/example/webtestclient/Payload.java[tags=explicit-typ include::{examples-dir}/com/example/restassured/Payload.java[tags=explicit-type] ---- <1> Set the field's type to `String`. +==== [[documenting-your-api-request-response-payloads-fields-xml]] ===== XML payloads +This section describes how to work with XML payloads. + [[documenting-your-api-request-response-payloads-fields-xml-field-paths]] -====== XML field paths +====== XML Field Paths XML field paths are described using XPath. `/` is used to descend into a child node. [[documenting-your-api-request-response-payloads-fields-xml-field-types]] -====== XML field types +====== XML Field Types -When documenting an XML payload, you must provide a type for the field using the +When documenting an XML payload, you must provide a type for the field by using the `type(Object)` method on `FieldDescriptor`. The result of the supplied type's `toString` -method will be used in the documentation. +method is used in the documentation. [[documenting-your-api-request-response-payloads-fields-reusing-field-descriptors]] -===== Reusing field descriptors +===== Reusing Field Descriptors In addition to the general support for <>, the request and response snippets allow additional descriptors to be -configured with a path prefix. This allows the descriptors for a repeated portion of a -request or response payload to be created once and then reused. +snippets>>, the request and response snippets let additional descriptors be +configured with a path prefix. This lets the descriptors for a repeated portion of a +request or response payload be created once and then reused. Consider an endpoint that returns a book: +==== [source,json,indent=0] ---- { @@ -454,11 +482,13 @@ Consider an endpoint that returns a book: "author": "Jane Austen" } ---- +==== -The paths for `title` and `author` are simply `title` and `author` respectively. +The paths for `title` and `author` are `title` and `author`, respectively. Now consider an endpoint that returns an array of books: +==== [source,json,indent=0] ---- [{ @@ -470,79 +500,87 @@ Now consider an endpoint that returns an array of books: "author": "Harper Lee" }] ---- +==== -The paths for `title` and `author` are `[].title` and `[].author` respectively. The only +The paths for `title` and `author` are `[].title` and `[].author`, respectively. The only difference between the single book and the array of books is that the fields' paths now have a `[].` prefix. -The descriptors that document a book can be created: +You can create the descriptors that document a book as follows: +==== [source,java,indent=0] ---- include::{examples-dir}/com/example/Payload.java[tags=book-descriptors] ---- +==== -They can then be used to document a single book: +You can then use them to document a single book, as follows: +==== [source,java,indent=0,role="primary"] .MockMvc ---- include::{examples-dir}/com/example/mockmvc/Payload.java[tags=single-book] ---- -<1> Document `title` and `author` using existing descriptors +<1> Document `title` and `author` by using existing descriptors [source,java,indent=0,role="secondary"] .WebTestClient ---- include::{examples-dir}/com/example/webtestclient/Payload.java[tags=single-book] ---- -<1> Document `title` and `author` using existing descriptors +<1> Document `title` and `author` by using existing descriptors [source,java,indent=0,role="secondary"] .REST Assured ---- include::{examples-dir}/com/example/restassured/Payload.java[tags=single-book] ---- -<1> Document `title` and `author` using existing descriptors +<1> Document `title` and `author` by using existing descriptors +==== -And an array of books: +You can also use the descriptors to document an array of books, as follows: +==== [source,java,indent=0,role="primary"] .MockMvc ---- include::{examples-dir}/com/example/mockmvc/Payload.java[tags=book-array] ---- -<1> Document the array -<2> Document `[].title` and `[].author` using the existing descriptors prefixed with `[].` +<1> Document the array. +<2> Document `[].title` and `[].author` by using the existing descriptors prefixed with `[].` [source,java,indent=0,role="secondary"] .WebTestClient ---- include::{examples-dir}/com/example/webtestclient/Payload.java[tags=book-array] ---- -<1> Document the array -<2> Document `[].title` and `[].author` using the existing descriptors prefixed with `[].` +<1> Document the array. +<2> Document `[].title` and `[].author` by using the existing descriptors prefixed with `[].` [source,java,indent=0,role="secondary"] .REST Assured ---- include::{examples-dir}/com/example/restassured/Payload.java[tags=book-array] ---- -<1> Document the array -<2> Document `[].title` and `[].author` using the existing descriptors prefixed with `[].` +<1> Document the array. +<2> Document `[].title` and `[].author` by using the existing descriptors prefixed with `[].` +==== [[documenting-your-api-request-response-payloads-subsections]] -==== Documenting a subsection of a request or response payload +==== Documenting a Subsection of a Request or Response Payload If a payload is large or structurally complex, it can be useful to document -individual sections of the payload. REST Docs allows you to do so by extracting a +individual sections of the payload. REST Docs let you do so by extracting a subsection of the payload and then documenting it. [[documenting-your-api-request-response-payloads-subsections-body]] -===== Documenting a subsection of a request or response body +===== Documenting a Subsection of a Request or Response Body Consider the following JSON response body: +==== [source,json,indent=0] ---- { @@ -558,9 +596,11 @@ Consider the following JSON response body: } } ---- +==== -A snippet that documents the `temperature` object can be produces as follows: +You can produce a snippet that documents the `temperature` object as follows: +==== [source,java,indent=0,role="primary"] .MockMvc ---- @@ -569,7 +609,7 @@ include::{examples-dir}/com/example/mockmvc/Payload.java[tags=body-subsection] <1> Produce a snippet containing a subsection of the response body. Uses the static `responseBody` and `beneathPath` methods on `org.springframework.restdocs.payload.PayloadDocumentation`. To produce a snippet - for the request body, `requestBody` can be used in place of `responseBody`. + for the request body, you can use `requestBody` in place of `responseBody`. [source,java,indent=0,role="secondary"] .WebTestClient @@ -579,7 +619,7 @@ include::{examples-dir}/com/example/webtestclient/Payload.java[tags=body-subsect <1> Produce a snippet containing a subsection of the response body. Uses the static `responseBody` and `beneathPath` methods on `org.springframework.restdocs.payload.PayloadDocumentation`. To produce a snippet - for the request body, `requestBody` can be used in place of `responseBody`. + for the request body, you can use `requestBody` in place of `responseBody`. [source,java,indent=0,role="secondary"] .REST Assured @@ -589,10 +629,12 @@ include::{examples-dir}/com/example/restassured/Payload.java[tags=body-subsectio <1> Produce a snippet containing a subsection of the response body. Uses the static `responseBody` and `beneathPath` methods on `org.springframework.restdocs.payload.PayloadDocumentation`. To produce a snippet - for the request body, `requestBody` can be used in place of `responseBody`. + for the request body, you can use `requestBody` in place of `responseBody`. +==== The result is a snippet with the following contents: +==== [source,json,indent=0] ---- { @@ -602,28 +644,32 @@ The result is a snippet with the following contents: } } ---- +==== To make the snippet's name distinct, an identifier for the subsection is included. By -default, this identifier is `beneath-${path}`. For example, the code above will result in -a snippet named `response-body-beneath-weather.temperature.adoc`. The identifier can -be customized using the `withSubsectionId(String)` method: +default, this identifier is `beneath-${path}`. For example, the precding code results in +a snippet named `response-body-beneath-weather.temperature.adoc`. You can customized the +identifier by using the `withSubsectionId(String)` method, as follows: +==== [source,java,indent=0] ---- include::{examples-dir}/com/example/Payload.java[tags=custom-subsection-id] ---- +==== -This example will result in a snippet named `request-body-temp.adoc`. +The result is a snippet named `request-body-temp.adoc`. [[documenting-your-api-request-response-payloads-subsections-fields]] -===== Documenting the fields of a subsection of a request or response +===== Documenting the Fields of a Subsection of a Request or Response -As well as documenting a subsection of a request or response body, it's also possible to -document the fields in a particular subsection. A snippet that documents the fields of -the `temperature` object (`high` and `low`) can be produced as follows: +As well as documenting a subsection of a request or response body, you can also +document the fields in a particular subsection. You can produce a snippet that documents +the fields of the `temperature` object (`high` and `low`) as follows: +==== [source,java,indent=0,role="primary"] .MockMvc ---- @@ -653,27 +699,30 @@ include::{examples-dir}/com/example/restassured/Payload.java[tags=fields-subsect beneath the path `weather.temperature`. Uses the static `beneathPath` method on `org.springframework.restdocs.payload.PayloadDocumentation`. <2> Document the `high` and `low` fields. +==== The result is a snippet that contains a table describing the `high` and `low` fields of `weather.temperature`. To make the snippet's name distinct, an identifier for the subsection is included. By default, this identifier is `beneath-${path}`. For -example, the code above will result in a snippet named +example, the preceding code results in a snippet named `response-fields-beneath-weather.temperature.adoc`. [[documenting-your-api-request-parameters]] -=== Request parameters +=== Request Parameters -A request's parameters can be documented using `requestParameters`. Request parameters -can be included in a `GET` request's query string. For example: +You can document a request's parameters by using `requestParameters`. You can include +request parameters in a `GET` request's query string. The following examples show how to +do so: +==== [source,java,indent=0,role="primary"] .MockMvc ---- include::{examples-dir}/com/example/mockmvc/RequestParameters.java[tags=request-parameters-query-string] ---- -<1> Perform a `GET` request with two parameters, `page` and `per_page` in the query +<1> Perform a `GET` request with two parameters, `page` and `per_page`, in the query string. <2> Configure Spring REST Docs to produce a snippet describing the request's parameters. Uses the static `requestParameters` method on @@ -687,7 +736,7 @@ include::{examples-dir}/com/example/mockmvc/RequestParameters.java[tags=request- ---- include::{examples-dir}/com/example/webtestclient/RequestParameters.java[tags=request-parameters-query-string] ---- -<1> Perform a `GET` request with two parameters, `page` and `per_page` in the query +<1> Perform a `GET` request with two parameters, `page` and `per_page`, in the query string. <2> Configure Spring REST Docs to produce a snippet describing the request's parameters. Uses the static `requestParameters` method on @@ -707,11 +756,14 @@ include::{examples-dir}/com/example/restassured/RequestParameters.java[tags=requ <2> Document the `page` parameter. Uses the static `parameterWithName` method on `org.springframework.restdocs.request.RequestDocumentation`. <3> Document the `per_page` parameter. -<4> Perform a `GET` request with two parameters, `page` and `per_page` in the query +<4> Perform a `GET` request with two parameters, `page` and `per_page`, in the query string. +==== -Request parameters can also be included as form data in the body of a POST request: +You can also include request parameters as form data in the body of a POST request. +The following examples show how to do so: +==== [source,java,indent=0,role="primary"] .MockMvc ---- @@ -733,21 +785,22 @@ include::{examples-dir}/com/example/restassured/RequestParameters.java[tags=requ ---- <1> Configure the `username` parameter. <2> Perform the `POST` request. +==== -In both cases, the result is a snippet named `request-parameters.adoc` that contains a +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. -When documenting request parameters, the test will fail if an undocumented request -parameter is used in the request. Similarly, the test will also fail if a documented +When documenting request parameters, the test fails if an undocumented request +parameter is used in the request. 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. -If you do not want to document a request parameter, you can mark it as ignored. This will -prevent it from appearing in the generated snippet while avoiding the failure described +If you do not want to document a request parameter, you can mark it as ignored. This +prevents it from appearing in the generated snippet while avoiding the failure described above. -Request parameters can also be documented in a relaxed mode where any undocumented -parameters will not cause a test failure. To do so, use the `relaxedRequestParameters` +You can also document request parameters in a relaxed mode where any undocumented +parameters do not cause a test failure. To do so, use the `relaxedRequestParameters` method on `org.springframework.restdocs.request.RequestDocumentation`. This can be useful when documenting a particular scenario where you only want to focus on a subset of the request parameters. @@ -755,10 +808,12 @@ request parameters. [[documenting-your-api-path-parameters]] -=== Path parameters +=== Path Parameters -A request's path parameters can be documented using `pathParameters`. For example: +You can document a request's path parameters by using `pathParameters`. The following +examples show how to do so: +==== [source,java,indent=0,role="primary"] .MockMvc ---- @@ -797,35 +852,38 @@ include::{examples-dir}/com/example/restassured/PathParameters.java[tags=path-pa `org.springframework.restdocs.request.RequestDocumentation`. <3> Document the parameter named `longitude`. <4> Perform a `GET` request with two path parameters, `latitude` and `longitude`. +==== The result is a snippet named `path-parameters.adoc` that contains a table describing the path parameters that are supported by the resource. -TIP: If you are using MockMvc then, to make the path parameters available for documentation, -the request must be built using one of the methods on `RestDocumentationRequestBuilders` -rather than `MockMvcRequestBuilders`. +TIP: If you use MockMvc, to make the path parameters available for documentation, +you must build the request by using one of the methods on +`RestDocumentationRequestBuilders` rather than `MockMvcRequestBuilders`. -When documenting path parameters, the test will fail if an undocumented path parameter -is used in the request. Similarly, the test will also fail if a documented path parameter +When documenting path parameters, the test fails if an undocumented path parameter +is used in the request. Similarly, the test also fails if a documented path parameter is not found in the request and the path parameter has not been marked as optional. -Path parameters can also be documented in a relaxed mode where any undocumented -parameters will not cause a test failure. To do so, use the `relaxedPathParameters` +You can also document path parameters in a relaxed mode, where any undocumented +parameters do not cause a test failure. To do so, use the `relaxedPathParameters` method on `org.springframework.restdocs.request.RequestDocumentation`. This can be useful when documenting a particular scenario where you only want to focus on a subset of the path parameters. -If you do not want to document a path parameter, you can mark it as ignored. This will -prevent it from appearing in the generated snippet while avoiding the failure described -above. +If you do not want to document a path parameter, you can mark it as ignored. Doing so +prevents it from appearing in the generated snippet while avoiding the failure described +earlier. [[documenting-your-api-request-parts]] -=== Request parts +=== Request Parts -The parts of a multipart request can be documenting using `requestParts`. For example: +You can use `requestParts` to document the parts of a multipart request. The following +example shows how to do so: +==== [source,java,indent=0,role="primary"] .MockMvc ---- @@ -862,48 +920,49 @@ include::{examples-dir}/com/example/restassured/RequestParts.java[tags=request-p `org.springframework.restdocs.request.RequestDocumentation`. <3> Configure the request with the part named `file`. <4> Perform the `POST` request to `/upload`. +==== The result is a snippet named `request-parts.adoc` that contains a table describing the request parts that are supported by the resource. -When documenting request parts, the test will fail if an undocumented part is used in the -request. Similarly, the test will also fail if a documented part is not found in the +When documenting request parts, the test fails if an undocumented part is used in the +request. Similarly, the test also fails if a documented part is not found in the request and the part has not been marked as optional. -Request parts can also be documented in a relaxed mode where any undocumented -parts will not cause a test failure. To do so, use the `relaxedRequestParts` method on +You can also document Request parts in a relaxed mode where any undocumented +parts do not cause a test failure. To do so, use the `relaxedRequestParts` method on `org.springframework.restdocs.request.RequestDocumentation`. This can be useful when documenting a particular scenario where you only want to focus on a subset of the request parts. -If you do not want to document a request part, you can mark it as ignored. This will -prevent it from appearing in the generated snippet while avoiding the failure described -above. +If you do not want to document a request part, you can mark it as ignored. This +prevents it from appearing in the generated snippet while avoiding the failure described +earlier. [[documenting-your-api-request-parts-payloads]] -=== Request part payloads +=== Request Part Payloads -The payload of a request part can be documented in much the same way as the -<> with support +You can document the payload of a request part in much the same way as the +<>, with support for documenting a request part's body and its fields. [[documenting-your-api-request-parts-payloads-body]] -==== Documenting a request part's body +==== Documenting a Request Part's Body -A snippet containing the body of a request part can be generated: +You can generate a snippet containing the body of a request part as follows: +==== [source,java,indent=0,role="primary"] .MockMvc ---- include::{examples-dir}/com/example/mockmvc/RequestPartPayload.java[tags=body] ---- <1> Configure Spring REST docs to produce a snippet containing the body of the - of the request part named `metadata`. Uses the static `requestPartBody` method on + request part named `metadata`. Uses the static `requestPartBody` method on `PayloadDocumentation`. - payload. [source,java,indent=0,role="secondary"] .WebTestClient @@ -911,9 +970,8 @@ include::{examples-dir}/com/example/mockmvc/RequestPartPayload.java[tags=body] include::{examples-dir}/com/example/webtestclient/RequestPartPayload.java[tags=body] ---- <1> Configure Spring REST docs to produce a snippet containing the body of the - of the request part named `metadata`. Uses the static `requestPartBody` method on + request part named `metadata`. Uses the static `requestPartBody` method on `PayloadDocumentation`. - payload. [source,java,indent=0,role="secondary"] .REST Assured @@ -923,19 +981,21 @@ include::{examples-dir}/com/example/restassured/RequestPartPayload.java[tags=bod <1> Configure Spring REST docs to produce a snippet containing the body of the request part named `metadata`. Uses the static `requestPartBody` method on `PayloadDocumentation`. +==== -The result is a snippet `request-part-${part-name}-body.adoc` that contains the part's -body. For example, documenting a part named `metadata` will produce a snippet named +The result is a snippet named `request-part-${part-name}-body.adoc` that contains the +part's body. For example, documenting a part named `metadata` produces a snippet named `request-part-metadata-body.adoc`. [[documenting-your-api-request-parts-payloads-fields]] -==== Documenting a request part's fields +==== Documenting a Request Part's Fields -A request part's fields can be documented in much the same way as the fields of a request -or response: +You can document a request part's fields in much the same way as the fields of a request +or response, as follows: +==== [source,java,indent=0,role="primary"] .MockMvc ---- @@ -944,7 +1004,6 @@ include::{examples-dir}/com/example/mockmvc/RequestPartPayload.java[tags=fields] <1> Configure Spring REST docs to produce a snippet describing the fields in the payload of the request part named `metadata`. Uses the static `requestPartFields` method on `PayloadDocumentation`. - payload. <2> Expect a field with the path `version`. Uses the static `fieldWithPath` method on `org.springframework.restdocs.payload.PayloadDocumentation`. @@ -956,7 +1015,6 @@ include::{examples-dir}/com/example/webtestclient/RequestPartPayload.java[tags=f <1> Configure Spring REST docs to produce a snippet describing the fields in the payload of the request part named `metadata`. Uses the static `requestPartFields` method on `PayloadDocumentation`. - payload. <2> Expect a field with the path `version`. Uses the static `fieldWithPath` method on `org.springframework.restdocs.payload.PayloadDocumentation`. @@ -970,43 +1028,45 @@ include::{examples-dir}/com/example/restassured/RequestPartPayload.java[tags=fie `PayloadDocumentation`. <2> Expect a field with the path `version`. Uses the static `fieldWithPath` method on `org.springframework.restdocs.payload.PayloadDocumentation`. +==== The result is a snippet that contains a table describing the part's fields. This snippet is named `request-part-${part-name}-fields.adoc`. For example, documenting a part named -`metadata` will produce a snippet named `request-part-metadata-fields.adoc`. +`metadata` produces a snippet named `request-part-metadata-fields.adoc`. -When documenting fields, the test will fail if an undocumented field is found in the -payload of the part. Similarly, the test will also fail if a documented field is not found +When documenting fields, the test fails if an undocumented field is found in the +payload of the part. Similarly, the test also fails if a documented field is not found in the payload of the part and the field has not been marked as optional. For payloads with a hierarchical structure, documenting a field is sufficient for all of its descendants to also be treated as having been documented. -If you do not want to document a field, you can mark it as ignored. This will prevent it +If you do not want to document a field, you can mark it as ignored. Doing so prevents it from appearing in the generated snippet while avoiding the failure described above. -Fields can also be documented in a relaxed mode where any undocumented fields will not +You can also document fields in a relaxed mode, where any undocumented fields do not cause a test failure. To do so, use the `relaxedRequestPartFields` method on `org.springframework.restdocs.payload.PayloadDocumentation`. This can be useful when documenting a particular scenario where you only want to focus on a subset of the payload of the part. For further information on describing fields, documenting payloads that use XML, -and more please refer to the +and more, see the <>. [[documenting-your-api-http-headers]] -=== HTTP headers +=== HTTP Headers -The headers in a request or response can be documented using `requestHeaders` and -`responseHeaders` respectively. For example: +You can document the headers in a request or response by using `requestHeaders` and +`responseHeaders`, respectively. The following examples show how to do so: +==== [source,java,indent=0,role="primary"] .MockMvc ---- include::{examples-dir}/com/example/mockmvc/HttpHeaders.java[tags=headers] ---- -<1> Perform a `GET` request with an `Authorization` header that uses basic authentication +<1> Perform a `GET` request with an `Authorization` header that uses basic authentication. <2> Configure Spring REST Docs to produce a snippet describing the request's headers. Uses the static `requestHeaders` method on `org.springframework.restdocs.headers.HeaderDocumentation`. @@ -1020,7 +1080,7 @@ include::{examples-dir}/com/example/mockmvc/HttpHeaders.java[tags=headers] ---- include::{examples-dir}/com/example/webtestclient/HttpHeaders.java[tags=headers] ---- -<1> Perform a `GET` request with an `Authorization` header that uses basic authentication +<1> Perform a `GET` request with an `Authorization` header that uses basic authentication. <2> Configure Spring REST Docs to produce a snippet describing the request's headers. Uses the static `requestHeaders` method on `org.springframework.restdocs.headers.HeaderDocumentation`. @@ -1041,39 +1101,44 @@ include::{examples-dir}/com/example/restassured/HttpHeaders.java[tags=headers] `org.springframework.restdocs.headers.HeaderDocumentation. <3> Produce a snippet describing the response's headers. Uses the static `responseHeaders` method on `org.springframework.restdocs.headers.HeaderDocumentation`. -<4> Configure the request with an `Authorization` header that uses basic authentication +<4> Configure the request with an `Authorization` header that uses basic authentication. +==== The result is a snippet named `request-headers.adoc` and a snippet named `response-headers.adoc`. Each contains a table describing the headers. -When documenting HTTP Headers, the test will fail if a documented header is not found in +When documenting HTTP Headers, the test fails if a documented header is not found in the request or response. [[documenting-your-api-reusing-snippets]] -=== Reusing snippets +=== Reusing Snippets -It's common for an API that's being documented to have some features that are common -across several of its resources. To avoid repetition when documenting such resources a -`Snippet` configured with the common elements can be reused. +It is common for an API that is being documented to have some features that are common +across several of its resources. To avoid repetition when documenting such resources, you' +can reuse a `Snippet` configured with the common elements. -First, create the `Snippet` that describes the common elements. For example: +First, create the `Snippet` that describes the common elements. The following example +shows how to do so: +==== [source,java,indent=0] ---- include::{examples-dir}/com/example/SnippetReuse.java[tags=field] ---- +==== -Second, use this snippet and add further descriptors that are resource-specific. For -example: +Second, use this snippet and add further descriptors that are resource-specific. The +following examples show how to do so: +==== [source,java,indent=0,role="primary"] .MockMvc ---- include::{examples-dir}/com/example/mockmvc/MockMvcSnippetReuse.java[tags=use] ---- -<1> Reuse the `pagingLinks` `Snippet` calling `and` to add descriptors that are specific +<1> Reuse the `pagingLinks` `Snippet`, calling `and` to add descriptors that are specific to the resource that is being documented. [source,java,indent=0,role="secondary"] @@ -1081,7 +1146,7 @@ include::{examples-dir}/com/example/mockmvc/MockMvcSnippetReuse.java[tags=use] ---- include::{examples-dir}/com/example/webtestclient/WebTestClientSnippetReuse.java[tags=use] ---- -<1> Reuse the `pagingLinks` `Snippet` calling `and` to add descriptors that are specific +<1> Reuse the `pagingLinks` `Snippet`, calling `and` to add descriptors that are specific to the resource that is being documented. [source,java,indent=0,role="secondary"] @@ -1089,28 +1154,31 @@ include::{examples-dir}/com/example/webtestclient/WebTestClientSnippetReuse.java ---- include::{examples-dir}/com/example/restassured/RestAssuredSnippetReuse.java[tags=use] ---- -<1> Reuse the `pagingLinks` `Snippet` calling `and` to add descriptors that are specific +<1> Reuse the `pagingLinks` `Snippet`, calling `and` to add descriptors that are specific to the resource that is being documented. +==== -The result of the example is that links with the rels `first`, `last`, `next`, `previous`, -`alpha`, and `bravo` are all documented. +The result of the example is that links with `rel` values of `first`, `last`, `next`, +`previous`, `alpha`, and `bravo` are all documented. [[documenting-your-api-constraints]] -=== Documenting constraints +=== Documenting Constraints Spring REST Docs provides a number of classes that can help you to document constraints. -An instance of `ConstraintDescriptions` can be used to access descriptions of a class's -constraints. For example: +You can use an instance of `ConstraintDescriptions` to access descriptions of a class's +constraints. The following example shows how to do so: +==== [source,java,indent=0] ---- include::{examples-dir}/com/example/Constraints.java[tags=constraints] ---- -<1> Create an instance of `ConstraintDescriptions` for the `UserInput` class -<2> Get the descriptions of the name property's constraints. This list will contain two - descriptions; one for the `NotNull` constraint and one for the `Size` constraint. +<1> Create an instance of `ConstraintDescriptions` for the `UserInput` class. +<2> Get the descriptions of the `name` property's constraints. This list contains two + descriptions: one for the `NotNull` constraint and one for the `Size` constraint. +==== The {samples}/rest-notes-spring-hateoas/src/test/java/com/example/notes/ApiDocumentation.java[`ApiDocumentation`] class in the Spring HATEOAS sample shows this functionality in action. @@ -1118,88 +1186,88 @@ class in the Spring HATEOAS sample shows this functionality in action. [[documenting-your-api-constraints-finding]] -==== Finding constraints +==== Finding Constraints -By default, constraints are found using a Bean Validation `Validator`. Currently, only -property constraints are supported. You can customize the `Validator` that's used by +By default, constraints are found by using a Bean Validation `Validator`. Currently, only +property constraints are supported. You can customize the `Validator` that is used by creating `ConstraintDescriptions` with a custom `ValidatorConstraintResolver` instance. -To take complete control of constraint resolution, your own implementation of -`ConstraintResolver` can be used. +To take complete control of constraint resolution, you can use your own implementation of +`ConstraintResolver`. [[documenting-your-api-constraints-describing]] -==== Describing constraints +==== Describing Constraints Default descriptions are provided for all of Bean Validation 2.0's constraints: -* AssertFalse -* AssertTrue -* DecimalMax -* DecimalMin -* Digits -* Email -* Future -* FutureOrPresent -* Max -* Min -* Negative -* NegativeOrZero -* NotBlank -* NotEmpty -* NotNull -* Null -* Past -* PastOrPresent -* Pattern -* Positive -* PositiveOrZero -* Size +* `AssertFalse` +* `AssertTrue` +* `DecimalMax` +* `DecimalMin` +* `Digits` +* `Email` +* `Future` +* `FutureOrPresent` +* `Max` +* `Min` +* `Negative` +* `NegativeOrZero` +* `NotBlank` +* `NotEmpty` +* `NotNull` +* `Null` +* `Past` +* `PastOrPresent` +* `Pattern` +* `Positive` +* `PositiveOrZero` +* `Size` Default descriptions are also provided for the following constraints from Hibernate Validator: -* CodePointLength -* CreditCardNumber -* Currency -* EAN -* Email -* Length -* LuhnCheck -* Mod10Check -* Mod11Check -* NotBlank -* NotEmpty -* Currency -* Range -* SafeHtml -* URL - -To override the default descriptions, or to provide a new description, create a resource -bundle with the base name +* `CodePointLength` +* `CreditCardNumber` +* `Currency` +* `EAN` +* `Email` +* `Length` +* `LuhnCheck` +* `Mod10Check` +* `Mod11Check` +* `NotBlank` +* `NotEmpty` +* `Currency` +* `Range` +* `SafeHtml` +* `URL` + +To override the default descriptions or to provide a new description, you can create a +resource bundle with a base name of `org.springframework.restdocs.constraints.ConstraintDescriptions`. The Spring HATEOAS-based sample contains {samples}/rest-notes-spring-hateoas/src/test/resources/org/springframework/restdocs/constraints/ConstraintDescriptions.properties[an example of such a resource bundle]. -Each key in the resource bundle is the fully-qualified name of a constraint plus +Each key in the resource bundle is the fully-qualified name of a constraint plus a `.description`. For example, the key for the standard `@NotNull` constraint is `javax.validation.constraints.NotNull.description`. -Property placeholder's referring to a constraint's attributes can be used in its +You can use a property placeholder referring to a constraint's attributes in its description. For example, the default description of the `@Min` constraint, `Must be at least ${value}`, refers to the constraint's `value` attribute. -To take more control of constraint description resolution, create `ConstraintDescriptions` -with a custom `ResourceBundleConstraintDescriptionResolver`. To take complete control, -create `ConstraintDescriptions` with a custom `ConstraintDescriptionResolver` -implementation. +To take more control of constraint description resolution, you can create +`ConstraintDescriptions` with a custom `ResourceBundleConstraintDescriptionResolver`. To +take complete control, you can create `ConstraintDescriptions` with a custom +`ConstraintDescriptionResolver` implementation. -==== Using constraint descriptions in generated snippets +==== Using Constraint Descriptions in Generated Snippets -Once you have a constraint's descriptions, you're free to use them however you like in +Once you have a constraint's descriptions, you are free to use them however you like in the generated snippets. For example, you may want to include the constraint descriptions as part of a field's description. Alternatively, you could include the constraints as <> in @@ -1210,7 +1278,7 @@ class in the Spring HATEOAS-based sample illustrates the latter approach. [[documenting-your-api-default-snippets]] -=== Default snippets +=== Default Snippets A number of snippets are produced automatically when you document a request and response. @@ -1220,37 +1288,37 @@ A number of snippets are produced automatically when you document a request and | `curl-request.adoc` | Contains the http://curl.haxx.se[`curl`] command that is equivalent to the `MockMvc` -call that is being documented +call that is being documented. | `httpie-request.adoc` | Contains the http://httpie.org[`HTTPie`] command that is equivalent to the `MockMvc` -call that is being documented +call that is being documented. | `http-request.adoc` | Contains the HTTP request that is equivalent to the `MockMvc` call that is being -documented +documented. | `http-response.adoc` -| Contains the HTTP response that was returned +| Contains the HTTP response that was returned. | `request-body.adoc` -| Contains the body of the request that was sent +| Contains the body of the request that was sent. | `response-body.adoc` -| Contains the body of the response that was returned +| Contains the body of the response that was returned. |=== -You can configure which snippets are produced by default. Please refer to the +You can configure which snippets are produced by default. See the <> for more information. [[documentating-your-api-parameterized-output-directories]] -=== Using parameterized output directories +=== Using Parameterized Output Directories -When using MockMvc or REST Assured, the output directory used by `document` can be -parameterized. The output directory cannot be parameterized when using WebTestClient. +When using MockMvc or REST Assured, you can parameterize the output directory used by +`document`. You cannot parameterize the output directory when using `WebTestClient`. The following parameters are supported: @@ -1259,35 +1327,36 @@ The following parameters are supported: | Parameter | Description | {methodName} -| The unmodified name of the test method +| The unmodified name of the test method. | {method-name} -| The name of the test method, formatted using kebab-case +| The name of the test method, formatted using kebab-case. | {method_name} -| The name of the test method, formatted using snake_case +| The name of the test method, formatted using snake_case. | {ClassName} -| The unmodified simple name of the test class +| The unmodified simple name of the test class. | {class-name} -| The simple name of the test class, formatted using kebab-case +| The simple name of the test class, formatted using kebab-case. | {class_name} -| The simple name of the test class, formatted using snake_case +| The simple name of the test class, formatted using snake_case. | {step} -| The count of calls made to the service in the current test +| The count of calls made to the service in the current test. |=== For example, `document("{class-name}/{method-name}")` in a test method named -`creatingANote` on the test class `GettingStartedDocumentation`, will write +`creatingANote` on the test class `GettingStartedDocumentation` writes snippets into a directory named `getting-started-documentation/creating-a-note`. -A parameterized output directory is particularly useful in combination with an `@Before` -method. It allows documentation to be configured once in a setup method and then reused -in every test in the class: +A parameterized output directory is particularly useful in combination with a `@Before` +method. It lets documentation be configured once in a setup method and then reused +in every test in the class. The following examples show how to do so: +==== [source,java,indent=0,role="primary"] .MockMvc ---- @@ -1299,8 +1368,9 @@ include::{examples-dir}/com/example/mockmvc/ParameterizedOutput.java[tags=parame ---- include::{examples-dir}/com/example/restassured/ParameterizedOutput.java[tags=parameterized-output] ---- +==== -With this configuration in place, every call to the service you are testing will produce +With this configuration in place, every call to the service you are testing produces the <> without any further configuration. Take a look at the `GettingStartedDocumentation` classes in each of the sample applications to see this functionality in action. @@ -1308,12 +1378,12 @@ sample applications to see this functionality in action. [[documenting-your-api-customizing]] -=== Customizing the output - +=== Customizing the Output +This section describes how to customize the output of Spring REST Docs. [[documenting-your-api-customizing-snippets]] -==== Customizing the generated snippets +==== Customizing the Generated Snippets Spring REST Docs uses https://mustache.github.io[Mustache] templates to produce the generated snippets. {source}/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates[Default @@ -1322,9 +1392,9 @@ customize a snippet's content, you can provide your own template. Templates are loaded from the classpath from an `org.springframework.restdocs.templates` subpackage. The name of the subpackage is determined by the ID of the template format -that is in use. The default template format, Asciidoctor, has the ID `asciidoctor` so +that is in use. The default template format, Asciidoctor, has an ID of `asciidoctor`, so snippets are loaded from `org.springframework.restdocs.templates.asciidoctor`. Each -template is named after the snippet that it will produce. For example, to +template is named after the snippet that it produces. For example, to override the template for the `curl-request.adoc` snippet, create a template named `curl-request.snippet` in `src/test/resources/org/springframework/restdocs/templates/asciidoctor`. @@ -1332,53 +1402,57 @@ override the template for the `curl-request.adoc` snippet, create a template nam [[documenting-your-api-customizing-including-extra-information]] -==== Including extra information +==== Including Extra Information There are two ways to provide extra information for inclusion in a generated snippet: -. Use the `attributes` method on a descriptor to add one or more attributes to it. -. Pass in some attributes when calling `curlRequest`, `httpRequest`, `httpResponse`, etc. - Such attributes will be associated with the snippet as a whole. +* Use the `attributes` method on a descriptor to add one or more attributes to it. +* Pass in some attributes when calling `curlRequest`, `httpRequest`, `httpResponse`, and + so on. Such attributes are associated with the snippet as a whole. Any additional attributes are made available during the template rendering process. Coupled with a custom snippet template, this makes it possible to include extra information in a generated snippet. -A concrete example of the above is the addition of a constraints column and a title when +A concrete example is the addition of a constraints column and a title when documenting request fields. The first step is to provide a `constraints` attribute for -each field that you are documenting and to provide a `title` attribute: +each field that you document and to provide a `title` attribute. The following examples +show how to do so: +==== [source,java,indent=0,role="primary"] .MockMvc ---- include::{examples-dir}/com/example/mockmvc/Payload.java[tags=constraints] ---- -<1> Configure the `title` attribute for the request fields snippet -<2> Set the `constraints` attribute for the `name` field -<3> Set the `constraints` attribute for the `email` field +<1> Configure the `title` attribute for the request fields snippet. +<2> Set the `constraints` attribute for the `name` field. +<3> Set the `constraints` attribute for the `email` field. [source,java,indent=0,role="secondary"] .WebTestClient ---- include::{examples-dir}/com/example/webtestclient/Payload.java[tags=constraints] ---- -<1> Configure the `title` attribute for the request fields snippet -<2> Set the `constraints` attribute for the `name` field -<3> Set the `constraints` attribute for the `email` field +<1> Configure the `title` attribute for the request fields snippet. +<2> Set the `constraints` attribute for the `name` field. +<3> Set the `constraints` attribute for the `email` field. [source,java,indent=0,role="secondary"] .REST Assured ---- include::{examples-dir}/com/example/restassured/Payload.java[tags=constraints] ---- -<1> Configure the `title` attribute for the request fields snippet -<2> Set the `constraints` attribute for the `name` field -<3> Set the `constraints` attribute for the `email` field +<1> Configure the `title` attribute for the request fields snippet. +<2> Set the `constraints` attribute for the `name` field. +<3> Set the `constraints` attribute for the `email` field. +==== The second step is to provide a custom template named `request-fields.snippet` that includes the information about the fields' constraints in the generated snippet's table -and adds a title: +and adds a title. The following example shows how to do so: +==== [source,indent=0] ---- .{{title}} <1> @@ -1394,7 +1468,7 @@ and adds a title: {{/fields}} |=== ---- -<1> Add a title to the table -<2> Add a new column named "Constraints" -<3> Include the descriptors' `constraints` attribute in each row of the table - +<1> Add a title to the table. +<2> Add a new column named "Constraints". +<3> Include the descriptors' `constraints` attribute in each row of the table. +==== diff --git a/docs/src/docs/asciidoc/getting-started.adoc b/docs/src/docs/asciidoc/getting-started.adoc index 66af165e0..97da3fb9d 100644 --- a/docs/src/docs/asciidoc/getting-started.adoc +++ b/docs/src/docs/asciidoc/getting-started.adoc @@ -6,11 +6,10 @@ This section describes how to get started with Spring REST Docs. [[getting-started-sample-applications]] -=== Sample applications +=== Sample Applications If you want to jump straight in, a number of sample applications are available: - [cols="3,2,10"] .MockMvc |=== @@ -19,12 +18,12 @@ If you want to jump straight in, a number of sample applications are available: | {samples}/rest-notes-spring-data-rest[Spring Data REST] | Maven | Demonstrates the creation of a getting started guide and an API guide for a service - implemented using http://projects.spring.io/spring-data-rest/[Spring Data REST]. + implemented by using http://projects.spring.io/spring-data-rest/[Spring Data REST]. | {samples}/rest-notes-spring-hateoas[Spring HATEOAS] | Gradle | Demonstrates the creation of a getting started guide and an API guide for a service - implemented using http://projects.spring.io/spring-hateoas/[Spring HATEOAS]. + implemented by using http://projects.spring.io/spring-hateoas/[Spring HATEOAS]. |=== @@ -82,13 +81,10 @@ If you want to jump straight in, a number of sample applications are available: Spring REST Docs has the following minimum requirements: -- Java 8 -- Spring Framework 5 (5.0.2 or later) - -Additionally, the `spring-restdocs-restassured` module has the following minimum -requirements: +* Java 8 +* Spring Framework 5 (5.0.2 or later) -- REST Assured 3.0 +Additionally, the `spring-restdocs-restassured` module requires REST Assured 3.0. [[getting-started-build-configuration]] === Build configuration @@ -96,9 +92,10 @@ requirements: The first step in using Spring REST Docs is to configure your project's build. The {samples}/rest-notes-spring-hateoas[Spring HATEOAS] and {samples}/rest-notes-spring-data-rest[Spring Data REST] samples contain a `build.gradle` -and `pom.xml` respectively that you may wish to use as a reference. The key parts of -the configuration are described below. +and `pom.xml`, respectively, that you may wish to use as a reference. The key parts of +the configuration are described in the following listings: +==== [source,xml,indent=0,subs="verbatim,attributes",role="primary"] .Maven ---- @@ -189,18 +186,21 @@ the configuration are described below. <7> Configure the snippets directory as an input. <8> Make the task depend on the test task so that the tests are run before the documentation is created. - +==== [[getting-started-build-configuration-packaging-the-documentation]] -==== Packaging the documentation +==== Packaging the Documentation -You may want to package the generated documentation in your project's jar file, for -example to have it {spring-boot-docs}/#boot-features-spring-mvc-static-content[served as +You may want to package the generated documentation in your project's jar file -- for +example, to have it {spring-boot-docs}/#boot-features-spring-mvc-static-content[served as static content] by Spring Boot. To do so, configure your project's build so that: 1. The documentation is generated before the jar is built 2. The generated documentation is included in the jar +The following listings show how to do so in both Maven and Gradle: + +==== [source,xml,indent=0,role="primary"] .Maven ---- @@ -254,14 +254,15 @@ from where it will be included in the jar file. ---- <1> Ensure that the documentation has been generated before the jar is built. <2> Copy the generated documentation into the jar's `static/docs` directory. +==== [[getting-started-documentation-snippets]] -=== Generating documentation snippets +=== Generating Documentation Snippets Spring REST Docs uses Spring MVC's {spring-framework-docs}/testing.html#spring-mvc-test-framework[test framework], -Spring WebFlux's {spring-framework-docs}/testing.html#webtestclient[`WebTestClient`] or +Spring WebFlux's {spring-framework-docs}/testing.html#webtestclient[`WebTestClient`], or http://www.rest-assured.io[REST Assured] to make requests to the service that you are documenting. It then produces documentation snippets for the request and the resulting response. @@ -269,25 +270,26 @@ response. [[getting-started-documentation-snippets-setup]] -==== Setting up your tests +==== Setting up Your Tests -Exactly how you setup your tests depends on the test framework that you're using. +Exactly how you set up your tests depends on the test framework that you use. Spring REST Docs provides first-class support for JUnit 4 and JUnit 5. Other frameworks, -such as TestNG, are also supported although slightly more setup is required. +such as TestNG, are also supported, although slightly more setup is required. [[getting-started-documentation-snippets-setup-junit]] -===== Setting up your JUnit 4 tests +===== Setting up Your JUnit 4 Tests When using JUnit 4, the first step in generating documentation snippets is to declare a -`public` `JUnitRestDocumentation` field that's annotated as a JUnit `@Rule`. - +`public` `JUnitRestDocumentation` field that is annotated as a JUnit `@Rule`. +The following example shows how to do so: +==== [source,java,indent=0] ---- @Rule public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation(); ---- - +==== By default, the `JUnitRestDocumentation` rule is automatically configured with an output directory based on your project's build tool: @@ -304,24 +306,29 @@ directory based on your project's build tool: |=== -The default can be overridden by providing an output directory when creating the -`JUnitRestDocumentation` instance: +You can override the default by providing an output directory when you create the +`JUnitRestDocumentation` instance. +The following example shows how to do so: +==== [source,java,indent=0] ---- @Rule public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("custom"); ---- +==== -Next, provide an `@Before` method to configure MockMvc, WebTestClient or REST Assured: +Next, you can provide an `@Before` method to configure MockMvc, WebTestClient or REST +Assured. The following examples show how to do so: +==== [source,java,indent=0,role="primary"] .MockMvc ---- include::{examples-dir}/com/example/mockmvc/ExampleApplicationTests.java[tags=setup] ---- -<1> The `MockMvc` instance is configured using a `MockMvcRestDocumentationConfigurer`. An -instance of this class can be obtained from the static `documentationConfiguration()` +<1> The `MockMvc` instance is configured by using a `MockMvcRestDocumentationConfigurer`. +You can obtain an instance of this class from the static `documentationConfiguration()` method on `org.springframework.restdocs.mockmvc.MockMvcRestDocumentation`. [source,java,indent=0,role="secondary"] @@ -330,8 +337,8 @@ method on `org.springframework.restdocs.mockmvc.MockMvcRestDocumentation`. include::{examples-dir}/com/example/webtestclient/ExampleApplicationTests.java[tags=setup] ---- <1> The `WebTestClient` instance is configured by adding a -`WebTestclientRestDocumentationConfigurer` as an `ExchangeFilterFunction`. An instance of -this class can be obtained from the static `documentationConfiguration()` method on +`WebTestclientRestDocumentationConfigurer` as an `ExchangeFilterFunction`. You can obtain +an instance of this class from the static `documentationConfiguration()` method on `org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation`. [source,java,indent=0,role="secondary"] @@ -340,34 +347,40 @@ this class can be obtained from the static `documentationConfiguration()` method include::{examples-dir}/com/example/restassured/ExampleApplicationTests.java[tags=setup] ---- <1> REST Assured is configured by adding a `RestAssuredRestDocumentationConfigurer` as a -`Filter`. An instance of this class can be obtained from the static +`Filter`. You can obtain an instance of this class from the static `documentationConfiguration()` method on `RestAssuredRestDocumentation` in the `org.springframework.restdocs.restassured3` package. +==== The configurer applies sensible defaults and also provides an API for customizing the -configuration. Refer to the <> for more information. +configuration. See the <> for more information. [[getting-started-documentation-snippets-setup-junit-5]] -===== Setting up your JUnit 5 tests +===== Setting up Your JUnit 5 Tests When using JUnit 5, the first step in generating documentation snippets is to apply -the `RestDocumentationExtension` to your test class: +the `RestDocumentationExtension` to your test class. +The following example shows how to do so: +==== [source,java,indent=0] ---- @ExtendWith(RestDocumentationExtension.class) public class JUnit5ExampleTests { ---- +==== -For testing a typical Spring application the `SpringExtension` should also be applied: +When testing a typical Spring application, you should also apply the `SpringExtension`: +==== [source,java,indent=0] ---- @ExtendWith({RestDocumentationExtension.class, SpringExtension.class}) public class JUnit5ExampleTests { ---- +==== The `RestDocumentationExtension` is automatically configured with an output directory based on your project's build tool: @@ -386,15 +399,17 @@ based on your project's build tool: -Next, provide a `@BeforeEach` method to configure MockMvc, WebTestClient, or REST Assured: +Next, you can provide a `@BeforeEach` method to configure MockMvc, WebTestClient, or REST +Assured. The following listings show how to do so: +==== [source,java,indent=0,role="primary"] .MockMvc ---- include::{examples-dir}/com/example/mockmvc/ExampleApplicationJUnit5Tests.java[tags=setup] ---- -<1> The `MockMvc` instance is configured using a `MockMvcRestDocumentationConfigurer`. An -instance of this class can be obtained from the static `documentationConfiguration()` +<1> The `MockMvc` instance is configured by using a `MockMvcRestDocumentationConfigurer`. +You can obtain an instance of this class from the static `documentationConfiguration()` method on `org.springframework.restdocs.mockmvc.MockMvcRestDocumentation`. [source,java,indent=0,role="secondary"] @@ -403,8 +418,8 @@ method on `org.springframework.restdocs.mockmvc.MockMvcRestDocumentation`. include::{examples-dir}/com/example/webtestclient/ExampleApplicationJUnit5Tests.java[tags=setup] ---- <1> The `WebTestClient` instance is configured by adding a -`WebTestClientRestDocumentationConfigurer` as an `ExchangeFilterFunction`. An instance of -this class can be obtained from the static `documentationConfiguration()` method on +`WebTestClientRestDocumentationConfigurer` as an `ExchangeFilterFunction`. You can obtain +an instance of this class from the static `documentationConfiguration()` method on `org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation`. [source,java,indent=0,role="secondary"] @@ -413,12 +428,13 @@ this class can be obtained from the static `documentationConfiguration()` method include::{examples-dir}/com/example/restassured/ExampleApplicationJUnit5Tests.java[tags=setup] ---- <1> REST Assured is configured by adding a `RestAssuredRestDocumentationConfigurer` as a -`Filter`. An instance of this class can be obtained from the static +`Filter`. You can obtain an instance of this class from the static `documentationConfiguration()` method on `RestAssuredRestDocumentation` in the `org.springframework.restdocs.restassured3` package. +==== The configurer applies sensible defaults and also provides an API for customizing the -configuration. Refer to the <> for more information. +configuration. See the <> for more information. @@ -429,18 +445,23 @@ The configuration when JUnit is not being used is largely similar to when it is used. This section describes the key differences. The {samples}/testng[TestNG sample] also illustrates the approach. -The first difference is that `ManualRestDocumentation` should be used in place of -`JUnitRestDocumentation` and there's no need for the `@Rule` annotation: +The first difference is that you should use `ManualRestDocumentation` in place of +`JUnitRestDocumentation`. Also, you do not need the `@Rule` annotation. +the following example shows hos to use `ManualRestDocumentation`: +==== [source,java,indent=0] ---- private ManualRestDocumentation restDocumentation = new ManualRestDocumentation(); ---- +==== -Secondly, `ManualRestDocumentation.beforeTest(Class, String)` -must be called before each test. This can be done as part of the method that is -configuring MockMvc, WebTestClient, or REST Assured: +Secondly, you must call `ManualRestDocumentation.beforeTest(Class, String)` +before each test. You can do so as part of the method that +configures MockMvc, WebTestClient, or REST Assured. +The following examples show how to do so: +==== [source,java,indent=0,role="primary"] .MockMvc ---- @@ -458,21 +479,25 @@ include::{examples-dir}/com/example/webtestclient/ExampleApplicationTestNgTests. ---- include::{examples-dir}/com/example/restassured/ExampleApplicationTestNgTests.java[tags=setup] ---- +==== -Lastly, `ManualRestDocumentation.afterTest` must be called after each test. For example, -with TestNG: +Finally, you must call `ManualRestDocumentation.afterTest` after each test. +The following example shows how to do so with TestNG: +==== [source,java,indent=0] ---- include::{examples-dir}/com/example/restassured/ExampleApplicationTestNgTests.java[tags=teardown] ---- +==== [[getting-started-documentation-snippets-invoking-the-service]] -==== Invoking the RESTful service +==== Invoking the RESTful Service -Now that the testing framework has been configured, it can be used to invoke the RESTful -service and document the request and response. For example: +Now that you have configured the testing framework, you can use it to invoke the RESTful +service and document the request and response. The following examples show how to do so: +==== [source,java,indent=0,role="primary"] .MockMvc ---- @@ -482,8 +507,8 @@ include::{examples-dir}/com/example/mockmvc/InvokeService.java[tags=invoke-servi is required. <2> Assert that the service produced the expected response. <3> Document the call to the service, writing the snippets into a directory named `index` -that will be located beneath the configured output directory. The snippets are written by -a `RestDocumentationResultHandler`. An instance of this class can be obtained from the +(which is located beneath the configured output directory). The snippets are written by +a `RestDocumentationResultHandler`. You can obtain an instance of this clas from the static `document` method on `org.springframework.restdocs.mockmvc.MockMvcRestDocumentation`. @@ -496,24 +521,26 @@ include::{examples-dir}/com/example/webtestclient/InvokeService.java[tags=invoke is required. <2> Assert that the service produced the expected response. <3> Document the call to the service, writing the snippets into a directory named `index` -that will be located beneath the configured output directory. The snippets are written by -a `Consumer` of the `ExchangeResult`. Such a consumer can be obtained from the static -`document` method on `org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation`. +(which is located beneath the configured output directory). The snippets are written by +a `Consumer` of the `ExchangeResult`. You can obtain such a consumer from the static +`document` method on +`org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation`. [source,java,indent=0,role="secondary"] .REST Assured ---- include::{examples-dir}/com/example/restassured/InvokeService.java[tags=invoke-service] ---- -<1> Apply the specification that was initialised in the `@Before` method. +<1> Apply the specification that was initialized in the `@Before` method. <2> Indicate that an `application/json` response is required. <3> Document the call to the service, writing the snippets into a directory named `index` -that will be located beneath the configured output directory. The snippets are written by -a `RestDocumentationFilter`. An instance of this class can be obtained from the +(which is located beneath the configured output directory). The snippets are written by +a `RestDocumentationFilter`. You can obtain an instance of this class from the static `document` method on `RestAssuredRestDocumentation` in the `org.springframework.restdocs.restassured3` package. <4> Invoke the root (`/`) of the service. <5> Assert that the service produce the expected response. +==== By default, six snippets are written: @@ -524,18 +551,18 @@ By default, six snippets are written: * `/index/request-body.adoc` * `/index/response-body.adoc` -Refer to <> for more information about these and other snippets +See <> for more information about these and other snippets that can be produced by Spring REST Docs. [[getting-started-using-the-snippets]] -=== Using the snippets +=== Using the Snippets -Before using the generated snippets, a `.adoc` source file must be created. You can name -the file whatever you like as long as it has a `.adoc` suffix. The result HTML file will -have the same name but with a `.html` suffix. The default location of the source files and -the resulting HTML files depends on whether you are using Maven or Gradle: +Before using the generated snippets, you must create an `.adoc` source file. You can name +the file whatever you like as long as it has a `.adoc` suffix. The resulting HTML file +has the same name but with an `.html` suffix. The default location of the source files and +the resulting HTML files depends on whether you use Maven or Gradle: [cols="2,5,8"] |=== @@ -551,14 +578,17 @@ the resulting HTML files depends on whether you are using Maven or Gradle: |=== -The generated snippets can then be included in the manually created Asciidoctor file from -above using the +You can then include the generated snippets in the manually created Asciidoc file +(described earlier in this section) by using the http://asciidoctor.org/docs/asciidoc-syntax-quick-reference/#include-files[include macro]. -The `snippets` attribute that is automatically set by `spring-restdocs-asciidoctor` -configured in the <> can be used to reference the snippets output directory. For example: +You can use The `snippets` attribute that is automatically set by +`spring-restdocs-asciidoctor` configured in the +<> to reference the snippets +output directory. The following example shows how to do so: +==== [source,adoc,indent=0] ---- \include::{snippets}/index/curl-request.adoc[] ---- +==== diff --git a/docs/src/docs/asciidoc/index.adoc b/docs/src/docs/asciidoc/index.adoc index eee9257f8..4d679da51 100644 --- a/docs/src/docs/asciidoc/index.adoc +++ b/docs/src/docs/asciidoc/index.adoc @@ -1,5 +1,5 @@ = Spring REST Docs -Andy Wilkinson +Andy Wilkinson; Jay Bryant :doctype: book :icons: font :source-highlighter: highlightjs @@ -28,4 +28,4 @@ include::customizing-requests-and-responses.adoc[] include::configuration.adoc[] include::working-with-asciidoctor.adoc[] include::working-with-markdown.adoc[] -include::contributing.adoc[] \ No newline at end of file +include::contributing.adoc[] diff --git a/docs/src/docs/asciidoc/introduction.adoc b/docs/src/docs/asciidoc/introduction.adoc index 730439e44..87032c378 100644 --- a/docs/src/docs/asciidoc/introduction.adoc +++ b/docs/src/docs/asciidoc/introduction.adoc @@ -1,26 +1,26 @@ [[introduction]] == Introduction -The aim of Spring REST Docs is to help you to produce documentation for your RESTful -services that is accurate and readable. +The aim of Spring REST Docs is to help you produce accurate and readable documentation for +your RESTful services. Writing high-quality documentation is difficult. One way to ease that difficulty is to use tools that are well-suited to the job. To this end, Spring REST Docs uses http://asciidoctor.org[Asciidoctor] by default. Asciidoctor processes plain text and -produces HTML, styled and layed out to suit your needs. If you prefer, Spring REST Docs -can also be configured to use Markdown. +produces HTML, styled and laid out to suit your needs. If you prefer, you can also +configure Spring REST Docs to use Markdown. -Spring REST Docs makes use of snippets produced by tests written with Spring MVC's +Spring REST Docs uses snippets produced by tests written with Spring MVC's {spring-framework-docs}/testing.html#spring-mvc-test-framework[test framework], Spring WebFlux's {spring-framework-docs}/testing.html#webtestclient[`WebTestClient`] or http://www.rest-assured.io[REST Assured 3]. This test-driven approach helps to guarantee -the accuracy of your service's documentation. If a snippet is incorrect the test that -produces it will fail. +the accuracy of your service's documentation. If a snippet is incorrect, the test that +produces it fails. Documenting a RESTful service is largely about describing its resources. Two key parts of each resource's description are the details of the HTTP requests that it consumes -and the HTTP responses that it produces. Spring REST Docs allows you to work with these +and the HTTP responses that it produces. Spring REST Docs lets you work with these resources and the HTTP requests and responses, shielding your documentation -from the inner-details of your service's implementation. This separation helps you to +from the inner-details of your service's implementation. This separation helps you document your service's API rather than its implementation. It also frees you to evolve the implementation without having to rework the documentation. diff --git a/docs/src/docs/asciidoc/working-with-asciidoctor.adoc b/docs/src/docs/asciidoc/working-with-asciidoctor.adoc index de36f81ce..c4c2abc76 100644 --- a/docs/src/docs/asciidoc/working-with-asciidoctor.adoc +++ b/docs/src/docs/asciidoc/working-with-asciidoctor.adoc @@ -1,9 +1,12 @@ [[working-with-asciidoctor]] == Working with Asciidoctor -This section describes any aspects of working with Asciidoctor that are particularly +This section describes the aspects of working with Asciidoctor that are particularly relevant to Spring REST Docs. +NOTE: Asciidoc is the document format. Asciidoctor is the tool that produces content +(usually as HTML) from Asciidoc files (which end with `.adoc`). + [[working-with-asciidoctor-resources]] @@ -15,43 +18,50 @@ relevant to Spring REST Docs. [[working-with-asciidoctor-including-snippets]] -=== Including snippets +=== Including Snippets + +This section covers how to include Asciidoc snippets. [[working-with-asciidoctor-including-snippets-operation]] -==== Including multiple snippets for an operation +==== Including Multiple Snippets for an Operation -A macro named `operation` can be used to import all or some of the snippets that have +You can use a macro named `operation` to import all or some of the snippets that have been generated for a specific operation. It is made available by including `spring-restdocs-asciidoctor` in your project's <>. -WARNING: If you are using Gradle and its daemon or support for continuous builds, do not +WARNING: If you use Gradle and its daemon or support for continuous builds, do not use version 1.5.6 of the `org.asciidoctor.convert` plugin. It contains a https://github.com/asciidoctor/asciidoctor-gradle-plugin/issues/222[regression] that prevents extensions from working reliably. -The target of the macro is the name of the operation. In its simplest form, the macro -can be used to include all of the snippets for an operation, as shown in the following +The target of the macro is the name of the operation. In its simplest form, you can use +the macro to include all of the snippets for an operation, as shown in the following example: +==== [source,indent=0] ---- operation::index[] ---- +==== -The operation macro also supports a `snippets` attribute. The `snippets` attribute can be -used to select the snippets that should be included. The attribute's value is a -comma-separated list. Each entry in the list should be the name of a snippet file, minus -the `.adoc` suffix, to include. For example, only the curl, HTTP request and HTTP response -snippets can be included as shown in the following example: +You can use the operation macro also supports a `snippets` attribute. The `snippets` +attribute to select the snippets that should be included. The attribute's value is a +comma-separated list. Each entry in the list should be the name of a snippet file (minus +the `.adoc` suffix) to include. For example, only the curl, HTTP request and HTTP response +snippets can be included, as shown in the following example: +==== [source,indent=0] ---- operation::index[snippets='curl-request,http-request,http-response'] ---- +==== -This is the equivalent of the following: +The preceding example is the equivalent of the following: +==== [source,adoc,indent=0] ---- [[example_curl_request]] @@ -70,78 +80,82 @@ This is the equivalent of the following: \include::{snippets}/index/http-response.adoc[] ---- - +==== [[working-with-asciidoctor-including-snippets-operation-titles]] -===== Section titles +===== Section Titles -For each snippet that's including using `operation`, a section with a title will be -created. Default titles are provided for the built-in snippets: +For each snippet that includes `operation`, a section with a title is +created. Default titles are provided for the following built-in snippets: |=== | Snippet | Title -| curl-request +| `curl-request` | Curl Request -| http-request +| `http-request` | HTTP request -| http-response +| `http-response` | HTTP response -| httpie-request +| `httpie-request` | HTTPie request -| links +| `links` | Links -| request-body +| `request-body` | Request body -| request-fields +| `request-fields` | Request fields -| response-body +| `response-body` | Response body -| response-fields +| `response-fields` | Response fields |=== -For snippets not listed in the table above, a default title will be generated by replacing -`-` characters with spaces and capitalising the first letter. For example, the title for a -snippet named `custom-snippet` will be "Custom snippet". +For snippets not listed in the preceding table, a default title is generated by replacing +`-` characters with spaces and capitalizing the first letter. For example, the title for a +snippet named `custom-snippet` `will be` "`Custom snippet`". -The default titles can be customized using document attributes. The name of the attribute -should be `operation-{snippet}-title`. For example, to customize the title of the -`curl-request` snippet to be "Example request", use the following attribute: +You can customize the default titles by using document attributes. The name of the +attribute should be `operation-{snippet}-title`. For example, to customize the title of +the `curl-request` snippet to be "Example request", you can use the following attribute: +==== [source,indent=0] ---- :operation-curl-request-title: Example request ---- +==== [[working-with-asciidoctor-including-snippets-individual]] -==== Including individual snippets +==== Including Individual Snippets The http://asciidoctor.org/docs/asciidoc-syntax-quick-reference/#include-files[include -macro] is used to include individual snippets in your documentation. The `snippets` -attribute that is automatically set by `spring-restdocs-asciidoctor` configured in the -<> can be used to reference the -snippets output directory. For example: +macro] is used to include individual snippets in your documentation. You can use the +`snippets` attribute (which is automatically set by `spring-restdocs-asciidoctor` +configured in the <>) to +reference the snippets output directory. The following example shows how to do so: +==== [source,indent=0] ---- \include::{snippets}/index/curl-request.adoc[] ---- +==== [[working-with-asciidoctor-customizing-tables]] -=== Customizing tables +=== Customizing Tables Many of the snippets contain a table in its default configuration. The appearance of the table can be customized, either by providing some additional configuration when the @@ -150,55 +164,63 @@ snippet is included or by using a custom snippet template. [[working-with-asciidoctor-customizing-tables-formatting-columns]] -==== Formatting columns +==== Formatting Columns Asciidoctor has rich support for -http://asciidoctor.org/docs/user-manual/#cols-format[formatting a table's columns]. For -example, the widths of a table's columns can be specified using the `cols` attribute: +http://asciidoctor.org/docs/user-manual/#cols-format[formatting a table's columns]. As the +following example shows, you can specify the widths of a table's columns by using the +`cols` attribute: +==== [source,indent=0] ---- [cols="1,3"] <1> \include::{snippets}/index/links.adoc[] ---- -<1> The table's width will be split across its two columns with the second column being +<1> The table's width is split across its two columns, with the second column being three times as wide as the first. +==== [[working-with-asciidoctor-customizing-tables-title]] -==== Configuring the title +==== Configuring the Title -The title of a table can be specified using a line prefixed by a `.`: +You can specify the title of a table by using a line prefixed by a `.`. +The following example shows how to do so: +==== [source,indent=0] ---- .Links <1> \include::{snippets}/index/links.adoc[] ---- <1> The table's title will be `Links`. +==== [[working-with-asciidoctor-customizing-tables-formatting-problems]] -==== Avoiding table formatting problems +==== Avoiding Table Formatting Problems Asciidoctor uses the `|` character to delimit cells in a table. This can cause problems -if you want a `|` to appear in a cell's contents. The problem can be avoided by -escaping the `|` with a backslash, i.e. by using `\|` rather than `|`. +if you want a `|` to appear in a cell's contents. You can avoide the problem by +escaping the `|` with a backslash -- in other words, by using `\|` rather than `|`. All of the default Asciidoctor snippet templates perform this escaping automatically -use a Mustache lamba named `tableCellContent`. If you write your own custom templates -you may want to use this lamba. For example, to escape `|` characters +by using a Mustache lamba named `tableCellContent`. If you write your own custom templates +you may want to use this lamba. The following example shows how to escape `|` characters in a cell that contains the value of a `description` attribute: +==== ---- | {{#tableCellContent}}{{description}}{{/tableCellContent}} ---- +==== -==== Further reading +==== Further Reading -Refer to the http://asciidoctor.org/docs/user-manual/#tables[Tables section of +See the http://asciidoctor.org/docs/user-manual/#tables[Tables section of the Asciidoctor user manual] for more information about customizing tables. diff --git a/docs/src/docs/asciidoc/working-with-markdown.adoc b/docs/src/docs/asciidoc/working-with-markdown.adoc index 4d532a91f..8b8492863 100644 --- a/docs/src/docs/asciidoc/working-with-markdown.adoc +++ b/docs/src/docs/asciidoc/working-with-markdown.adoc @@ -1,7 +1,7 @@ [[working-with-markdown]] == Working with Markdown -This section describes any aspects of working with Markdown that are particularly +This section describes the aspects of working with Markdown that are particularly relevant to Spring REST Docs. @@ -9,7 +9,7 @@ relevant to Spring REST Docs. [[working-with-markdown-limitations]] === Limitations -Markdown was originally designed for people writing for the web and, as such, isn't +Markdown was originally designed for people writing for the web and, as such, is not as well-suited to writing documentation as Asciidoctor. Typically, these limitations are overcome by using another tool that builds on top of Markdown. @@ -20,9 +20,9 @@ format]. [[working-with-markdown-including-snippets]] -=== Including snippets +=== Including Snippets Markdown has no built-in support for including one Markdown file in another. To include the generated snippets of Markdown in your documentation, you should use an additional -tool that supports this functionality. One example that's particularly well-suited to +tool that supports this functionality. One example that is particularly well-suited to documenting APIs is https://github.com/tripit/slate[Slate]. From 4a1ee50b237823d5fa105ba768fba95ba3b2eaca Mon Sep 17 00:00:00 2001 From: Jay Bryant Date: Thu, 7 Feb 2019 13:00:29 -0600 Subject: [PATCH 2/2] Changes to account for problems Andy Wilkinson spotted Andy found a number of flaws in my editing. Apparently, I was having a typo day when I did that editing pass. We also negotiated a better sentence in one spot and fixed up a few other details. Thanks, Andy. --- docs/src/docs/asciidoc/configuration.adoc | 4 ++-- docs/src/docs/asciidoc/contributing.adoc | 2 +- .../customizing-requests-and-responses.adoc | 8 ++++---- docs/src/docs/asciidoc/documenting-your-api.adoc | 14 +++++++------- docs/src/docs/asciidoc/getting-started.adoc | 6 +++--- .../docs/asciidoc/working-with-asciidoctor.adoc | 6 +++--- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/docs/src/docs/asciidoc/configuration.adoc b/docs/src/docs/asciidoc/configuration.adoc index 8fb04bda3..f9b56f7dc 100644 --- a/docs/src/docs/asciidoc/configuration.adoc +++ b/docs/src/docs/asciidoc/configuration.adoc @@ -103,8 +103,8 @@ include::{examples-dir}/com/example/restassured/CustomEncoding.java[tags=custom- ---- ==== -TIP: When Spring REST Docs converts the content of a request or a response to a `String`. -The `charset` specified in the `Content-Type` header is used if it is available. In its +TIP: When Spring REST Docs converts the content of a request or a response to a `String`, +the `charset` specified in the `Content-Type` header is used if it is available. In its absence, the JVM's default `Charset` is used. You can configure the JVM's default `Charset` byusing the `file.encoding` system property. diff --git a/docs/src/docs/asciidoc/contributing.adoc b/docs/src/docs/asciidoc/contributing.adoc index 870b138d6..1792a37d3 100644 --- a/docs/src/docs/asciidoc/contributing.adoc +++ b/docs/src/docs/asciidoc/contributing.adoc @@ -29,7 +29,7 @@ ideally, includes a test that reproduces it. [[contributing-enhancements]] === Enhancements -If you want an enhancement to be made to Spring REST Docs, pull requests are most +If you would like an enhancement to be made to Spring REST Docs, pull requests are most welcome. The source code is on {github}[GitHub]. You may want to search the {github}/issues?q=is%3Aissue[existing issues] and {github}/pulls?q=is%3Apr[pull requests] to see if the enhancement has already been proprosed. You may also want to diff --git a/docs/src/docs/asciidoc/customizing-requests-and-responses.adoc b/docs/src/docs/asciidoc/customizing-requests-and-responses.adoc index d2a3c09c8..ae9c6af40 100644 --- a/docs/src/docs/asciidoc/customizing-requests-and-responses.adoc +++ b/docs/src/docs/asciidoc/customizing-requests-and-responses.adoc @@ -6,7 +6,7 @@ or a response exactly as it was received. Spring REST Docs provides a number of preprocessors that can be used to modify a request or response before it is documented. Preprocessing is configured by calling `document` with an `OperationRequestPreprocessor` -or an `OperationResponsePreprocessor`. You an obtain instances by using the +or an `OperationResponsePreprocessor`. You can obtain instances by using the static `preprocessRequest` and `preprocessResponse` methods on `Preprocessors`. The following examples show how to do so: @@ -36,9 +36,9 @@ include::{examples-dir}/com/example/restassured/PerTestPreprocessing.java[tags=p <2> Apply a response preprocessor that pretty prints its content. ==== -Alternatively, you may want to apply the same preprocessors to every test. You can do -so by configuring the preprocessors to use the `RestDocumentationConfigurer` API in your -`@Before` method. For example to remove the `Foo` header from all requests and pretty print +Alternatively, you may want to apply the same preprocessors to every test. You can do so +by using the `RestDocumentationConfigurer` API in your `@Before` method to configure the +preprocessors. For example to remove the `Foo` header from all requests and pretty print all responses, you could do one of the following (depending on your testing environment): ==== diff --git a/docs/src/docs/asciidoc/documenting-your-api.adoc b/docs/src/docs/asciidoc/documenting-your-api.adoc index 5092f81d1..6def9d595 100644 --- a/docs/src/docs/asciidoc/documenting-your-api.adoc +++ b/docs/src/docs/asciidoc/documenting-your-api.adoc @@ -263,7 +263,7 @@ methods on `org.springframework.restdocs.payload.PayloadDocumentation`. This can be useful when documenting a particular scenario where you want to focus only on a subset of the payload. -TIP: By default, Spring REST Docs assume that the payload you are documenting is +TIP: By default, Spring REST Docs assumes that the payload you are documenting is JSON. If you want to document an XML payload, the content type of the request or response must be compatible with `application/xml`. @@ -277,7 +277,7 @@ This section descries how to work with fields in JSON payloads. JSON field paths use either dot notation or bracket notation. Dot notation uses '.' to separate each key in the path (for example, `a.b`). Bracket notation wraps each key in -square brackets and single quotatuon marks; `['a']['b']`, for example. In either case, +square brackets and single quotation marks (for example, `['a']['b']`). In either case, `[]` is used to identify an array. Dot notation is more concise, but using bracket notation enables the use of `.` within a key name (for example, `['a.b']`). The two different notations can be used in the same path (for example, `a['b']`). @@ -415,9 +415,9 @@ examining the payload. Seven different types are supported: |=== You can also explicitly set the type by using the `type(Object)` method on -`FieldDescriptor`. The result of the supplied `toString` method of the `Object` is used +`FieldDescriptor`. The result of the `toString` method of the supplied `Object` is used in the documentation. Typically, one of the values enumerated by `JsonFieldType` is -used. The following example show how to do so: +used. The following examples show how to do so: ==== [source,java,indent=0,role="primary"] @@ -647,7 +647,7 @@ The result is a snippet with the following contents: ==== To make the snippet's name distinct, an identifier for the subsection is included. By -default, this identifier is `beneath-${path}`. For example, the precding code results in +default, this identifier is `beneath-${path}`. For example, the preceding code results in a snippet named `response-body-beneath-weather.temperature.adoc`. You can customized the identifier by using the `withSubsectionId(String)` method, as follows: @@ -929,7 +929,7 @@ When documenting request parts, the test fails if an undocumented part is used i request. Similarly, the test also fails if a documented part is not found in the request and the part has not been marked as optional. -You can also document Request parts in a relaxed mode where any undocumented +You can also document request parts in a relaxed mode where any undocumented parts do not cause a test failure. To do so, use the `relaxedRequestParts` method on `org.springframework.restdocs.request.RequestDocumentation`. This can be useful when documenting a particular scenario where you only want to focus on a subset of the @@ -1116,7 +1116,7 @@ the request or response. === Reusing Snippets It is common for an API that is being documented to have some features that are common -across several of its resources. To avoid repetition when documenting such resources, you' +across several of its resources. To avoid repetition when documenting such resources, you can reuse a `Snippet` configured with the common elements. First, create the `Snippet` that describes the common elements. The following example diff --git a/docs/src/docs/asciidoc/getting-started.adoc b/docs/src/docs/asciidoc/getting-started.adoc index 97da3fb9d..58685cb35 100644 --- a/docs/src/docs/asciidoc/getting-started.adoc +++ b/docs/src/docs/asciidoc/getting-started.adoc @@ -318,7 +318,7 @@ public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("cu ---- ==== -Next, you can provide an `@Before` method to configure MockMvc, WebTestClient or REST +Next, you must provide an `@Before` method to configure MockMvc, WebTestClient or REST Assured. The following examples show how to do so: ==== @@ -399,7 +399,7 @@ based on your project's build tool: -Next, you can provide a `@BeforeEach` method to configure MockMvc, WebTestClient, or REST +Next, you must provide a `@BeforeEach` method to configure MockMvc, WebTestClient, or REST Assured. The following listings show how to do so: ==== @@ -581,7 +581,7 @@ the resulting HTML files depends on whether you use Maven or Gradle: You can then include the generated snippets in the manually created Asciidoc file (described earlier in this section) by using the http://asciidoctor.org/docs/asciidoc-syntax-quick-reference/#include-files[include macro]. -You can use The `snippets` attribute that is automatically set by +You can use the `snippets` attribute that is automatically set by `spring-restdocs-asciidoctor` configured in the <> to reference the snippets output directory. The following example shows how to do so: diff --git a/docs/src/docs/asciidoc/working-with-asciidoctor.adoc b/docs/src/docs/asciidoc/working-with-asciidoctor.adoc index c4c2abc76..26615e54a 100644 --- a/docs/src/docs/asciidoc/working-with-asciidoctor.adoc +++ b/docs/src/docs/asciidoc/working-with-asciidoctor.adoc @@ -85,8 +85,8 @@ The preceding example is the equivalent of the following: [[working-with-asciidoctor-including-snippets-operation-titles]] ===== Section Titles -For each snippet that includes `operation`, a section with a title is -created. Default titles are provided for the following built-in snippets: +For each snippet that is included by using the `operation` macro, a section with a title +is created. Default titles are provided for the following built-in snippets: |=== | Snippet | Title @@ -204,7 +204,7 @@ The following example shows how to do so: ==== Avoiding Table Formatting Problems Asciidoctor uses the `|` character to delimit cells in a table. This can cause problems -if you want a `|` to appear in a cell's contents. You can avoide the problem by +if you want a `|` to appear in a cell's contents. You can avoid the problem by escaping the `|` with a backslash -- in other words, by using `\|` rather than `|`. All of the default Asciidoctor snippet templates perform this escaping automatically