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

Commit e0f8482

Browse files
authored
array with item type (#158)
* array with item type * regen docs
1 parent a75a096 commit e0f8482

File tree

8 files changed

+192
-98
lines changed

8 files changed

+192
-98
lines changed

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1637,7 +1637,7 @@ <h4 id="contributing-building-build"><a class="link" href="#contributing-buildin
16371637
</div>
16381638
<div id="footer">
16391639
<div id="footer-text">
1640-
Last updated 2017-10-30 09:01:48 CET
1640+
Last updated 2017-10-31 14:21:24 CET
16411641
</div>
16421642
</div>
16431643
<link rel="stylesheet" href="highlight/styles/github.min.css">

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/jackson/FieldDocumentationVisitorWrapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package capital.scalable.restdocs.jackson;
1818

19+
import static capital.scalable.restdocs.util.TypeUtil.determineArrayOfType;
1920
import static capital.scalable.restdocs.util.TypeUtil.resolveAllTypes;
2021
import static org.slf4j.LoggerFactory.getLogger;
2122

@@ -101,8 +102,8 @@ public JsonObjectFormatVisitor expectObjectFormat(JavaType type) throws JsonMapp
101102
@Override
102103
public JsonArrayFormatVisitor expectArrayFormat(JavaType arrayType)
103104
throws JsonMappingException {
104-
addFieldIfPresent("Array");
105105
JavaType contentType = arrayType.getContentType();
106+
addFieldIfPresent(determineArrayOfType(contentType));
106107
if (contentType != null && shouldExpand() && (topLevelPath() || !wasVisited(contentType))) {
107108
log.trace("({}) {} expanding array", path, toString(contentType));
108109
// do not add this type to visited now, it will be done in expectObjectFormat for

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static capital.scalable.restdocs.util.FieldUtil.fromGetter;
44
import static capital.scalable.restdocs.util.FieldUtil.isGetter;
55
import static org.apache.commons.lang3.ClassUtils.primitiveToWrapper;
6+
import static org.apache.commons.lang3.StringUtils.chop;
67

78
import java.lang.reflect.Field;
89
import java.util.ArrayList;
@@ -20,7 +21,31 @@ private TypeUtil() {
2021

2122
public static String determineTypeName(Class<?> type) {
2223
// should return the same as FieldDocumentationVisitorWrapper
23-
switch (primitiveToWrapper(type).getCanonicalName()) {
24+
boolean isArray = false;
25+
String canonicalName = primitiveToWrapper(type).getCanonicalName();
26+
if (canonicalName.endsWith("[]")) {
27+
isArray = true;
28+
canonicalName = chop(chop(canonicalName));
29+
}
30+
31+
String finalName = getSimpleName(canonicalName);
32+
if (isArray) {
33+
return "Array[" + finalName + "]";
34+
} else {
35+
return finalName;
36+
}
37+
}
38+
39+
public static String determineArrayOfType(JavaType contentType) {
40+
if (contentType != null) {
41+
return "Array[" + determineTypeName(contentType.getRawClass()) + "]";
42+
} else {
43+
return "Array[Object]";
44+
}
45+
}
46+
47+
private static String getSimpleName(String canonicalName) {
48+
switch (canonicalName) {
2449
case "java.lang.Byte":
2550
case "java.lang.Short":
2651
case "java.lang.Long":
@@ -32,11 +57,12 @@ public static String determineTypeName(Class<?> type) {
3257
case "java.math.BigDecimal":
3358
return "Decimal";
3459
case "java.lang.Character":
60+
case "java.lang.String":
3561
return "String";
3662
case "java.lang.Boolean":
3763
return "Boolean";
3864
default:
39-
return type.getSimpleName();
65+
return "Object";
4066
}
4167
}
4268

spring-auto-restdocs-core/src/test/java/capital/scalable/restdocs/jackson/FieldDocumentationGeneratorTest.java

Lines changed: 61 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ public class FieldDocumentationGeneratorTest {
7070
public void testGenerateDocumentationForBasicTypes() throws Exception {
7171
// given
7272
ObjectMapper mapper = createMapper();
73-
mockFieldComment(BasicTypes.class, "stringField", "A string");
74-
mockFieldComment(BasicTypes.class, "booleanField", "A boolean");
75-
mockFieldComment(BasicTypes.class, "numberField1", "An integer");
76-
mockFieldComment(BasicTypes.class, "numberField2", "A decimal");
73+
mockFieldComment(BasicTypes.class, "string", "A string");
74+
mockFieldComment(BasicTypes.class, "bool", "A boolean");
75+
mockFieldComment(BasicTypes.class, "number", "An integer");
76+
mockFieldComment(BasicTypes.class, "decimal", "A decimal");
7777

7878
FieldDocumentationGenerator generator =
7979
new FieldDocumentationGenerator(mapper.writer(), mapper.getDeserializationConfig(),
@@ -85,20 +85,20 @@ public void testGenerateDocumentationForBasicTypes() throws Exception {
8585
.generateDocumentation(type, mapper.getTypeFactory()));
8686
// then
8787
assertThat(result.size(), is(4));
88-
assertThat(result.get(0), is(descriptor("stringField", "String", "A string", "true")));
89-
assertThat(result.get(1), is(descriptor("booleanField", "Boolean", "A boolean", "true")));
90-
assertThat(result.get(2), is(descriptor("numberField1", "Integer", "An integer", "true")));
91-
assertThat(result.get(3), is(descriptor("numberField2", "Decimal", "A decimal", "true")));
88+
assertThat(result.get(0), is(descriptor("string", "String", "A string", "true")));
89+
assertThat(result.get(1), is(descriptor("bool", "Boolean", "A boolean", "true")));
90+
assertThat(result.get(2), is(descriptor("number", "Integer", "An integer", "true")));
91+
assertThat(result.get(3), is(descriptor("decimal", "Decimal", "A decimal", "true")));
9292
}
9393

9494
@Test
9595
public void testGenerateDocumentationForPrimitiveTypes() throws Exception {
9696
// given
9797
ObjectMapper mapper = createMapper();
98-
mockFieldComment(PrimitiveTypes.class, "stringField", "A string");
99-
mockFieldComment(PrimitiveTypes.class, "booleanField", "A boolean");
100-
mockFieldComment(PrimitiveTypes.class, "numberField1", "An integer");
101-
mockFieldComment(PrimitiveTypes.class, "numberField2", "A decimal");
98+
mockFieldComment(PrimitiveTypes.class, "string", "A string");
99+
mockFieldComment(PrimitiveTypes.class, "bool", "A boolean");
100+
mockFieldComment(PrimitiveTypes.class, "number", "An integer");
101+
mockFieldComment(PrimitiveTypes.class, "decimal", "A decimal");
102102

103103
FieldDocumentationGenerator generator =
104104
new FieldDocumentationGenerator(mapper.writer(),
@@ -110,10 +110,10 @@ public void testGenerateDocumentationForPrimitiveTypes() throws Exception {
110110
.generateDocumentation(type, mapper.getTypeFactory()));
111111
// then
112112
assertThat(result.size(), is(4));
113-
assertThat(result.get(0), is(descriptor("stringField", "Array", "A string", "true")));
114-
assertThat(result.get(1), is(descriptor("booleanField", "Boolean", "A boolean", "true")));
115-
assertThat(result.get(2), is(descriptor("numberField1", "Integer", "An integer", "true")));
116-
assertThat(result.get(3), is(descriptor("numberField2", "Decimal", "A decimal", "true")));
113+
assertThat(result.get(0), is(descriptor("string", "Array[String]", "A string", "true")));
114+
assertThat(result.get(1), is(descriptor("bool", "Boolean", "A boolean", "true")));
115+
assertThat(result.get(2), is(descriptor("number", "Integer", "An integer", "true")));
116+
assertThat(result.get(3), is(descriptor("decimal", "Decimal", "A decimal", "true")));
117117

118118
// when change deserialization config
119119
mapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true);
@@ -125,22 +125,22 @@ public void testGenerateDocumentationForPrimitiveTypes() throws Exception {
125125

126126
// then
127127
assertThat(result.size(), is(4));
128-
assertThat(result.get(0), is(descriptor("stringField", "Array", "A string", "true")));
129-
assertThat(result.get(1), is(descriptor("booleanField", "Boolean", "A boolean", "false")));
130-
assertThat(result.get(2), is(descriptor("numberField1", "Integer", "An integer", "false")));
131-
assertThat(result.get(3), is(descriptor("numberField2", "Decimal", "A decimal", "false")));
128+
assertThat(result.get(0), is(descriptor("string", "Array[String]", "A string", "true")));
129+
assertThat(result.get(1), is(descriptor("bool", "Boolean", "A boolean", "false")));
130+
assertThat(result.get(2), is(descriptor("number", "Integer", "An integer", "false")));
131+
assertThat(result.get(3), is(descriptor("decimal", "Decimal", "A decimal", "false")));
132132
}
133133

134134
@Test
135135
public void testGenerateDocumentationForComposedTypes() throws Exception {
136136
// given
137137
ObjectMapper mapper = createMapper();
138-
mockFieldComment(ComposedTypes.class, "objectField", "An object");
139-
mockFieldComment(BasicTypes.class, "stringField", "A string");
140-
mockFieldComment(BasicTypes.class, "booleanField", "A boolean");
141-
mockFieldComment(BasicTypes.class, "numberField1", "An integer");
142-
mockFieldComment(BasicTypes.class, "numberField2", "A decimal");
143-
mockFieldComment(ComposedTypes.class, "arrayField", "An array");
138+
mockFieldComment(ComposedTypes.class, "object", "An object");
139+
mockFieldComment(BasicTypes.class, "string", "A string");
140+
mockFieldComment(BasicTypes.class, "bool", "A boolean");
141+
mockFieldComment(BasicTypes.class, "number", "An integer");
142+
mockFieldComment(BasicTypes.class, "decimal", "A decimal");
143+
mockFieldComment(ComposedTypes.class, "array", "An array");
144144

145145
FieldDocumentationGenerator generator =
146146
new FieldDocumentationGenerator(mapper.writer(),
@@ -154,25 +154,25 @@ public void testGenerateDocumentationForComposedTypes() throws Exception {
154154
// then
155155
assertThat(result.size(), is(10));
156156
assertThat(result.get(0),
157-
is(descriptor("objectField", "Object", "An object", "true")));
157+
is(descriptor("object", "Object", "An object", "true")));
158158
assertThat(result.get(1),
159-
is(descriptor("objectField.stringField", "String", "A string", "true")));
159+
is(descriptor("object.string", "String", "A string", "true")));
160160
assertThat(result.get(2),
161-
is(descriptor("objectField.booleanField", "Boolean", "A boolean", "true")));
161+
is(descriptor("object.bool", "Boolean", "A boolean", "true")));
162162
assertThat(result.get(3),
163-
is(descriptor("objectField.numberField1", "Integer", "An integer", "true")));
163+
is(descriptor("object.number", "Integer", "An integer", "true")));
164164
assertThat(result.get(4),
165-
is(descriptor("objectField.numberField2", "Decimal", "A decimal", "true")));
165+
is(descriptor("object.decimal", "Decimal", "A decimal", "true")));
166166
assertThat(result.get(5),
167-
is(descriptor("arrayField", "Array", "An array", "true")));
167+
is(descriptor("array", "Array[Object]", "An array", "true")));
168168
assertThat(result.get(6),
169-
is(descriptor("arrayField[].stringField", "String", "A string", "true")));
169+
is(descriptor("array[].string", "String", "A string", "true")));
170170
assertThat(result.get(7),
171-
is(descriptor("arrayField[].booleanField", "Boolean", "A boolean", "true")));
171+
is(descriptor("array[].bool", "Boolean", "A boolean", "true")));
172172
assertThat(result.get(8),
173-
is(descriptor("arrayField[].numberField1", "Integer", "An integer", "true")));
173+
is(descriptor("array[].number", "Integer", "An integer", "true")));
174174
assertThat(result.get(9),
175-
is(descriptor("arrayField[].numberField2", "Decimal", "A decimal", "true")));
175+
is(descriptor("array[].decimal", "Decimal", "A decimal", "true")));
176176
}
177177

178178
@Test
@@ -199,11 +199,12 @@ public void testGenerateDocumentationForNestedTypes() throws Exception {
199199
assertThat(result.get(0),
200200
is(descriptor("second", "Object", "2nd level", "true")));
201201
assertThat(result.get(1),
202-
is(descriptor("second.third", "Array", "3rd level", "true")));
202+
is(descriptor("second.third", "Array[Object]", "3rd level", "true")));
203203
assertThat(result.get(2),
204204
is(descriptor("second.third[].fourth", "Object", "4th level", "true")));
205205
assertThat(result.get(3),
206-
is(descriptor("second.third[].fourth.fifth", "Array", "5th level", "true")));
206+
is(descriptor("second.third[].fourth.fifth", "Array[Object]", "5th level",
207+
"true")));
207208
assertThat(result.get(4),
208209
is(descriptor("second.third[].fourth.fifth[].last", "Integer", "An integer",
209210
"true")));
@@ -223,21 +224,21 @@ public void testGenerateDocumentationForRecursiveTypes() throws Exception {
223224

224225
// then
225226
assertThat(result.size(), is(16));
226-
assertThat(result.get(0), is(descriptor("sub1", "Array", null, "true")));
227+
assertThat(result.get(0), is(descriptor("sub1", "Array[Object]", null, "true")));
227228
assertThat(result.get(1), is(descriptor("sub2", "Object", null, "true")));
228-
assertThat(result.get(2), is(descriptor("sub3", "Array", null, "true")));
229+
assertThat(result.get(2), is(descriptor("sub3", "Array[Object]", null, "true")));
229230
assertThat(result.get(3), is(descriptor("sub4", "Object", null, "true")));
230-
assertThat(result.get(4), is(descriptor("sub5", "Array", null, "true")));
231+
assertThat(result.get(4), is(descriptor("sub5", "Array[Object]", null, "true")));
231232
assertThat(result.get(5), is(descriptor("sub6", "Object", null, "true")));
232-
assertThat(result.get(6), is(descriptor("sub7", "Array", null, "true")));
233-
assertThat(result.get(7), is(descriptor("sub7[].sub1", "Array", null, "true")));
233+
assertThat(result.get(6), is(descriptor("sub7", "Array[Object]", null, "true")));
234+
assertThat(result.get(7), is(descriptor("sub7[].sub1", "Array[Object]", null, "true")));
234235
assertThat(result.get(8), is(descriptor("sub7[].sub2", "Object", null, "true")));
235-
assertThat(result.get(9), is(descriptor("sub7[].sub3", "Array", null, "true")));
236+
assertThat(result.get(9), is(descriptor("sub7[].sub3", "Array[Object]", null, "true")));
236237
assertThat(result.get(10), is(descriptor("sub7[].sub4", "Object", null, "true")));
237238
assertThat(result.get(11), is(descriptor("sub8", "Object", null, "true")));
238-
assertThat(result.get(12), is(descriptor("sub8.sub1", "Array", null, "true")));
239+
assertThat(result.get(12), is(descriptor("sub8.sub1", "Array[Object]", null, "true")));
239240
assertThat(result.get(13), is(descriptor("sub8.sub2", "Object", null, "true")));
240-
assertThat(result.get(14), is(descriptor("sub8.sub3", "Array", null, "true")));
241+
assertThat(result.get(14), is(descriptor("sub8.sub3", "Array[Object]", null, "true")));
241242
assertThat(result.get(15), is(descriptor("sub8.sub4", "Object", null, "true")));
242243
}
243244

@@ -363,12 +364,12 @@ public void testGenerateDocumentationForConstraints() throws Exception {
363364
assertThat(fieldDescriptions.get(1),
364365
is(descriptor("type", "Integer", null, "false", "A constraint for type")));
365366
assertThat(fieldDescriptions.get(2),
366-
is(descriptor("params", "Array", null, "false")));
367+
is(descriptor("params", "Array[Object]", null, "false")));
367368
assertThat(fieldDescriptions.get(3),
368369
is(descriptor("params[].value", "String", null, "false",
369370
"A constraint1 for value", "A constraint2 for value")));
370371
assertThat(fieldDescriptions.get(4),
371-
is(descriptor("flags", "Array", null, "true")));
372+
is(descriptor("flags", "Array[Boolean]", null, "true")));
372373
}
373374

374375
@Test
@@ -404,11 +405,11 @@ public void testGenerateDocumentationForJacksonSubTypes() throws Exception {
404405
assertThat(fieldDescriptions.get(1),
405406
is(descriptor("name", "String", "A name", "true")));
406407
assertThat(fieldDescriptions.get(2),
407-
is(descriptor("base1", "Array", "A base 1", "true")));
408+
is(descriptor("base1", "Array[Object]", "A base 1", "true")));
408409
assertThat(fieldDescriptions.get(3),
409410
is(descriptor("base2", "Object", "A base 2", "true")));
410411
assertThat(fieldDescriptions.get(4),
411-
is(descriptor("base3", "Array", "A base 3", "true")));
412+
is(descriptor("base3", "Array[Object]", "A base 3", "true")));
412413
assertThat(fieldDescriptions.get(5),
413414
is(descriptor("base3[].clazz", "String", "A clazz", "true")));
414415
assertThat(fieldDescriptions.get(6),
@@ -475,23 +476,23 @@ private List<ExtendedFieldDescriptor> cast(List<FieldDescriptor> original) {
475476
}
476477

477478
private static class BasicTypes {
478-
private String stringField;
479-
private Boolean booleanField;
480-
private Integer numberField1;
481-
private Double numberField2;
479+
private String string;
480+
private Boolean bool;
481+
private Integer number;
482+
private Double decimal;
482483

483484
}
484485

485486
private static class PrimitiveTypes {
486-
private char[] stringField; // array is nullable
487-
private boolean booleanField;
488-
private int numberField1;
489-
private double numberField2;
487+
private char[] string; // array is nullable
488+
private boolean bool;
489+
private int number;
490+
private double decimal;
490491
}
491492

492493
private static class ComposedTypes {
493-
private BasicTypes objectField;
494-
private List<BasicTypes> arrayField;
494+
private BasicTypes object;
495+
private List<BasicTypes> array;
495496
}
496497

497498
private static class FirstLevel {

0 commit comments

Comments
 (0)