Skip to content

Commit 6855889

Browse files
committed
findName属性
1 parent 99bb8fe commit 6855889

File tree

4 files changed

+176
-11
lines changed

4 files changed

+176
-11
lines changed

apidoc-core/src/main/java/com/ztianzeng/apidoc/ModelResolver.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.util.*;
2020

21+
import static com.ztianzeng.apidoc.utils.DocUtils.genericityContentType;
22+
import static com.ztianzeng.apidoc.utils.DocUtils.genericityCount;
2123
import static com.ztianzeng.apidoc.utils.RefUtils.constructRef;
2224

2325

@@ -55,7 +57,6 @@ public Schema resolve(AnnotatedType annotatedType,
5557
String parentName = annotatedType.getName();
5658
if (StringUtils.isBlank(parentName)) {
5759
parentName = findName(targetClass, new StringBuilder());
58-
;
5960
}
6061

6162

@@ -66,9 +67,23 @@ public Schema resolve(AnnotatedType annotatedType,
6667
}
6768
}
6869

69-
if (targetClass instanceof DefaultJavaParameterizedType) {
70-
setRefType(targetClass, schema, annotatedType, targetClass, context);
70+
// 如果泛型大于0
71+
if (genericityCount(targetClass) > 0) {
72+
JavaClass aClass = genericityContentType(targetClass);
73+
if (aClass != null) {
74+
AnnotatedType aType = new AnnotatedType()
75+
.javaClass(aClass)
76+
.parent(schema)
77+
.resolveAsRef(annotatedType.isResolveAsRef())
78+
.jsonViewAnnotation(annotatedType.getJsonViewAnnotation())
79+
.skipSchemaName(true)
80+
.schemaProperty(true)
81+
.propertyName(targetClass.getName());
82+
context.resolve(aType);
83+
84+
}
7185
}
86+
7287
// 转换成OpenApi定义的字段信息
7388
PrimitiveType parentType = PrimitiveType.fromType(targetClass.getBinaryName());
7489
schema.setType(Optional.ofNullable(parentType).orElse(PrimitiveType.OBJECT).getCommonName());
@@ -154,8 +169,7 @@ public Schema resolve(AnnotatedType annotatedType,
154169
if (DocUtils.isPrimitive(field.getName())) {
155170
continue;
156171
}
157-
JavaClass type = field.getType();
158-
setRefType(type, schema, annotatedType, targetClass, context);
172+
JavaClass type = genericityContentType(targetClass) == null ? field.getType() : genericityContentType(targetClass);
159173

160174
AnnotatedType aType = new AnnotatedType()
161175
.javaClass(type)

apidoc-core/src/main/java/com/ztianzeng/apidoc/Reader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public Operation parseMethod(JavaMethod javaMethod, boolean deprecated, String t
275275

276276
if (objectSchema != null) {
277277
if (objectSchema instanceof ArraySchema) {
278-
((ArraySchema) objectSchema).getItems().$ref(constructRef(objectSchema.getName()));
278+
((ArraySchema) objectSchema).getItems().$ref(constructRef(schemaMap.keySet().stream().findFirst().orElse("")));
279279
} else {
280280
objectSchema.$ref(constructRef(objectSchema.getName()));
281281

apidoc-core/src/main/java/com/ztianzeng/apidoc/utils/DocUtils.java

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
import com.fasterxml.jackson.databind.PropertyName;
77
import com.fasterxml.jackson.databind.type.TypeFactory;
88
import com.thoughtworks.qdox.model.JavaAnnotation;
9+
import com.thoughtworks.qdox.model.JavaClass;
910
import com.thoughtworks.qdox.model.JavaField;
11+
import com.thoughtworks.qdox.model.impl.DefaultJavaParameterizedType;
1012
import com.ztianzeng.apidoc.constants.RequestMethod;
13+
import org.apache.commons.lang3.StringUtils;
1114

1215
import java.lang.reflect.Type;
16+
import java.util.ArrayList;
1317
import java.util.List;
1418
import java.util.Set;
1519

@@ -233,4 +237,128 @@ public static String findTypeName(JavaType type, BeanDescription beanDesc) {
233237
return type.getRawClass().getSimpleName() + stringBuilder.toString();
234238
}
235239

240+
241+
/**
242+
* 获取泛型的数量
243+
*
244+
* @param returnType
245+
* @return
246+
*/
247+
public static int genericityCount(JavaClass returnType) {
248+
if (returnType instanceof DefaultJavaParameterizedType) {
249+
return ((DefaultJavaParameterizedType) returnType).getActualTypeArguments().size();
250+
}
251+
return 0;
252+
}
253+
254+
/**
255+
* 获取泛型的数量
256+
*
257+
* @param returnType
258+
* @return
259+
*/
260+
public static JavaClass genericityContentType(JavaClass returnType) {
261+
if (returnType instanceof DefaultJavaParameterizedType) {
262+
if (!((DefaultJavaParameterizedType) returnType).getActualTypeArguments().isEmpty()) {
263+
return (DefaultJavaParameterizedType) ((DefaultJavaParameterizedType) returnType).getActualTypeArguments().get(0);
264+
}
265+
}
266+
return null;
267+
}
268+
269+
/**
270+
* 是否为map
271+
*
272+
* @param type java type
273+
* @return boolean
274+
*/
275+
public static boolean isMap(String type) {
276+
switch (type) {
277+
case "java.util.Map":
278+
case "java.util.SortedMap":
279+
case "java.util.TreeMap":
280+
case "java.util.LinkedHashMap":
281+
case "java.util.HashMap":
282+
case "java.util.concurrent.ConcurrentHashMap":
283+
case "java.util.Properties":
284+
case "java.util.Hashtable":
285+
return true;
286+
default:
287+
return false;
288+
}
289+
}
290+
291+
/**
292+
* Automatic repair of generic split class names
293+
*
294+
* @param arr arr of class name
295+
* @return array of String
296+
*/
297+
private static String[] classNameFix(String[] arr) {
298+
List<String> classes = new ArrayList<>();
299+
List<Integer> indexList = new ArrayList<>();
300+
int globIndex = 0;
301+
for (int i = 0; i < arr.length; i++) {
302+
if (!classes.isEmpty()) {
303+
int index = classes.size() - 1;
304+
if (!isClassName(classes.get(index))) {
305+
globIndex = globIndex + 1;
306+
if (globIndex < arr.length) {
307+
indexList.add(globIndex);
308+
String className = classes.get(index) + "," + arr[globIndex];
309+
classes.set(index, className);
310+
}
311+
312+
} else {
313+
globIndex = globIndex + 1;
314+
if (globIndex < arr.length) {
315+
if (isClassName(arr[globIndex])) {
316+
indexList.add(globIndex);
317+
classes.add(arr[globIndex]);
318+
} else {
319+
if (!indexList.contains(globIndex) && !indexList.contains(globIndex + 1)) {
320+
indexList.add(globIndex);
321+
classes.add(arr[globIndex] + "," + arr[globIndex + 1]);
322+
globIndex = globIndex + 1;
323+
indexList.add(globIndex);
324+
}
325+
}
326+
}
327+
}
328+
} else {
329+
if (isClassName(arr[i])) {
330+
indexList.add(i);
331+
classes.add(arr[i]);
332+
} else {
333+
if (!indexList.contains(i) && !indexList.contains(i + 1)) {
334+
globIndex = i + 1;
335+
classes.add(arr[i] + "," + arr[globIndex]);
336+
indexList.add(i);
337+
indexList.add(i + 1);
338+
}
339+
}
340+
}
341+
}
342+
return classes.toArray(new String[classes.size()]);
343+
}
344+
345+
/**
346+
* 是否是合法的java类名称
347+
*
348+
* @param className class nem
349+
* @return boolean
350+
*/
351+
public static boolean isClassName(String className) {
352+
if (StringUtils.isEmpty(className)) {
353+
return false;
354+
}
355+
if (className.contains("<") && !className.contains(">")) {
356+
return false;
357+
} else if (className.contains(">") && !className.contains("<")) {
358+
return false;
359+
} else {
360+
return true;
361+
}
362+
}
363+
236364
}

apidoc-core/src/test/java/com/ztianzeng/apidoc/test/ReaderTest.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public void testMuRet() {
183183
" content:\n" +
184184
" application/json:\n" +
185185
" schema:\n" +
186-
" $ref: '#/components/schemas/Result'\n" +
186+
" $ref: '#/components/schemas/ResultResult2CreateParam'\n" +
187187
" deprecated: false\n" +
188188
"components:\n" +
189189
" schemas:\n" +
@@ -198,20 +198,43 @@ public void testMuRet() {
198198
" mobile:\n" +
199199
" type: string\n" +
200200
" description: 手机\n" +
201-
" Result:\n" +
201+
" Result2CreateParam:\n" +
202202
" type: object\n" +
203203
" properties:\n" +
204204
" msg:\n" +
205205
" type: string\n" +
206206
" data:\n" +
207-
" $ref: '#/components/schemas/Result2'\n" +
208-
" Result2:\n" +
207+
" required:\n" +
208+
" - username\n" +
209+
" type: object\n" +
210+
" properties:\n" +
211+
" username:\n" +
212+
" type: string\n" +
213+
" description: 用户名\n" +
214+
" mobile:\n" +
215+
" type: string\n" +
216+
" description: 手机\n" +
217+
" ResultResult2CreateParam:\n" +
209218
" type: object\n" +
210219
" properties:\n" +
211220
" msg:\n" +
212221
" type: string\n" +
213222
" data:\n" +
214-
" $ref: '#/components/schemas/CreateParam'\n";
223+
" type: object\n" +
224+
" properties:\n" +
225+
" msg:\n" +
226+
" type: string\n" +
227+
" data:\n" +
228+
" required:\n" +
229+
" - username\n" +
230+
" type: object\n" +
231+
" properties:\n" +
232+
" username:\n" +
233+
" type: string\n" +
234+
" description: 用户名\n" +
235+
" mobile:\n" +
236+
" type: string\n" +
237+
" description: 手机\n";
215238

216239
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
217240
}

0 commit comments

Comments
 (0)