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

Commit ba89125

Browse files
authored
Support enums in path and request parameters (#191)
* Support enums in path and request parameters * Move enum down to the other types
1 parent ab70d80 commit ba89125

File tree

6 files changed

+115
-4
lines changed

6 files changed

+115
-4
lines changed

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/constraints/ConstraintReaderImpl.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public List<String> getConstraintMessages(MethodParameter param) {
8787
constraintMessages.add(
8888
constraintDescriptionResolver.resolveDescription(constraint));
8989
}
90+
constraintMessages.addAll(getEnumConstraintMessage(param));
9091
Collections.sort(constraintMessages);
9192
return constraintMessages;
9293
}
@@ -98,7 +99,14 @@ private List<String> getEnumConstraintMessage(Class<?> javaBaseClass, String jav
9899
return emptyList();
99100
}
100101

101-
Class<?> rawClass = field.getType();
102+
return getEnumConstraintMessage(field.getType());
103+
}
104+
105+
private List<String> getEnumConstraintMessage(MethodParameter param) {
106+
return getEnumConstraintMessage(param.getParameterType());
107+
}
108+
109+
private List<String> getEnumConstraintMessage(Class<?> rawClass) {
102110
if (!rawClass.isEnum()) {
103111
return emptyList();
104112
}

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/util/TypeUtil.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ private TypeUtil() {
2121

2222
public static String determineTypeName(Class<?> type) {
2323
// should return the same as FieldDocumentationVisitorWrapper
24+
if (type.isEnum()) {
25+
return "String";
26+
}
2427
boolean isArray = false;
2528
String canonicalName = primitiveToWrapper(type).getCanonicalName();
2629
if (canonicalName.endsWith("[]")) {

spring-auto-restdocs-core/src/test/java/capital/scalable/restdocs/constraints/ConstraintReaderImplTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public void getOptionalMessages() {
142142
public void getParameterConstraintMessages() throws NoSuchMethodException {
143143
ConstraintReader reader = createWithValidation();
144144

145-
Method method = MethodTest.class.getMethod("exec", Integer.class, String.class);
145+
Method method = MethodTest.class.getMethod("exec", Integer.class, String.class, Enum1.class);
146146

147147
List<String> messages = reader.getConstraintMessages(new MethodParameter(method, 0));
148148
assertThat(messages.size(), is(2));
@@ -152,6 +152,10 @@ public void getParameterConstraintMessages() throws NoSuchMethodException {
152152
messages = reader.getConstraintMessages(new MethodParameter(method, 1));
153153
assertThat(messages.size(), is(1));
154154
assertThat(messages.get(0), is("Must be one of [all, single]"));
155+
156+
messages = reader.getConstraintMessages(new MethodParameter(method, 2));
157+
assertThat(messages.size(), is(1));
158+
assertThat(messages.get(0), is("Must be one of [ONE, TWO]"));
155159
}
156160

157161
@Test
@@ -169,7 +173,7 @@ public void getOptionalMessages_validationNotPresent() {
169173
@Test
170174
public void getParameterConstraintMessages_validationNotPresent() throws NoSuchMethodException {
171175
ConstraintReaderImpl reader = createWithoutValidation();
172-
Method method = MethodTest.class.getMethod("exec", Integer.class, String.class);
176+
Method method = MethodTest.class.getMethod("exec", Integer.class, String.class, Enum1.class);
173177
assertThat(reader.getConstraintMessages(new MethodParameter(method, 0)).size(), is(0));
174178
}
175179

@@ -226,7 +230,8 @@ public void exec(
226230
@Min(value = 1, groups = Create.class)
227231
@Max(value = 2, groups = Update.class) Integer count,
228232
@NotBlank
229-
@OneOf({"all", "single"}) String type) {
233+
@OneOf({"all", "single"}) String type,
234+
Enum1 enumeration) {
230235
}
231236
}
232237
}

spring-auto-restdocs-core/src/test/java/capital/scalable/restdocs/payload/JacksonRequestFieldSnippetTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,24 @@ public void simpleRequest() throws Exception {
8989
.build());
9090
}
9191

92+
@Test
93+
public void simpleRequestWithEnum() throws Exception {
94+
HandlerMethod handlerMethod = createHandlerMethod("addItemWithWeight", ItemWithWeight.class);
95+
mockFieldComment(ItemWithWeight.class, "weight", "An enum");
96+
mockConstraintMessage(ItemWithWeight.class, "weight", "Must be one of [LIGHT, HEAVY]");
97+
98+
this.snippets.expect(REQUEST_FIELDS).withContents(
99+
tableWithHeader("Path", "Type", "Optional", "Description")
100+
.row("weight", "String", "true", "An enum.\n\nMust be one of [LIGHT, HEAVY]."));
101+
102+
new JacksonRequestFieldSnippet().document(operationBuilder
103+
.attribute(HandlerMethod.class.getName(), handlerMethod)
104+
.attribute(ObjectMapper.class.getName(), mapper)
105+
.attribute(JavadocReader.class.getName(), javadocReader)
106+
.attribute(ConstraintReader.class.getName(), constraintReader)
107+
.build());
108+
}
109+
92110
@Test
93111
public void noRequestBody() throws Exception {
94112
HandlerMethod handlerMethod = new HandlerMethod(new TestResource(), "addItem2");
@@ -265,6 +283,10 @@ public void addItem(@RequestBody Item item) {
265283
// NOOP
266284
}
267285

286+
public void addItemWithWeight(@RequestBody ItemWithWeight item) {
287+
// NOOP
288+
}
289+
268290
public void addItems(@RequestBody List<Item> items) {
269291
// NOOP
270292
}
@@ -297,6 +319,14 @@ private static class Item {
297319
private Integer field2;
298320
}
299321

322+
private enum Weight {
323+
LIGHT, HEAVY
324+
}
325+
326+
private static class ItemWithWeight {
327+
private Weight weight;
328+
}
329+
300330
private static class DeprecatedItem {
301331
@Deprecated
302332
private int index;

spring-auto-restdocs-core/src/test/java/capital/scalable/restdocs/request/PathParametersSnippetTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919

2020
import static capital.scalable.restdocs.request.PathParametersSnippet.PATH_PARAMETERS;
21+
import static java.util.Collections.singletonList;
2122
import static org.hamcrest.CoreMatchers.equalTo;
2223
import static org.mockito.Mockito.mock;
2324
import static org.mockito.Mockito.when;
@@ -80,6 +81,24 @@ public void simpleRequest() throws Exception {
8081
.build());
8182
}
8283

84+
@Test
85+
public void simpleRequestWithEnum() throws Exception {
86+
HandlerMethod handlerMethod = createHandlerMethod("getItemsByShape", TestResource.Shape.class);
87+
initParameters(handlerMethod);
88+
mockParamComment("getItemsByShape", "shape", "An enum");
89+
mockConstraintMessage(handlerMethod.getMethodParameters()[0], "Must be one of [SPHERIC, SQUARE]");
90+
91+
this.snippets.expect(PATH_PARAMETERS).withContents(
92+
tableWithHeader("Parameter", "Type", "Optional", "Description")
93+
.row("shape", "String", "false", "An enum.\n\nMust be one of [SPHERIC, SQUARE]."));
94+
95+
new PathParametersSnippet().document(operationBuilder
96+
.attribute(HandlerMethod.class.getName(), handlerMethod)
97+
.attribute(JavadocReader.class.getName(), javadocReader)
98+
.attribute(ConstraintReader.class.getName(), constraintReader)
99+
.build());
100+
}
101+
83102
@Test
84103
public void noParameters() throws Exception {
85104
HandlerMethod handlerMethod = createHandlerMethod("addItem");
@@ -144,13 +163,22 @@ private void mockParamComment(String methodName, String parameterName, String co
144163
parameterName)).thenReturn(comment);
145164
}
146165

166+
private void mockConstraintMessage(MethodParameter param, String comment) {
167+
when(constraintReader.getConstraintMessages(param))
168+
.thenReturn(singletonList(comment));
169+
}
170+
147171
private HandlerMethod createHandlerMethod(String name, Class<?>... parameterTypes)
148172
throws NoSuchMethodException {
149173
return new HandlerMethod(new TestResource(), name, parameterTypes);
150174
}
151175

152176
private static class TestResource {
153177

178+
enum Shape {
179+
SPHERIC, SQUARE
180+
}
181+
154182
@PostMapping("/items/{id}/subitem/{subid}/{partId}/{yetAnotherId}")
155183
public void addItem(@PathVariable Integer id,
156184
@PathVariable("subid") String otherId,
@@ -165,6 +193,11 @@ public void addItem() {
165193
// NOOP
166194
}
167195

196+
@GetMapping("/items/{shape}")
197+
public void getItemsByShape(@PathVariable Shape shape) {
198+
// NOOP
199+
}
200+
168201
public void removeItem(@Deprecated @PathVariable int index) {
169202
}
170203
}

spring-auto-restdocs-core/src/test/java/capital/scalable/restdocs/request/RequestParametersSnippetTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import static capital.scalable.restdocs.payload.TableWithPrefixMatcher.tableWithPrefix;
2121
import static capital.scalable.restdocs.request.RequestParametersSnippet.REQUEST_PARAMETERS;
22+
import static java.util.Collections.singletonList;
2223
import static org.hamcrest.CoreMatchers.equalTo;
2324
import static org.mockito.Mockito.mock;
2425
import static org.mockito.Mockito.when;
@@ -159,6 +160,24 @@ public void simpleRequestWithCustomTypes() throws Exception {
159160
.build());
160161
}
161162

163+
@Test
164+
public void simpleRequestWithEnum() throws Exception {
165+
HandlerMethod handlerMethod = createHandlerMethod("searchItemBySize", TestResource.Size.class);
166+
initParameters(handlerMethod);
167+
mockParamComment("searchItemBySize", "size", "An enum");
168+
mockConstraintMessage(handlerMethod.getMethodParameters()[0], "Must be one of [SMALL, LARGE]");
169+
170+
this.snippets.expect(REQUEST_PARAMETERS).withContents(
171+
tableWithHeader("Parameter", "Type", "Optional", "Description")
172+
.row("size", "String", "false", "An enum.\n\nMust be one of [SMALL, LARGE]."));
173+
174+
new RequestParametersSnippet().document(operationBuilder
175+
.attribute(HandlerMethod.class.getName(), handlerMethod)
176+
.attribute(JavadocReader.class.getName(), javadocReader)
177+
.attribute(ConstraintReader.class.getName(), constraintReader)
178+
.build());
179+
}
180+
162181
@Test
163182
public void noParameters() throws Exception {
164183
HandlerMethod handlerMethod = createHandlerMethod("items");
@@ -254,6 +273,11 @@ private void mockParamComment(String methodName, String parameterName, String co
254273
parameterName)).thenReturn(comment);
255274
}
256275

276+
private void mockConstraintMessage(MethodParameter param, String comment) {
277+
when(constraintReader.getConstraintMessages(param))
278+
.thenReturn(singletonList(comment));
279+
}
280+
257281
private HandlerMethod createHandlerMethod(String name, Class<?>... parameterTypes)
258282
throws NoSuchMethodException {
259283
return new HandlerMethod(new TestResource(), name, parameterTypes);
@@ -269,6 +293,10 @@ private String paginationPrefix() {
269293

270294
private static class TestResource {
271295

296+
public enum Size {
297+
SMALL, LARGE
298+
}
299+
272300
public void searchItem(@RequestParam Integer type,
273301
@RequestParam(value = "text", required = false) String description) {
274302
// NOOP
@@ -286,6 +314,10 @@ public void searchItem2String(@RequestParam double param1, // required
286314
// NOOP
287315
}
288316

317+
public void searchItemBySize(@RequestParam Size size) {
318+
// NOOP
319+
}
320+
289321
public void items() {
290322
// NOOP
291323
}

0 commit comments

Comments
 (0)