Skip to content

Commit 6046fa8

Browse files
committed
Improve javadoc support.
1 parent 459c190 commit 6046fa8

File tree

23 files changed

+683
-530
lines changed

23 files changed

+683
-530
lines changed

springdoc-openapi-javadoc/src/main/java/org/springdoc/openapi/javadoc/SpringDocJavadocProvider.java

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222

2323
import java.lang.reflect.Field;
2424
import java.lang.reflect.Method;
25+
import java.util.ArrayList;
2526
import java.util.List;
27+
import java.util.stream.Collectors;
2628

29+
import com.github.therapi.runtimejavadoc.ClassJavadoc;
2730
import com.github.therapi.runtimejavadoc.CommentFormatter;
2831
import com.github.therapi.runtimejavadoc.FieldJavadoc;
2932
import com.github.therapi.runtimejavadoc.MethodJavadoc;
@@ -50,7 +53,7 @@ public class SpringDocJavadocProvider implements JavadocProvider {
5053
*/
5154
@Override
5255
public String getMethodJavadocDescription(Method method) {
53-
MethodJavadoc methodJavadoc = RuntimeJavadoc.getJavadoc(method);
56+
MethodJavadoc methodJavadoc = findMethodJavadoc(method);
5457
return formatter.format(methodJavadoc.getComment());
5558
}
5659

@@ -62,7 +65,7 @@ public String getMethodJavadocDescription(Method method) {
6265
*/
6366
@Override
6467
public String getMethodJavadocReturn(Method method) {
65-
MethodJavadoc methodJavadoc = RuntimeJavadoc.getJavadoc(method);
68+
MethodJavadoc methodJavadoc = findMethodJavadoc(method);
6669
return formatter.format(methodJavadoc.getReturns());
6770
}
6871

@@ -75,7 +78,7 @@ public String getMethodJavadocReturn(Method method) {
7578
*/
7679
@Override
7780
public String getParamJavadoc(Method method, String name) {
78-
MethodJavadoc methodJavadoc = RuntimeJavadoc.getJavadoc(method);
81+
MethodJavadoc methodJavadoc = findMethodJavadoc(method);
7982
List<ParamJavadoc> paramsDoc = methodJavadoc.getParams();
8083
return paramsDoc.stream().filter(paramJavadoc1 -> name.equals(paramJavadoc1.getName())).findAny()
8184
.map(paramJavadoc1 -> formatter.format(paramJavadoc1.getComment())).orElse(null);
@@ -93,4 +96,61 @@ public String getFieldJavadoc(Field field) {
9396
return formatter.format(fieldJavadoc.getComment());
9497
}
9598

99+
/**
100+
* Find method javadoc method javadoc.
101+
*
102+
* @param method the method
103+
* @return the method javadoc
104+
*/
105+
private MethodJavadoc findMethodJavadoc(Method method) {
106+
ClassJavadoc classJavadoc = RuntimeJavadoc.getJavadoc(method.getDeclaringClass());
107+
List<MethodJavadoc> methodDocs = classJavadoc.getMethods();
108+
// filter by method name
109+
List<MethodJavadoc> methodDocByMethodName = methodDocs.stream().filter(methodJavadoc -> methodJavadoc.getName().equals(method.getName())).collect(Collectors.toList());
110+
if (methodDocByMethodName.size() == 1)
111+
return methodDocByMethodName.get(0);
112+
// filter by parameters
113+
if (methodDocByMethodName.size() > 1) {
114+
List<MethodJavadoc> methodDocByParamType = methodDocByMethodName.stream().filter(methodJavadoc -> paramsMatch(method.getParameterTypes(), methodJavadoc.getParamTypes())).collect(Collectors.toList());
115+
if (methodDocByParamType.size() == 1)
116+
return methodDocByParamType.get(0);
117+
}
118+
return MethodJavadoc.createEmpty(method);
119+
}
120+
121+
/**
122+
* Params match boolean.
123+
*
124+
* @param paramTypesClass the param types class
125+
* @param paramTypes the param types
126+
* @return the boolean
127+
*/
128+
private boolean paramsMatch(Class<?>[] paramTypesClass, List<String> paramTypes) {
129+
List<String> paramTypesJavadoc = new ArrayList<>();
130+
for (int i = 0; i < paramTypes.size(); i++) {
131+
if (paramTypes.get(i).contains("::")) {
132+
String[] paramTypeArray = paramTypes.get(i).split("::");
133+
String paramType = paramTypeArray[paramTypeArray.length - 1].trim().replace(")", "");
134+
paramTypesJavadoc.add(paramType);
135+
}
136+
else
137+
paramTypesJavadoc.add(paramTypes.get(i));
138+
}
139+
return getCanonicalNames(paramTypesClass).equals(paramTypesJavadoc);
140+
}
141+
142+
/**
143+
* Gets canonical names.
144+
*
145+
* @param paramTypes the param types
146+
* @return the canonical names
147+
*/
148+
private List<String> getCanonicalNames(Class<?>[] paramTypes) {
149+
List<String> methodParamsTypes = new ArrayList<>();
150+
for (Class<?> aClass : paramTypes) {
151+
methodParamsTypes.add(aClass.getCanonicalName());
152+
}
153+
return methodParamsTypes;
154+
}
155+
96156
}

springdoc-openapi-javadoc/src/test/resources/results/app1.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,13 @@
198198
"tags": [
199199
"items"
200200
],
201+
"description": "Show items list.",
201202
"operationId": "showItems",
202203
"parameters": [
203204
{
204205
"name": "cusID",
205206
"in": "query",
207+
"description": "the customer id",
206208
"required": true,
207209
"schema": {
208210
"maxLength": 6,
@@ -213,6 +215,7 @@
213215
{
214216
"name": "toto",
215217
"in": "query",
218+
"description": "the toto",
216219
"required": true,
217220
"schema": {
218221
"maximum": 6,
@@ -224,6 +227,7 @@
224227
{
225228
"name": "start",
226229
"in": "query",
230+
"description": "the start date",
227231
"required": false,
228232
"schema": {
229233
"type": "string",
@@ -234,6 +238,7 @@
234238
{
235239
"name": "filterIds",
236240
"in": "query",
241+
"description": "the filter ids",
237242
"required": false,
238243
"explode": false,
239244
"schema": {
@@ -256,7 +261,7 @@
256261
}
257262
},
258263
"200": {
259-
"description": "OK",
264+
"description": "the list",
260265
"content": {
261266
"application/json": {
262267
"schema": {
@@ -274,8 +279,10 @@
274279
"tags": [
275280
"items"
276281
],
282+
"description": "Add item response entity.",
277283
"operationId": "addItem",
278284
"requestBody": {
285+
"description": "the item dto",
279286
"content": {
280287
"application/json": {
281288
"schema": {
@@ -297,7 +304,7 @@
297304
}
298305
},
299306
"200": {
300-
"description": "OK",
307+
"description": "the response entity",
301308
"content": {
302309
"application/json": {
303310
"schema": {

springdoc-openapi-javadoc/src/test/resources/results/app100.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
"hello-ap1",
1818
"hello-ap2"
1919
],
20+
"description": "Gets all pets.",
2021
"operationId": "getAllPets",
2122
"parameters": [
2223
{
2324
"name": "toto",
2425
"in": "query",
26+
"description": "the toto",
2527
"required": true,
2628
"schema": {
2729
"type": "string"
@@ -30,7 +32,7 @@
3032
],
3133
"responses": {
3234
"200": {
33-
"description": "OK",
35+
"description": "the all pets",
3436
"content": {
3537
"application/xml": {
3638
"schema": {

0 commit comments

Comments
 (0)