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

Commit 5e17758

Browse files
authored
Simplify includes in section snippet (#220) (#221)
* Simplify includes in section snippet * Make clear that section snippets are not supported with Markdown
1 parent 4e1a864 commit 5e17758

File tree

4 files changed

+61
-56
lines changed

4 files changed

+61
-56
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[[resources-{{link}}]]
22
=== {{title}}
33

4-
include::{snippets}/{{path}}/auto-method-path.adoc[]
4+
include::auto-method-path.adoc[]
55

6-
include::{snippets}/{{path}}/auto-description.adoc[]
6+
include::auto-description.adoc[]
77
{{#sections}}
88

99
==== {{header}}
1010

11-
include::{snippets}/{{path}}/{{fileName}}.adoc[]
11+
include::{{fileName}}.adoc[]
1212
{{/sections}}
Original file line numberDiff line numberDiff line change
@@ -1,12 +0,0 @@
1-
[[resources-{{link}}]]
2-
=== {{title}}
3-
4-
include::{snippets}/{{path}}/auto-method-path.adoc[]
5-
6-
include::{snippets}/{{path}}/auto-description.adoc[]
7-
{{#sections}}
8-
9-
==== {{header}}
10-
11-
include::{snippets}/{{path}}/{{fileName}}.adoc[]
12-
{{/sections}}

spring-auto-restdocs-core/src/test/java/capital/scalable/restdocs/section/SectionSnippetTest.java

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -37,26 +37,41 @@
3737

3838
import java.util.ArrayList;
3939
import java.util.Arrays;
40+
import java.util.Collections;
41+
import java.util.List;
4042

4143
import capital.scalable.restdocs.i18n.TranslationRule;
4244
import capital.scalable.restdocs.javadoc.JavadocReader;
4345
import org.junit.Before;
4446
import org.junit.Rule;
4547
import org.junit.Test;
48+
import org.junit.runners.Parameterized;
4649
import org.springframework.restdocs.AbstractSnippetTests;
4750
import org.springframework.restdocs.http.HttpDocumentation;
4851
import org.springframework.restdocs.templates.TemplateFormat;
52+
import org.springframework.restdocs.templates.TemplateFormats;
53+
import org.springframework.restdocs.test.ExpectedSnippets;
54+
import org.springframework.restdocs.test.OperationBuilder;
4955
import org.springframework.web.method.HandlerMethod;
5056

51-
public class SectionSnippetTest extends AbstractSnippetTests {
57+
public class SectionSnippetTest {
5258

5359
private JavadocReader javadocReader;
5460

5561
@Rule
5662
public TranslationRule translationRule = new TranslationRule();
5763

58-
public SectionSnippetTest(String name, TemplateFormat templateFormat) {
59-
super(name, templateFormat);
64+
@Rule
65+
public ExpectedSnippets snippets;
66+
67+
@Rule
68+
public OperationBuilder operationBuilder;
69+
70+
public SectionSnippetTest() {
71+
// Only runs for AsciiDoctor, because Markdown is not supported.
72+
TemplateFormat templateFormat = TemplateFormats.asciidoctor();
73+
this.snippets = new ExpectedSnippets(templateFormat);
74+
this.operationBuilder = new OperationBuilder(templateFormat);
6075
}
6176

6277
@Before
@@ -72,8 +87,8 @@ public void noSnippets() throws Exception {
7287
this.snippets.expect(SECTION)
7388
.withContents(equalTo("[[resources-noSnippets]]\n" +
7489
"=== Get Item By Id\n\n" +
75-
"include::{snippets}/noSnippets/auto-method-path.adoc[]\n\n" +
76-
"include::{snippets}/noSnippets/auto-description.adoc[]\n"));
90+
"include::auto-method-path.adoc[]\n\n" +
91+
"include::auto-description.adoc[]\n"));
7792

7893
new SectionBuilder()
7994
.snippetNames()
@@ -91,8 +106,8 @@ public void noHandlerMethod() throws Exception {
91106
this.snippets.expect(SECTION)
92107
.withContents(equalTo("[[resources-noHandlerMethod]]\n" +
93108
"=== No Handler Method\n\n" +
94-
"include::{snippets}/noHandlerMethod/auto-method-path.adoc[]\n\n" +
95-
"include::{snippets}/noHandlerMethod/auto-description.adoc[]\n"));
109+
"include::auto-method-path.adoc[]\n\n" +
110+
"include::auto-description.adoc[]\n"));
96111

97112
new SectionBuilder()
98113
.snippetNames()
@@ -112,22 +127,22 @@ public void defaultSnippets() throws Exception {
112127
this.snippets.expect(SECTION)
113128
.withContents(equalTo("[[resources-defaultSnippets]]\n" +
114129
"=== Get Item By Id\n\n" +
115-
"include::{snippets}/defaultSnippets/auto-method-path.adoc[]\n\n" +
116-
"include::{snippets}/defaultSnippets/auto-description.adoc[]\n\n" +
130+
"include::auto-method-path.adoc[]\n\n" +
131+
"include::auto-description.adoc[]\n\n" +
117132
"==== Authorization\n\n" +
118-
"include::{snippets}/defaultSnippets/auto-authorization.adoc[]\n\n" +
133+
"include::auto-authorization.adoc[]\n\n" +
119134
"==== Path parameters\n\n" +
120-
"include::{snippets}/defaultSnippets/auto-path-parameters.adoc[]\n\n" +
135+
"include::auto-path-parameters.adoc[]\n\n" +
121136
"==== Query parameters\n\n" +
122-
"include::{snippets}/defaultSnippets/auto-request-parameters.adoc[]\n\n" +
137+
"include::auto-request-parameters.adoc[]\n\n" +
123138
"==== Request fields\n\n" +
124-
"include::{snippets}/defaultSnippets/auto-request-fields.adoc[]\n\n" +
139+
"include::auto-request-fields.adoc[]\n\n" +
125140
"==== Response fields\n\n" +
126-
"include::{snippets}/defaultSnippets/auto-response-fields.adoc[]\n\n" +
141+
"include::auto-response-fields.adoc[]\n\n" +
127142
"==== Example request\n\n" +
128-
"include::{snippets}/defaultSnippets/curl-request.adoc[]\n\n" +
143+
"include::curl-request.adoc[]\n\n" +
129144
"==== Example response\n\n" +
130-
"include::{snippets}/defaultSnippets/http-response.adoc[]\n"));
145+
"include::http-response.adoc[]\n"));
131146

132147
new SectionBuilder().build()
133148
.document(operationBuilder
@@ -149,14 +164,14 @@ public void customSnippets() throws Exception {
149164
this.snippets.expect(SECTION)
150165
.withContents(equalTo("[[resources-customSnippets]]\n" +
151166
"=== Get Item By Id\n\n" +
152-
"include::{snippets}/customSnippets/auto-method-path.adoc[]\n\n" +
153-
"include::{snippets}/customSnippets/auto-description.adoc[]\n\n" +
167+
"include::auto-method-path.adoc[]\n\n" +
168+
"include::auto-description.adoc[]\n\n" +
154169
"==== Example response\n\n" +
155-
"include::{snippets}/customSnippets/http-response.adoc[]\n\n" +
170+
"include::http-response.adoc[]\n\n" +
156171
"==== Response fields\n\n" +
157-
"include::{snippets}/customSnippets/auto-response-fields.adoc[]\n\n" +
172+
"include::auto-response-fields.adoc[]\n\n" +
158173
"==== Example request\n\n" +
159-
"include::{snippets}/customSnippets/http-request.adoc[]\n"));
174+
"include::http-request.adoc[]\n"));
160175

161176
new SectionBuilder()
162177
.snippetNames(HTTP_RESPONSE, RESPONSE_FIELDS, HTTP_REQUEST)
@@ -180,14 +195,14 @@ public void skipEmpty() throws Exception {
180195
this.snippets.expect(SECTION)
181196
.withContents(equalTo("[[resources-skipEmpty]]\n" +
182197
"=== Get Item By Id\n\n" +
183-
"include::{snippets}/skipEmpty/auto-method-path.adoc[]\n\n" +
184-
"include::{snippets}/skipEmpty/auto-description.adoc[]\n\n" +
198+
"include::auto-method-path.adoc[]\n\n" +
199+
"include::auto-description.adoc[]\n\n" +
185200
"==== Authorization\n\n" +
186-
"include::{snippets}/skipEmpty/auto-authorization.adoc[]\n\n" +
201+
"include::auto-authorization.adoc[]\n\n" +
187202
"==== Example request\n\n" +
188-
"include::{snippets}/skipEmpty/curl-request.adoc[]\n\n" +
203+
"include::curl-request.adoc[]\n\n" +
189204
"==== Example response\n\n" +
190-
"include::{snippets}/skipEmpty/http-response.adoc[]\n"));
205+
"include::http-response.adoc[]\n"));
191206

192207
new SectionBuilder()
193208
.skipEmpty(true)
@@ -211,8 +226,8 @@ public void customTitle() throws Exception {
211226
this.snippets.expect(SECTION)
212227
.withContents(equalTo("[[resources-customTitle]]\n" +
213228
"=== Custom title\n\n" +
214-
"include::{snippets}/customTitle/auto-method-path.adoc[]\n\n" +
215-
"include::{snippets}/customTitle/auto-description.adoc[]\n"));
229+
"include::auto-method-path.adoc[]\n\n" +
230+
"include::auto-description.adoc[]\n"));
216231

217232
new SectionBuilder()
218233
.snippetNames()
@@ -236,8 +251,8 @@ public void deprecated() throws Exception {
236251
this.snippets.expect(SECTION)
237252
.withContents(equalTo("[[resources-deprecated]]\n" +
238253
"=== Get Item By Id (deprecated)\n\n" +
239-
"include::{snippets}/deprecated/auto-method-path.adoc[]\n\n" +
240-
"include::{snippets}/deprecated/auto-description.adoc[]\n"));
254+
"include::auto-method-path.adoc[]\n\n" +
255+
"include::auto-description.adoc[]\n"));
241256

242257
new SectionBuilder()
243258
.snippetNames()
@@ -260,22 +275,22 @@ public void translation() throws Exception {
260275
this.snippets.expect(SECTION)
261276
.withContents(equalTo("[[resources-translation]]\n" +
262277
"=== Get Item By Id\n\n" +
263-
"include::{snippets}/translation/auto-method-path.adoc[]\n\n" +
264-
"include::{snippets}/translation/auto-description.adoc[]\n\n" +
278+
"include::auto-method-path.adoc[]\n\n" +
279+
"include::auto-description.adoc[]\n\n" +
265280
"==== XAuthorization\n\n" +
266-
"include::{snippets}/translation/auto-authorization.adoc[]\n\n" +
281+
"include::auto-authorization.adoc[]\n\n" +
267282
"==== XPath parameters\n\n" +
268-
"include::{snippets}/translation/auto-path-parameters.adoc[]\n\n" +
283+
"include::auto-path-parameters.adoc[]\n\n" +
269284
"==== XQuery parameters\n\n" +
270-
"include::{snippets}/translation/auto-request-parameters.adoc[]\n\n" +
285+
"include::auto-request-parameters.adoc[]\n\n" +
271286
"==== XRequest fields\n\n" +
272-
"include::{snippets}/translation/auto-request-fields.adoc[]\n\n" +
287+
"include::auto-request-fields.adoc[]\n\n" +
273288
"==== XResponse fields\n\n" +
274-
"include::{snippets}/translation/auto-response-fields.adoc[]\n\n" +
289+
"include::auto-response-fields.adoc[]\n\n" +
275290
"==== XExample request\n\n" +
276-
"include::{snippets}/translation/curl-request.adoc[]\n\n" +
291+
"include::curl-request.adoc[]\n\n" +
277292
"==== XExample response\n\n" +
278-
"include::{snippets}/translation/http-response.adoc[]\n"));
293+
"include::http-response.adoc[]\n"));
279294

280295
new SectionBuilder().build()
281296
.document(operationBuilder

spring-auto-restdocs-docs/section-snippet.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
[[snippets-section]]
44
=== Section snippet
55

6+
NOTE: Section snippets only work if AsciiDoc is used. Markdown is not supported, because Markdown has no includes.
7+
68
The section snippet combines most common snippets into one convenient file.
79
It helps you being even more lazy, because a single line of AsciiDoc is sufficient to document one endpoint.
810
Assuming of course that you already documented your code with Javadoc.

0 commit comments

Comments
 (0)