Skip to content

Commit f76bc8d

Browse files
Swagger documentation comments
1 parent 4142adc commit f76bc8d

File tree

11 files changed

+157
-33
lines changed

11 files changed

+157
-33
lines changed

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/compiler/ModelCompiler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,10 @@ private static Map<String, Long> groupingByMethodName(List<JaxrsMethodModel> met
385385
private TsMethodModel processJaxrsMethod(SymbolTable symbolTable, String pathPrefix, Symbol responseSymbol, JaxrsMethodModel method, boolean createLongName, TsType optionsType, boolean implement) {
386386
final String path = Utils.joinPath(pathPrefix, method.getPath());
387387
final PathTemplate pathTemplate = PathTemplate.parse(path);
388-
final List<String> comments = new ArrayList<>();
389-
comments.add("HTTP " + method.getHttpMethod() + " /" + path);
390-
comments.add("Java method: " + method.getOriginClass().getName() + "." + method.getName());
388+
final List<String> comments = Utils.concat(method.getComments(), Arrays.asList(
389+
"HTTP " + method.getHttpMethod() + " /" + path,
390+
"Java method: " + method.getOriginClass().getName() + "." + method.getName()
391+
));
391392
final List<TsParameterModel> parameters = new ArrayList<>();
392393
// path params
393394
for (MethodParameterModel parameter : method.getPathParams()) {

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/parser/Javadoc.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package cz.habarta.typescript.generator.parser;
33

44
import cz.habarta.typescript.generator.compiler.EnumMemberModel;
5+
import cz.habarta.typescript.generator.util.Utils;
56
import cz.habarta.typescript.generator.xmldoclet.Class;
67
import cz.habarta.typescript.generator.xmldoclet.Enum;
78
import cz.habarta.typescript.generator.xmldoclet.EnumConstant;
@@ -73,7 +74,7 @@ private BeanModel enrichBean(BeanModel bean, String beanComment, List<TagInfo> t
7374
final PropertyModel enrichedProperty = enrichProperty(property, dFields, dMethods);
7475
enrichedProperties.add(enrichedProperty);
7576
}
76-
return bean.withProperties(enrichedProperties).withComments(concat(getComments(beanComment, tags), bean.getComments()));
77+
return bean.withProperties(enrichedProperties).withComments(Utils.concat(getComments(beanComment, tags), bean.getComments()));
7778
}
7879

7980
private PropertyModel enrichProperty(PropertyModel property, List<Field> dFields, List<Method> dMethods) {
@@ -104,7 +105,7 @@ private <T> EnumModel<T> enrichEnum(EnumModel<T> enumModel) {
104105
}
105106
final String enumComment = dEnum != null ? dEnum.getComment() : null;
106107
final List<TagInfo> tags = dEnum != null ? dEnum.getTag() : null;
107-
return enumModel.withMembers(enrichedMembers).withComments(concat(getComments(enumComment, tags), enumModel.getComments()));
108+
return enumModel.withMembers(enrichedMembers).withComments(Utils.concat(getComments(enumComment, tags), enumModel.getComments()));
108109
}
109110

110111
private <T> EnumMemberModel<T> enrichEnumMember(EnumMemberModel<T> enumMember, Enum dEnum) {
@@ -216,13 +217,4 @@ private static List<String> splitMultiline(String comments) {
216217
return result;
217218
}
218219

219-
private static <T> List<T> concat(List<? extends T> list1, List<? extends T> list2) {
220-
if (list1 == null && list2 == null) {
221-
return null;
222-
}
223-
final List<T> result = new ArrayList<>();
224-
if (list1 != null) result.addAll(list1);
225-
if (list2 != null) result.addAll(list2);
226-
return result;
227-
}
228220
}

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/parser/JaxrsApplicationParser.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,10 @@ private void parseResourceMethod(Result result, ResourceContext context, Class<?
130130
? new SwaggerOperation()
131131
: Swagger.parseSwaggerAnnotations(method);
132132
if (swaggerOperation.possibleResponses != null) {
133-
for (Type response : swaggerOperation.possibleResponses) {
134-
foundType(result, response, resourceClass, method.getName());
133+
for (SwaggerResponse response : swaggerOperation.possibleResponses) {
134+
if (response.responseType != null) {
135+
foundType(result, response.responseType, resourceClass, method.getName());
136+
}
135137
}
136138
}
137139
if (swaggerOperation.hidden) {
@@ -168,8 +170,8 @@ private void parseResourceMethod(Result result, ResourceContext context, Class<?
168170
if (returnType == void.class) {
169171
modelReturnType = returnType;
170172
} else if (returnType == Response.class) {
171-
if (swaggerOperation.response != null) {
172-
modelReturnType = swaggerOperation.response;
173+
if (swaggerOperation.responseType != null) {
174+
modelReturnType = swaggerOperation.responseType;
173175
foundType(result, modelReturnType, resourceClass, method.getName());
174176
} else {
175177
modelReturnType = Object.class;
@@ -182,8 +184,10 @@ private void parseResourceMethod(Result result, ResourceContext context, Class<?
182184
modelReturnType = genericReturnType;
183185
foundType(result, modelReturnType, resourceClass, method.getName());
184186
}
187+
// comments
188+
final List<String> comments = Swagger.getOperationComments(swaggerOperation);
185189
// create method
186-
model.getMethods().add(new JaxrsMethodModel(resourceClass, method.getName(), modelReturnType, httpMethod.value(), context.path, pathParams, queryParams, entityParameter));
190+
model.getMethods().add(new JaxrsMethodModel(resourceClass, method.getName(), modelReturnType, httpMethod.value(), context.path, pathParams, queryParams, entityParameter, comments));
187191
}
188192
// JAX-RS specification - 3.4.1 Sub Resources
189193
if (pathAnnotation != null && httpMethod == null) {

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/parser/JaxrsMethodModel.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ public class JaxrsMethodModel extends MethodModel {
1414
private final MethodParameterModel entityParam;
1515

1616
public JaxrsMethodModel(Class<?> originClass, String name, Type returnType,
17-
String httpMethod, String path, List<MethodParameterModel> pathParams, List<MethodParameterModel> queryParams, MethodParameterModel entityParam) {
18-
super(originClass, name, null, returnType);
17+
String httpMethod, String path, List<MethodParameterModel> pathParams, List<MethodParameterModel> queryParams, MethodParameterModel entityParam,
18+
List<String> comments) {
19+
super(originClass, name, null, returnType, comments);
1920
this.httpMethod = httpMethod;
2021
this.path = path;
2122
this.pathParams = pathParams;

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/parser/MethodModel.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ public class MethodModel {
1212
private final String name;
1313
private final List<MethodParameterModel> parameters;
1414
private final Type returnType;
15+
private final List<String> comments;
1516

16-
public MethodModel(Class<?> originClass, String name, List<MethodParameterModel> parameters, Type returnType) {
17+
public MethodModel(Class<?> originClass, String name, List<MethodParameterModel> parameters, Type returnType, List<String> comments) {
1718
this.originClass = originClass;
1819
this.name = name;
1920
this.parameters = parameters != null ? parameters : Collections.<MethodParameterModel>emptyList();
2021
this.returnType = returnType;
22+
this.comments = comments;
2123
}
2224

2325
public Class<?> getOriginClass() {
@@ -36,4 +38,8 @@ public Type getReturnType() {
3638
return returnType;
3739
}
3840

41+
public List<String> getComments() {
42+
return comments;
43+
}
44+
3945
}

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/parser/ModelParser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public Model parseModel(Type type) {
3030
public Model parseModel(List<SourceType<Type>> types) {
3131
typeQueue.addAll(types);
3232
final Model model = parseQueue();
33-
final Model modelWithJavadoc = javadoc.enrichModel(model);
33+
final Model modelWithSwaggerDoc = Swagger.enrichModel(model);
34+
final Model modelWithJavadoc = javadoc.enrichModel(modelWithSwaggerDoc);
3435
return modelWithJavadoc;
3536
}
3637

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/parser/Swagger.java

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package cz.habarta.typescript.generator.parser;
33

44
import cz.habarta.typescript.generator.util.Utils;
5+
import java.lang.reflect.AnnotatedElement;
56
import java.lang.reflect.Method;
67
import java.util.*;
78

@@ -17,21 +18,23 @@ static SwaggerOperation parseSwaggerAnnotations(Method method) {
1718
final Class<?> response = Utils.getAnnotationElementValue(apiOperation, "response", Class.class);
1819
final String responseContainer = Utils.getAnnotationElementValue(apiOperation, "responseContainer", String.class);
1920
if (responseContainer == null || responseContainer.isEmpty()) {
20-
swaggerOperation.response = response;
21+
swaggerOperation.responseType = response;
2122
} else {
2223
switch (responseContainer) {
2324
case "List":
24-
swaggerOperation.response = Utils.createParameterizedType(List.class, response);
25+
swaggerOperation.responseType = Utils.createParameterizedType(List.class, response);
2526
break;
2627
case "Set":
27-
swaggerOperation.response = Utils.createParameterizedType(Set.class, response);
28+
swaggerOperation.responseType = Utils.createParameterizedType(Set.class, response);
2829
break;
2930
case "Map":
30-
swaggerOperation.response = Utils.createParameterizedType(Map.class, String.class, response);
31+
swaggerOperation.responseType = Utils.createParameterizedType(Map.class, String.class, response);
3132
break;
3233
}
3334
}
3435
swaggerOperation.hidden = Utils.getAnnotationElementValue(apiOperation, "hidden", Boolean.class);
36+
swaggerOperation.comment = Utils.getAnnotationElementValue(apiOperation, "value", String.class);
37+
swaggerOperation.comment = swaggerOperation.comment.isEmpty() ? null : swaggerOperation.comment;
3538
}
3639
}
3740
// @ApiResponses
@@ -40,11 +43,59 @@ static SwaggerOperation parseSwaggerAnnotations(Method method) {
4043
if (apiResponses != null) {
4144
swaggerOperation.possibleResponses = new ArrayList<>();
4245
for (Object apiResponse : apiResponses) {
43-
swaggerOperation.possibleResponses.add(Utils.getAnnotationElementValue(apiResponse, "response", Class.class));
46+
final SwaggerResponse response = new SwaggerResponse();
47+
response.code = Utils.getAnnotationElementValue(apiResponse, "code", Integer.class);
48+
response.comment = Utils.getAnnotationElementValue(apiResponse, "message", String.class);
49+
response.responseType = Utils.getAnnotationElementValue(apiResponse, "response", Class.class);
50+
swaggerOperation.possibleResponses.add(response);
4451
}
4552
}
4653
}
4754
return swaggerOperation;
4855
}
4956

57+
static List<String> getOperationComments(SwaggerOperation operation) {
58+
final List<String> comments = new ArrayList<>();
59+
if (operation.comment != null) {
60+
comments.add(operation.comment);
61+
}
62+
if (operation.possibleResponses != null) {
63+
for (SwaggerResponse response : operation.possibleResponses) {
64+
comments.add(String.format("Response code %s - %s", response.code, response.comment));
65+
}
66+
}
67+
return comments.isEmpty() ? null : comments;
68+
}
69+
70+
public static Model enrichModel(Model model) {
71+
final List<BeanModel> dBeans = new ArrayList<>();
72+
for (BeanModel bean : model.getBeans()) {
73+
final BeanModel dBean = enrichBean(bean);
74+
dBeans.add(dBean);
75+
}
76+
return new Model(dBeans, model.getEnums(), model.getJaxrsApplication());
77+
}
78+
79+
private static BeanModel enrichBean(BeanModel bean) {
80+
final List<PropertyModel> enrichedProperties = new ArrayList<>();
81+
for (PropertyModel property : bean.getProperties()) {
82+
final PropertyModel enrichedProperty = enrichProperty(property);
83+
enrichedProperties.add(enrichedProperty);
84+
}
85+
final String comment = Utils.getAnnotationElementValue(bean.getOrigin(), "io.swagger.annotations.ApiModel", "description", String.class);
86+
final List<String> comments = comment != null && !comment.isEmpty() ? Arrays.asList(comment) : null;
87+
return bean.withProperties(enrichedProperties).withComments(Utils.concat(comments, bean.getComments()));
88+
}
89+
90+
private static PropertyModel enrichProperty(PropertyModel property) {
91+
if (property.getOriginalMember() instanceof AnnotatedElement) {
92+
final AnnotatedElement annotatedElement = (AnnotatedElement) property.getOriginalMember();
93+
final String comment = Utils.getAnnotationElementValue(annotatedElement, "io.swagger.annotations.ApiModelProperty", "value", String.class);
94+
final List<String> comments = comment != null && !comment.isEmpty() ? Arrays.asList(comment) : null;
95+
return property.withComments(Utils.concat(comments, property.getComments()));
96+
} else {
97+
return property;
98+
}
99+
}
100+
50101
}

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/parser/SwaggerOperation.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77

88
public class SwaggerOperation {
9-
public Type response;
10-
public List<Type> possibleResponses;
9+
public Type responseType;
10+
public List<SwaggerResponse> possibleResponses;
1111
public boolean hidden;
12+
public String comment;
1213
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
package cz.habarta.typescript.generator.parser;
3+
4+
import java.lang.reflect.Type;
5+
6+
7+
public class SwaggerResponse {
8+
public int code;
9+
public String comment;
10+
public Type responseType;
11+
}

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/util/Utils.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ public static <T> T getAnnotationElementValue(AnnotatedElement annotatedElement,
7171
}
7272

7373
public static Object getAnnotation(AnnotatedElement annotatedElement, String annotationClassName) {
74-
for (Annotation annotation : annotatedElement.getAnnotations()) {
75-
if (annotation.annotationType().getName().equals(annotationClassName)) {
76-
return annotation;
74+
if (annotatedElement != null) {
75+
for (Annotation annotation : annotatedElement.getAnnotations()) {
76+
if (annotation.annotationType().getName().equals(annotationClassName)) {
77+
return annotation;
78+
}
7779
}
7880
}
7981
return null;
@@ -139,6 +141,16 @@ public int hashCode() {
139141
};
140142
}
141143

144+
public static <T> List<T> concat(List<? extends T> list1, List<? extends T> list2) {
145+
if (list1 == null && list2 == null) {
146+
return null;
147+
}
148+
final List<T> result = new ArrayList<>();
149+
if (list1 != null) result.addAll(list1);
150+
if (list2 != null) result.addAll(list2);
151+
return result;
152+
}
153+
142154
public static <T> List<T> listFromNullable(T item) {
143155
return item != null ? Arrays.asList(item) : Collections.<T>emptyList();
144156
}

0 commit comments

Comments
 (0)