Skip to content

Commit 849c2cf

Browse files
committed
重写equals方法
1 parent 672e292 commit 849c2cf

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

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

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
import com.thoughtworks.qdox.JavaProjectBuilder;
55
import com.thoughtworks.qdox.model.JavaClass;
66
import com.thoughtworks.qdox.model.JavaField;
7+
import com.thoughtworks.qdox.model.JavaType;
8+
import com.thoughtworks.qdox.model.impl.DefaultJavaParameterizedType;
79
import com.ztianzeng.apidoc.converter.AnnotatedType;
810
import com.ztianzeng.apidoc.converter.ModelConverter;
911
import com.ztianzeng.apidoc.converter.ModelConverterContext;
12+
import com.ztianzeng.apidoc.models.media.MapSchema;
1013
import com.ztianzeng.apidoc.models.media.PrimitiveType;
1114
import com.ztianzeng.apidoc.models.media.Schema;
1215
import com.ztianzeng.apidoc.utils.DocUtils;
1316
import lombok.extern.slf4j.Slf4j;
1417
import org.apache.commons.lang3.StringUtils;
1518

16-
import java.util.ArrayList;
17-
import java.util.Iterator;
18-
import java.util.List;
19-
import java.util.Optional;
19+
import java.util.*;
2020

2121
import static com.ztianzeng.apidoc.utils.RefUtils.constructRef;
2222

@@ -58,7 +58,7 @@ public Schema resolve(AnnotatedType annotatedType,
5858
parentName = targetClass.getName();
5959
}
6060

61-
schema.name(parentName);
61+
6262

6363
Schema resolvedModel = context.resolve(annotatedType);
6464
if (resolvedModel != null) {
@@ -69,9 +69,12 @@ public Schema resolve(AnnotatedType annotatedType,
6969

7070

7171
// 转换成OpenApi定义的字段信息
72-
PrimitiveType parentType = PrimitiveType.fromType(parentName);
72+
PrimitiveType parentType = PrimitiveType.fromType(targetClass.getFullyQualifiedName());
7373
schema.setType(Optional.ofNullable(parentType).orElse(PrimitiveType.OBJECT).getCommonName());
74-
74+
if (DocUtils.isPrimitive(targetClass.getName())) {
75+
return schema;
76+
}
77+
schema.name(parentName);
7578

7679
// 分析类的字段
7780
List<JavaField> fields = new ArrayList<>();
@@ -83,9 +86,37 @@ public Schema resolve(AnnotatedType annotatedType,
8386
cls = cls.getSuperJavaClass();
8487
}
8588

86-
87-
// JavaType valueType = targetType.getContentType();
88-
// JavaType keyType = targetType.getKeyType();
89+
if (targetClass.isA(Map.class.getName())) {
90+
// 泛型信息
91+
List<JavaType> actualTypeArguments = ((DefaultJavaParameterizedType) targetClass).getActualTypeArguments();
92+
JavaType javaType = actualTypeArguments.get(1);
93+
94+
Schema addPropertiesSchema = context.resolve(
95+
new AnnotatedType()
96+
.javaClass(builder.getClassByName(javaType.getFullyQualifiedName()))
97+
.schemaProperty(annotatedType.isSchemaProperty())
98+
.skipSchemaName(true)
99+
.resolveAsRef(annotatedType.isResolveAsRef())
100+
.jsonViewAnnotation(annotatedType.getJsonViewAnnotation())
101+
.propertyName(annotatedType.getPropertyName())
102+
.parent(annotatedType.getParent())
103+
);
104+
String pName = null;
105+
if (addPropertiesSchema != null) {
106+
if (StringUtils.isNotBlank(addPropertiesSchema.getName())) {
107+
pName = addPropertiesSchema.getName();
108+
}
109+
if ("object".equals(addPropertiesSchema.getType()) && pName != null) {
110+
// create a reference for the items
111+
if (context.getDefinedModels().containsKey(pName)) {
112+
addPropertiesSchema = new Schema().$ref(constructRef(pName));
113+
}
114+
} else if (addPropertiesSchema.get$ref() != null) {
115+
addPropertiesSchema = new Schema().$ref(StringUtils.isNotEmpty(addPropertiesSchema.get$ref()) ? addPropertiesSchema.get$ref() : addPropertiesSchema.getName());
116+
}
117+
}
118+
schema = new MapSchema().additionalProperties(addPropertiesSchema);
119+
}
89120
// // 如果是集合类型,将类型向上抛出继续处理
90121
// if (targetType.isContainerType()) {
91122
// // 处理Map那种两种都有的
@@ -138,6 +169,9 @@ public Schema resolve(AnnotatedType annotatedType,
138169

139170

140171
for (JavaField field : fields) {
172+
if (DocUtils.isPrimitive(field.getName())) {
173+
continue;
174+
}
141175

142176
AnnotatedType aType = new AnnotatedType()
143177
.javaClass(field.getType())

0 commit comments

Comments
 (0)