Skip to content

Commit 495c8ad

Browse files
author
Saborowski, Kai
committed
Added SPringRestdocs constraint response-fields.snippet.
1 parent 41bb678 commit 495c8ad

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

src/test/java/de/ksbrwsk/people/RestAssuredDocsTest.java renamed to src/test/java/de/ksbrwsk/people/RestDocsTest.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.http.MediaType;
1111
import org.springframework.restdocs.RestDocumentationContextProvider;
1212
import org.springframework.restdocs.RestDocumentationExtension;
13+
import org.springframework.restdocs.constraints.ConstraintDescriptions;
1314
import org.springframework.restdocs.payload.JsonFieldType;
1415
import org.springframework.test.web.reactive.server.WebTestClient;
1516
import reactor.core.publisher.Flux;
@@ -22,12 +23,13 @@
2223
import static de.ksbrwsk.people.Constants.API;
2324
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
2425
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
26+
import static org.springframework.restdocs.snippet.Attributes.key;
2527
import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.document;
2628
import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.documentationConfiguration;
2729

2830
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
2931
@Import(TestReactivePeoplePostgresqlApplication.class)
30-
public class RestAssuredDocsTest extends PostgreSqlContainer {
32+
public class RestDocsTest extends PostgreSqlContainer {
3133

3234
@Autowired
3335
PersonRepository personRepository;
@@ -62,6 +64,11 @@ public void setUp(ApplicationContext applicationContext, RestDocumentationContex
6264
.verifyComplete();
6365
}
6466

67+
public List<String> constraintDescriptionForProperty(String property) {
68+
ConstraintDescriptions userConstraints = new ConstraintDescriptions(Person.class);
69+
return userConstraints.descriptionsForProperty(property);
70+
}
71+
6572
@Test
6673
void handleNotFound() {
6774
this.webTestClient
@@ -92,10 +99,12 @@ void handleFindAll() {
9299
responseFields(
93100
fieldWithPath("[].id")
94101
.type(JsonFieldType.NUMBER)
95-
.description("The person's id"),
102+
.description("The person's id")
103+
.attributes(key("constraints").value(constraintDescriptionForProperty("id"))),
96104
fieldWithPath("[].name")
97105
.type(JsonFieldType.STRING)
98-
.description("The person's name"))));
106+
.description("The person's name")
107+
.attributes(key("constraints").value(constraintDescriptionForProperty("name"))))));
99108
}
100109

101110
@Test
@@ -114,10 +123,12 @@ void handleFindById() {
114123
responseFields(
115124
fieldWithPath("id")
116125
.type(JsonFieldType.NUMBER)
117-
.description("The person's id"),
126+
.description("The person's id")
127+
.attributes(key("constraints").value(constraintDescriptionForProperty("id"))),
118128
fieldWithPath("name")
119129
.type(JsonFieldType.STRING)
120-
.description("The person's name"))));
130+
.description("The person's name")
131+
.attributes(key("constraints").value(constraintDescriptionForProperty("name"))))));
121132
}
122133

123134
@Test
@@ -177,10 +188,12 @@ void handleUpdate() {
177188
responseFields(
178189
fieldWithPath("id")
179190
.type(JsonFieldType.NUMBER)
180-
.description("The person's id"),
191+
.description("The person's id")
192+
.attributes(key("constraints").value(constraintDescriptionForProperty("id"))),
181193
fieldWithPath("name")
182194
.type(JsonFieldType.STRING)
183-
.description("The person's name"))));
195+
.description("The person's name")
196+
.attributes(key("constraints").value(constraintDescriptionForProperty("name"))))));
184197
}
185198

186199
@Test
@@ -240,10 +253,12 @@ void handleCreate() {
240253
responseFields(
241254
fieldWithPath("id")
242255
.type(JsonFieldType.NUMBER)
243-
.description("The person's id"),
256+
.description("The person's id")
257+
.attributes(key("constraints").value(constraintDescriptionForProperty("id"))),
244258
fieldWithPath("name")
245259
.type(JsonFieldType.STRING)
246-
.description("The person's name"))));
260+
.description("The person's name")
261+
.attributes(key("constraints").value(constraintDescriptionForProperty("name"))))));
247262
}
248263

249264
@Test
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
|===
2+
|Path|Type|Description|Constraints
3+
4+
{{#fields}}
5+
|{{path}}
6+
|{{type}}
7+
|{{description}}
8+
|{{constraints}}
9+
10+
{{/fields}}
11+
|===

0 commit comments

Comments
 (0)