Skip to content

Commit ca8f49e

Browse files
committed
List集合
1 parent c5dba4b commit ca8f49e

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.ztianzeng.apidoc.converter.AnnotatedType;
1010
import com.ztianzeng.apidoc.converter.ModelConverter;
1111
import com.ztianzeng.apidoc.converter.ModelConverterContext;
12+
import com.ztianzeng.apidoc.models.media.ArraySchema;
1213
import com.ztianzeng.apidoc.models.media.MapSchema;
1314
import com.ztianzeng.apidoc.models.media.PrimitiveType;
1415
import com.ztianzeng.apidoc.models.media.Schema;
@@ -59,7 +60,6 @@ public Schema resolve(AnnotatedType annotatedType,
5960
}
6061

6162

62-
6363
Schema resolvedModel = context.resolve(annotatedType);
6464
if (resolvedModel != null) {
6565
if (parentName.equals(resolvedModel.getName())) {
@@ -69,9 +69,14 @@ public Schema resolve(AnnotatedType annotatedType,
6969

7070

7171
// 转换成OpenApi定义的字段信息
72-
PrimitiveType parentType = PrimitiveType.fromType(targetClass.getFullyQualifiedName());
72+
PrimitiveType parentType = PrimitiveType.fromType(targetClass.getBinaryName());
7373
schema.setType(Optional.ofNullable(parentType).orElse(PrimitiveType.OBJECT).getCommonName());
7474
if (DocUtils.isPrimitive(targetClass.getName())) {
75+
if (targetClass.isArray()) {
76+
Schema array = new Schema();
77+
array.setType(schema.getType());
78+
schema = new ArraySchema().items(array);
79+
}
7580
return schema;
7681
}
7782
schema.name(parentName);
@@ -89,6 +94,9 @@ public Schema resolve(AnnotatedType annotatedType,
8994
if (targetClass.isA(Map.class.getName())) {
9095
// 泛型信息
9196
List<JavaType> actualTypeArguments = ((DefaultJavaParameterizedType) targetClass).getActualTypeArguments();
97+
if (actualTypeArguments.isEmpty()) {
98+
return null;
99+
}
92100
JavaType javaType = actualTypeArguments.get(1);
93101

94102
Schema addPropertiesSchema = context.resolve(
@@ -116,6 +124,28 @@ public Schema resolve(AnnotatedType annotatedType,
116124
}
117125
}
118126
schema = new MapSchema().additionalProperties(addPropertiesSchema);
127+
} else if (targetClass.isA(Collection.class.getName())
128+
|| targetClass.isA(List.class.getName())) {
129+
// 泛型信息
130+
List<JavaType> actualTypeArguments = ((DefaultJavaParameterizedType) targetClass).getActualTypeArguments();
131+
if (actualTypeArguments.isEmpty()) {
132+
return null;
133+
}
134+
JavaType javaType = actualTypeArguments.get(0);
135+
// 处理集合
136+
Schema items = context.resolve(new AnnotatedType()
137+
.javaClass(builder.getClassByName(javaType.getFullyQualifiedName()))
138+
.schemaProperty(annotatedType.isSchemaProperty())
139+
.skipSchemaName(true)
140+
.resolveAsRef(annotatedType.isResolveAsRef())
141+
.propertyName(annotatedType.getPropertyName())
142+
.jsonViewAnnotation(annotatedType.getJsonViewAnnotation())
143+
.parent(annotatedType.getParent()));
144+
145+
if (items == null) {
146+
return null;
147+
}
148+
schema = new ArraySchema().items(items);
119149
}
120150
// // 如果是集合类型,将类型向上抛出继续处理
121151
// if (targetType.isContainerType()) {

apidoc-core/src/main/java/com/ztianzeng/apidoc/models/media/PrimitiveType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ public Schema createProperty() {
219219
addKeys(keyClasses, NUMBER, Number.class.getName());
220220
addKeys(keyClasses, DATE, DateStub.class.getName());
221221
addKeys(keyClasses, DATE_TIME, java.util.Date.class.getName());
222+
addKeys(keyClasses, DATE_TIME, Calendar.class.getName());
222223
addKeys(keyClasses, FILE, java.io.File.class.getName());
223224
addKeys(keyClasses, OBJECT, Object.class.getName());
224225
KEY_CLASSES = Collections.unmodifiableMap(keyClasses);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void testArray() throws Exception {
4444
public void testMap() throws Exception {
4545
final ModelResolver modelResolver = new ModelResolver(mapper(), sourceBuilder);
4646
final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
47-
JavaClass classByName = TestBase.builder.getClassByName(ArrayBean.class.getName());
47+
JavaClass classByName = TestBase.builder.getClassByName(MapBean.class.getName());
4848

4949
final Schema model = context
5050
.resolve(new AnnotatedType(classByName));

0 commit comments

Comments
 (0)