Skip to content

Commit 4a5a4d2

Browse files
Javadoc comments for REST methods (#423)
1 parent adb6854 commit 4a5a4d2

File tree

5 files changed

+76
-8
lines changed

5 files changed

+76
-8
lines changed

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,21 @@ private static List<Root> loadJavadocXmlFiles(List<File> javadocXmlFiles) {
4848

4949
public Model enrichModel(Model model) {
5050
final List<BeanModel> dBeans = new ArrayList<>();
51-
final List<EnumModel> dEnums = new ArrayList<>();
5251
for (BeanModel bean : model.getBeans()) {
5352
final BeanModel dBean = enrichBean(bean);
5453
dBeans.add(dBean);
5554
}
55+
final List<EnumModel> dEnums = new ArrayList<>();
5656
for (EnumModel enumModel : model.getEnums()) {
5757
final EnumModel dEnumModel = enrichEnum(enumModel);
5858
dEnums.add(dEnumModel);
5959
}
60-
return new Model(dBeans, dEnums, model.getRestApplications());
60+
final List<RestApplicationModel> dRestApplications = new ArrayList<>();
61+
for (RestApplicationModel restApplication : model.getRestApplications()) {
62+
final RestApplicationModel dRestApplication = enrichRestApplication(restApplication);
63+
dRestApplications.add(dRestApplication);
64+
}
65+
return new Model(dBeans, dEnums, dRestApplications);
6166
}
6267

6368
private BeanModel enrichBean(BeanModel bean) {
@@ -122,8 +127,36 @@ private EnumMemberModel enrichEnumMember(EnumMemberModel enumMember, Enum dEnum)
122127
return enumMember.withComments(Utils.concat(getComments(memberComment, tags), enumMember.getComments()));
123128
}
124129

130+
private RestApplicationModel enrichRestApplication(RestApplicationModel restApplicationModel) {
131+
final List<RestMethodModel> enrichedRestMethods = new ArrayList<>();
132+
for (RestMethodModel restMethod : restApplicationModel.getMethods()) {
133+
final RestMethodModel enrichedRestMethod = enrichRestMethod(restMethod);
134+
enrichedRestMethods.add(enrichedRestMethod);
135+
}
136+
return restApplicationModel.withMethods(enrichedRestMethods);
137+
}
138+
139+
private RestMethodModel enrichRestMethod(RestMethodModel method) {
140+
final Method dMethod = findJavadocMethod(method.getOriginClass(), method.getName(), dRoots);
141+
return dMethod != null
142+
? method.withComments(getComments(dMethod.getComment(), dMethod.getTag()))
143+
: method;
144+
}
145+
125146
// finders
126147

148+
private static Method findJavadocMethod(java.lang.Class<?> cls, String name, List<Root> dRoots) {
149+
final Class dClass = findJavadocClass(cls, dRoots);
150+
final Interface dInterface = findJavadocInterface(cls, dRoots);
151+
if (dClass != null) {
152+
return findJavadocMethod(name, dClass.getMethod());
153+
} else if (dInterface != null) {
154+
return findJavadocMethod(name, dInterface.getMethod());
155+
} else {
156+
return null;
157+
}
158+
}
159+
127160
private static Class findJavadocClass(java.lang.Class<?> cls, List<Root> dRoots) {
128161
final String name = cls.getName().replace('$', '.');
129162
for (Root dRoot : dRoots) {

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
public class MethodModel {
1010

11-
private final Class<?> originClass;
12-
private final String name;
13-
private final List<MethodParameterModel> parameters;
14-
private final Type returnType;
15-
private final List<String> comments;
11+
protected final Class<?> originClass;
12+
protected final String name;
13+
protected final List<MethodParameterModel> parameters;
14+
protected final Type returnType;
15+
protected final List<String> comments;
1616

1717
public MethodModel(Class<?> originClass, String name, List<MethodParameterModel> parameters, Type returnType, List<String> comments) {
1818
this.originClass = originClass;
@@ -42,4 +42,8 @@ public List<String> getComments() {
4242
return comments;
4343
}
4444

45+
public MethodModel withComments(List<String> comments) {
46+
return new MethodModel(originClass, name, parameters, returnType, comments);
47+
}
48+
4549
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,26 @@
33

44
import java.util.ArrayList;
55
import java.util.List;
6+
import java.util.Objects;
67

78

89
public class RestApplicationModel {
910

1011
private final RestApplicationType type;
1112
private String applicationPath;
1213
private String applicationName;
13-
private final List<RestMethodModel> methods = new ArrayList<>();
14+
private final List<RestMethodModel> methods;
1415

1516
public RestApplicationModel(RestApplicationType type) {
1617
this.type = type;
18+
this.methods = new ArrayList<>();
19+
}
20+
21+
public RestApplicationModel(RestApplicationType type, String applicationPath, String applicationName, List<RestMethodModel> methods) {
22+
this.type = Objects.requireNonNull(type);
23+
this.applicationPath = applicationPath;
24+
this.applicationName = applicationName;
25+
this.methods = Objects.requireNonNull(methods);
1726
}
1827

1928
public RestApplicationType getType() {
@@ -40,4 +49,8 @@ public List<RestMethodModel> getMethods() {
4049
return methods;
4150
}
4251

52+
public RestApplicationModel withMethods(List<RestMethodModel> methods) {
53+
return new RestApplicationModel(type, applicationPath, applicationName, methods);
54+
}
55+
4356
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,9 @@ public MethodParameterModel getEntityParam() {
5050
return entityParam;
5151
}
5252

53+
@Override
54+
public RestMethodModel withComments(List<String> comments) {
55+
return new RestMethodModel(originClass, name, returnType, rootResource, httpMethod, path, pathParams, queryParams, entityParam, comments);
56+
}
57+
5358
}

typescript-generator-core/src/test/java/cz/habarta/typescript/generator/JaxrsApplicationTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,16 @@ public void testNamespacingByAnnotation() {
436436
Assert.assertTrue(errorMessage, !output.contains("class PersonResourceClient"));
437437
}
438438

439+
@Test
440+
public void testJavadoc() {
441+
final Settings settings = TestUtils.settings();
442+
settings.outputFileType = TypeScriptFileType.implementationFile;
443+
settings.generateJaxrsApplicationInterface = true;
444+
settings.javadocXmlFiles = Arrays.asList(new File("target/test-javadoc.xml"));
445+
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(OrganizationApplication.class));
446+
Assert.assertTrue(output.contains("Returns person with specified ID."));
447+
}
448+
439449
@ApplicationPath("api")
440450
public static class OrganizationApplication extends Application {
441451
@Override
@@ -480,6 +490,9 @@ public static class Organization {
480490
public static class PersonResource {
481491
@PathParam("personId")
482492
protected long personId;
493+
/**
494+
* Returns person with specified ID.
495+
*/
483496
@GET
484497
public Person getPerson() {
485498
return null;

0 commit comments

Comments
 (0)