Skip to content

Commit 6e337b4

Browse files
committed
api-docs - fixed toTypeDescription() impl to handle as much paramterized types than getTypeExpressionFor().
At the same time, provided MAP[x,y] definition support which was previously generating bad type description
1 parent 4921e05 commit 6e337b4

3 files changed

Lines changed: 53 additions & 32 deletions

File tree

‎restx-core-annotation-processor/src/main/java/restx/annotations/processor/RestxAnnotationProcessor.java‎

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.regex.Pattern;
2828

2929
import static restx.annotations.processor.TypeHelper.getTypeExpressionFor;
30+
import static restx.annotations.processor.TypeHelper.toTypeDescription;
3031

3132
/**
3233
* User: xavierhanin
@@ -401,34 +402,6 @@ private String toSchemaKey(String type) {
401402
}
402403
}
403404

404-
static String toTypeDescription(String type) {
405-
// see https://github.com/wordnik/swagger-core/wiki/datatypes
406-
boolean isList = false;
407-
Pattern p = Pattern.compile("java\\.lang\\.Iterable<(.+)>");
408-
Matcher m = p.matcher(type);
409-
if (m.matches()) {
410-
type = m.group(1);
411-
isList = true;
412-
}
413-
boolean primitive = type.startsWith("java.lang");
414-
type = type.substring(type.lastIndexOf('.') + 1);
415-
if ("Integer".equals(type)) {
416-
type = "int";
417-
}
418-
if (primitive) {
419-
type = type.toLowerCase();
420-
}
421-
if ("DateTime".equals(type) || "DateMidnight".equals(type)) {
422-
type = "Date";
423-
}
424-
425-
if (isList) {
426-
return "LIST[" + type + "]";
427-
} else {
428-
return type;
429-
}
430-
}
431-
432405
private Collection<ResourceMethodAnnotation> getResourceMethodAnnotationsInRound(RoundEnvironment roundEnv) {
433406
Collection<ResourceMethodAnnotation> methodAnnotations = Lists.newArrayList();
434407
for (Element resourceElem : roundEnv.getElementsAnnotatedWith(getRestAnnotationClass())) {

‎restx-core-annotation-processor/src/main/java/restx/annotations/processor/TypeHelper.java‎

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.google.common.base.Optional;
66
import com.google.common.collect.FluentIterable;
77
import com.google.common.collect.ImmutableList;
8+
import com.google.common.collect.ImmutableMap;
89
import com.google.common.collect.Sets;
910

1011
import java.util.*;
@@ -17,6 +18,11 @@
1718
*/
1819
public class TypeHelper {
1920
private static ImmutableList<String> PARSED_TYPES_DELIMITERS = ImmutableList.of(",", "<", ">");
21+
private static ImmutableMap<String, String> TYPE_DESCRIPTION_ALIASES = ImmutableMap.of(
22+
Integer.class.getCanonicalName(), "int",
23+
Iterable.class.getCanonicalName(), "LIST",
24+
List.class.getCanonicalName(), "LIST",
25+
Map.class.getCanonicalName(), "MAP");
2026
private static Pattern guavaOptionalPattern = Pattern.compile("\\Q" + Optional.class.getName() + "<\\E(.+)>");
2127
private static Pattern java8OptionalPattern = Pattern.compile("\\Qjava.util.Optional<\\E(.+)>");
2228
private static Set<String> RAW_TYPES_STR = Sets.newHashSet("byte", "short", "int", "long", "float", "double", "boolean", "char");
@@ -50,8 +56,43 @@ public String apply(ParsedType param) {
5056
}
5157
}
5258

53-
static String getTypeExpressionFor(String type) {
54-
StringTokenizer tokenizer = new StringTokenizer(type+"\n", Joiner.on("").join(PARSED_TYPES_DELIMITERS), true);
59+
static String toTypeDescription(ParsedType parsedType) {
60+
boolean aliasedType = TYPE_DESCRIPTION_ALIASES.containsKey(parsedType.className);
61+
String type;
62+
if(aliasedType) {
63+
type = TYPE_DESCRIPTION_ALIASES.get(parsedType.className);
64+
} else {
65+
boolean primitive = parsedType.className.startsWith("java.lang");
66+
type = parsedType.className.substring(parsedType.className.lastIndexOf('.') + 1);
67+
if (primitive) {
68+
type = type.toLowerCase();
69+
}
70+
if ("DateTime".equals(type) || "DateMidnight".equals(type)) {
71+
type = "Date";
72+
}
73+
}
74+
75+
if(parsedType.parameters.isEmpty()) { // We're on a raw type
76+
return type;
77+
} else { // We're on a parameterized type
78+
String parametersTypeDescriptions = FluentIterable.from(parsedType.parameters)
79+
.transform(new Function<ParsedType, String>() {
80+
@Override
81+
public String apply(ParsedType param) {
82+
return toTypeDescription(param);
83+
}
84+
}).join(Joiner.on(", "));
85+
86+
if(aliasedType) {
87+
return String.format("%s[%s]", type, parametersTypeDescriptions);
88+
} else {
89+
return String.format("%s<%s>", type, parametersTypeDescriptions);
90+
}
91+
}
92+
}
93+
94+
static ParsedType parseParameterizedType(String parameterizedType) {
95+
StringTokenizer tokenizer = new StringTokenizer(parameterizedType+"\n", Joiner.on("").join(PARSED_TYPES_DELIMITERS), true);
5596
String[] tokens = new String[tokenizer.countTokens()];
5697
ParsedType rootParsedType = null;
5798
ParsedType currentParsedType = null;
@@ -87,8 +128,15 @@ static String getTypeExpressionFor(String type) {
87128
}
88129
}
89130
}
131+
return rootParsedType;
132+
}
90133

91-
return getTypeExpressionFor(rootParsedType);
134+
static String toTypeDescription(String type) {
135+
return toTypeDescription(parseParameterizedType(type));
136+
}
137+
138+
static String getTypeExpressionFor(String type) {
139+
return getTypeExpressionFor(parseParameterizedType(type));
92140
}
93141

94142
static boolean isParameterizedType(String type) {

‎restx-core-annotation-processor/src/test/java/restx/annotations/processor/TypeHelperTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ public void should_produce_type_expression() throws Exception {
6565

6666
@Test
6767
public void should_produce_type_description() throws Exception {
68-
assertThat(RestxAnnotationProcessor.toTypeDescription(this.stringedType)).isEqualTo(this.expectedTypeDescription);
68+
assertThat(TypeHelper.toTypeDescription(this.stringedType)).isEqualTo(this.expectedTypeDescription);
6969
}
7070
}

0 commit comments

Comments
 (0)