Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 628d46e

Browse files
authored
Demonstrate mixing of Spring Auto REST Docs and Spring REST Docs (#199)
* Demonstrate mixing of Spring Auto REST Docs and Spring REST Docs * Fix new FAQ entry
1 parent 76af31a commit 628d46e

File tree

6 files changed

+189
-11
lines changed

6 files changed

+189
-11
lines changed

docs/index.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,12 @@ <h2 id="faq"><a class="link" href="#faq">FAQ</a></h2>
546546
For advanced customization, multiple source directories for the JSON files
547547
can be configured (see <a href="#gettingstarted-usage">usage</a>).</p>
548548
</li>
549+
<li>
550+
<p><em>Can Spring REST Docs and Spring Auto REST Docs features be combined?</em></p>
551+
<p>Yes, this even works in the same test.
552+
One can generate Spring REST Docs snippets and Spring Auto REST Docs snippets out of the same test.
553+
This is demonstrated in the example project.</p>
554+
</li>
549555
</ol>
550556
</div>
551557
</div>
@@ -1767,7 +1773,7 @@ <h4 id="contributing-building-build"><a class="link" href="#contributing-buildin
17671773
</div>
17681774
<div id="footer">
17691775
<div id="footer-text">
1770-
Last updated 2018-01-03 18:23:11 CET
1776+
Last updated 2018-01-06 21:38:23 CET
17711777
</div>
17721778
</div>
17731779
<link rel="stylesheet" href="highlight/styles/github.min.css">

spring-auto-restdocs-docs/index.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ Is a multi-module project setup supported?::
5151
so modularization of codebase is fully supported.
5252
For advanced customization, multiple source directories for the JSON files
5353
can be configured (see <<gettingstarted-usage,usage>>).
54+
Can Spring REST Docs and Spring Auto REST Docs features be combined?::
55+
Yes, this even works in the same test.
56+
One can generate Spring REST Docs snippets and Spring Auto REST Docs snippets out of the same test.
57+
This is demonstrated in the example project.
5458

5559
include::getting-started.adoc[]
5660

spring-auto-restdocs-example/generated-docs/index.html

Lines changed: 122 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ <h1>Example API Documentation</h1>
449449
<li><a href="#resources-item-resource-test-process-single-item">2.9. Process One Item</a></li>
450450
<li><a href="#resources-item-resource-test-validate-metadata">2.10. Validate Metadata</a></li>
451451
<li><a href="#resources-item-resource-test-clone-item">2.11. Clone Item (deprecated)</a></li>
452+
<li><a href="#resources-items-get-spring-rest-docs">2.12. Get an item (SARD/SRD mix)</a></li>
452453
</ul>
453454
</li>
454455
</ul>
@@ -1121,7 +1122,7 @@ <h4 id="_example_request"><a class="anchor" href="#_example_request"></a><a clas
11211122
<div class="content">
11221123
<pre class="highlightjs highlight"><code class="language-bash" data-lang="bash">$ curl 'http://localhost:8080/items' -i -X POST \
11231124
-H 'Content-Type: application/json' \
1124-
-H 'Authorization: Bearer 5ce7867e-0458-4378-8b94-a7133a194e47' \
1125+
-H 'Authorization: Bearer 5594beae-01ec-412b-9169-b69c30696c18' \
11251126
-d '{"description":"Hot News"}'</code></pre>
11261127
</div>
11271128
</div>
@@ -1358,7 +1359,7 @@ <h4 id="_example_request_2"><a class="anchor" href="#_example_request_2"></a><a
13581359
<div class="content">
13591360
<pre class="highlightjs highlight"><code class="language-bash" data-lang="bash">$ curl 'http://localhost:8080/items/1' -i -X PUT \
13601361
-H 'Content-Type: application/json' \
1361-
-H 'Authorization: Bearer 5ce7867e-0458-4378-8b94-a7133a194e47' \
1362+
-H 'Authorization: Bearer 5594beae-01ec-412b-9169-b69c30696c18' \
13621363
-d '{"description":"Hot News"}'</code></pre>
13631364
</div>
13641365
</div>
@@ -1464,7 +1465,7 @@ <h4 id="_example_request_3"><a class="anchor" href="#_example_request_3"></a><a
14641465
<div class="listingblock">
14651466
<div class="content">
14661467
<pre class="highlightjs highlight"><code class="language-bash" data-lang="bash">$ curl 'http://localhost:8080/items/1' -i -X DELETE \
1467-
-H 'Authorization: Bearer 5ce7867e-0458-4378-8b94-a7133a194e47'</code></pre>
1468+
-H 'Authorization: Bearer 5594beae-01ec-412b-9169-b69c30696c18'</code></pre>
14681469
</div>
14691470
</div>
14701471
</div>
@@ -1949,19 +1950,19 @@ <h4 id="_example_response_5"><a class="anchor" href="#_example_response_5"></a><
19491950
"page" : 0,
19501951
"size" : 20,
19511952
"sort" : null,
1952-
"offset" : 0,
19531953
"pageNumber" : 0,
1954-
"pageSize" : 20
1954+
"pageSize" : 20,
1955+
"offset" : 0
19551956
},
19561957
"total" : 1,
1957-
"last" : true,
19581958
"totalPages" : 1,
1959+
"last" : true,
19591960
"totalElements" : 1,
1960-
"size" : 20,
1961-
"number" : 0,
19621961
"sort" : null,
19631962
"first" : true,
1964-
"numberOfElements" : 1
1963+
"numberOfElements" : 1,
1964+
"size" : 20,
1965+
"number" : 0
19651966
}</code></pre>
19661967
</div>
19671968
</div>
@@ -2420,12 +2421,123 @@ <h4 id="_example_response_9"><a class="anchor" href="#_example_response_9"></a><
24202421
</div>
24212422
</div>
24222423
</div>
2424+
<div class="sect2">
2425+
<h3 id="resources-items-get-spring-rest-docs"><a class="anchor" href="#resources-items-get-spring-rest-docs"></a><a class="link" href="#resources-items-get-spring-rest-docs">2.12. Get an item (SARD/SRD mix)</a></h3>
2426+
<div class="paragraph">
2427+
<p>This part of the documentation demonstrates the combination of Spring Auto REST Docs with Spring REST Docs:
2428+
The path parameter and response field are manually documented with Spring REST Docs.
2429+
The authorization is still coming from Spring Auto REST Docs and
2430+
the cURL and HTTP response are coming from Spring REST Docs in any case.</p>
2431+
</div>
2432+
<div class="paragraph">
2433+
<p><code>GET /items/{id}</code></p>
2434+
</div>
2435+
<div class="sect3">
2436+
<h4 id="_authorization_12"><a class="anchor" href="#_authorization_12"></a><a class="link" href="#_authorization_12">2.12.1. Authorization</a></h4>
2437+
<div class="paragraph">
2438+
<p>OAuth2: Resource is public.</p>
2439+
</div>
2440+
</div>
2441+
<div class="sect3">
2442+
<h4 id="_path_parameters_11"><a class="anchor" href="#_path_parameters_11"></a><a class="link" href="#_path_parameters_11">2.12.2. Path parameters</a></h4>
2443+
<table class="tableblock frame-all grid-all spread">
2444+
<caption class="title">Table 3. /items/{id}</caption>
2445+
<colgroup>
2446+
<col style="width: 50%;">
2447+
<col style="width: 50%;">
2448+
</colgroup>
2449+
<thead>
2450+
<tr>
2451+
<th class="tableblock halign-left valign-top">Parameter</th>
2452+
<th class="tableblock halign-left valign-top">Description</th>
2453+
</tr>
2454+
</thead>
2455+
<tbody>
2456+
<tr>
2457+
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>id</code></p></td>
2458+
<td class="tableblock halign-left valign-top"><p class="tableblock">ID of the item.</p></td>
2459+
</tr>
2460+
</tbody>
2461+
</table>
2462+
</div>
2463+
<div class="sect3">
2464+
<h4 id="_response_fields_12"><a class="anchor" href="#_response_fields_12"></a><a class="link" href="#_response_fields_12">2.12.3. Response fields</a></h4>
2465+
<table class="tableblock frame-all grid-all spread">
2466+
<colgroup>
2467+
<col style="width: 33.3333%;">
2468+
<col style="width: 33.3333%;">
2469+
<col style="width: 33.3334%;">
2470+
</colgroup>
2471+
<thead>
2472+
<tr>
2473+
<th class="tableblock halign-left valign-top">Path</th>
2474+
<th class="tableblock halign-left valign-top">Type</th>
2475+
<th class="tableblock halign-left valign-top">Description</th>
2476+
</tr>
2477+
</thead>
2478+
<tbody>
2479+
<tr>
2480+
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>id</code></p></td>
2481+
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code></p></td>
2482+
<td class="tableblock halign-left valign-top"><p class="tableblock">There are more fields but only the ID field is documented.</p></td>
2483+
</tr>
2484+
</tbody>
2485+
</table>
2486+
</div>
2487+
<div class="sect3">
2488+
<h4 id="_example_request_response_3"><a class="anchor" href="#_example_request_response_3"></a><a class="link" href="#_example_request_response_3">2.12.4. Example request/response</a></h4>
2489+
<div class="listingblock">
2490+
<div class="content">
2491+
<pre class="highlightjs highlight"><code class="language-bash" data-lang="bash">$ curl 'http://localhost:8080/items/1' -i</code></pre>
2492+
</div>
2493+
</div>
2494+
<div class="listingblock">
2495+
<div class="content">
2496+
<pre class="highlightjs highlight nowrap"><code class="language-http" data-lang="http">HTTP/1.1 200 OK
2497+
X-Content-Type-Options: nosniff
2498+
X-XSS-Protection: 1; mode=block
2499+
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
2500+
Pragma: no-cache
2501+
Expires: 0
2502+
X-Frame-Options: DENY
2503+
Content-Type: application/json;charset=UTF-8
2504+
Content-Length: 459
2505+
2506+
{
2507+
"id" : "1",
2508+
"meta" : {
2509+
"type" : "1",
2510+
"tag" : "meta1"
2511+
},
2512+
"attributes" : {
2513+
"text" : "first item",
2514+
"number" : 1,
2515+
"bool" : true,
2516+
"decimal" : 1.11,
2517+
"amount" : "3.14 EUR",
2518+
"enumType" : "ONE"
2519+
},
2520+
"children" : [ {
2521+
"id" : "child-1",
2522+
"meta" : null,
2523+
"attributes" : null,
2524+
"children" : null,
2525+
"tags" : null,
2526+
"description" : "first child"
2527+
} ],
2528+
"tags" : [ "top-level" ],
2529+
"description" : "main item"
2530+
}</code></pre>
2531+
</div>
2532+
</div>
2533+
</div>
2534+
</div>
24232535
</div>
24242536
</div>
24252537
</div>
24262538
<div id="footer">
24272539
<div id="footer-text">
2428-
Last updated 2017-11-02 11:03:07 CET
2540+
Last updated 2018-01-06 21:26:54 CET
24292541
</div>
24302542
</div>
24312543
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.9.1/styles/github.min.css">

spring-auto-restdocs-example/src/main/asciidoc/index.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,4 @@ Pagination response has following structure:
121121
= Resources
122122

123123
include::items.adoc[]
124+
include::srd-items.adoc[]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[[resources-items-get-spring-rest-docs]]
2+
=== Get an item (SARD/SRD mix)
3+
4+
This part of the documentation demonstrates the combination of Spring Auto REST Docs with Spring REST Docs:
5+
The path parameter and response field are manually documented with Spring REST Docs.
6+
The authorization is still coming from Spring Auto REST Docs and
7+
the cURL and HTTP response are coming from Spring REST Docs in any case.
8+
9+
`GET /items/{id}`
10+
11+
==== Authorization
12+
13+
include::{snippets}/item-resource-test/get-item-with-rest-docs/auto-authorization.adoc[]
14+
15+
==== Path parameters
16+
17+
include::{snippets}/item-resource-test/get-item-with-rest-docs/path-parameters.adoc[]
18+
19+
==== Response fields
20+
21+
include::{snippets}/item-resource-test/get-item-with-rest-docs/response-fields.adoc[]
22+
23+
==== Example request/response
24+
25+
include::{snippets}/item-resource-test/get-item-with-rest-docs/curl-request.adoc[]
26+
include::{snippets}/item-resource-test/get-item-with-rest-docs/http-response.adoc[]

spring-auto-restdocs-example/src/test/java/capital/scalable/restdocs/example/items/ItemResourceTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
import static org.hamcrest.Matchers.is;
2727
import static org.hamcrest.Matchers.nullValue;
2828
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
29+
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
30+
import static org.springframework.restdocs.payload.PayloadDocumentation.relaxedResponseFields;
31+
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
32+
import static org.springframework.restdocs.request.RequestDocumentation.pathParameters;
2933
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
3034
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
3135
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
@@ -40,6 +44,7 @@
4044
import capital.scalable.restdocs.example.testsupport.MockMvcBase;
4145
import org.junit.Test;
4246
import org.springframework.http.MediaType;
47+
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders;
4348

4449
public class ItemResourceTest extends MockMvcBase {
4550

@@ -155,4 +160,28 @@ public void cloneItem() throws Exception {
155160
.content("{ \"name\": \"xyz\" }"))
156161
.andExpect(status().isOk());
157162
}
163+
164+
/**
165+
* This test demonstrates that Spring REST Docs can be used in a Spring Auto REST Docs setup.
166+
* <p>
167+
* All Spring Auto REST Docs snippets are created as well because of the setup in {@link MockMvcBase}.
168+
* This is not a requirement and one could use a pure Spring REST Docs setup here.
169+
* <p>
170+
* The result of the manual documentation below ends up in path-parameters.adoc and response-fields.adoc and
171+
* not in auto-path-parameters.adoc or auto-request-fields.adoc.
172+
* So there is no conflict and one is free to decide which snippets are included in the documentation.
173+
* <p>
174+
* RestDocumentationRequestBuilders.get is required for Spring REST Docs' pathParameters to work.
175+
* Spring AUTO Rest Docs works with both MockMvcRequestBuilders and RestDocumentationRequestBuilders.
176+
*/
177+
@Test
178+
public void getItemWithRestDocs() throws Exception {
179+
mockMvc.perform(RestDocumentationRequestBuilders.get("/items/{id}", 1))
180+
.andExpect(status().isOk())
181+
.andDo(document("{class-name}/{method-name}", commonResponsePreprocessor(),
182+
pathParameters(
183+
parameterWithName("id").description("ID of the item.")),
184+
relaxedResponseFields(fieldWithPath("id")
185+
.description("There are more fields but only the ID field is documented."))));
186+
}
158187
}

0 commit comments

Comments
 (0)