4
4
import com .thoughtworks .qdox .JavaProjectBuilder ;
5
5
import com .thoughtworks .qdox .model .JavaClass ;
6
6
import com .thoughtworks .qdox .model .JavaField ;
7
+ import com .thoughtworks .qdox .model .JavaType ;
8
+ import com .thoughtworks .qdox .model .impl .DefaultJavaParameterizedType ;
7
9
import com .ztianzeng .apidoc .converter .AnnotatedType ;
8
10
import com .ztianzeng .apidoc .converter .ModelConverter ;
9
11
import com .ztianzeng .apidoc .converter .ModelConverterContext ;
12
+ import com .ztianzeng .apidoc .models .media .MapSchema ;
10
13
import com .ztianzeng .apidoc .models .media .PrimitiveType ;
11
14
import com .ztianzeng .apidoc .models .media .Schema ;
12
15
import com .ztianzeng .apidoc .utils .DocUtils ;
13
16
import lombok .extern .slf4j .Slf4j ;
14
17
import org .apache .commons .lang3 .StringUtils ;
15
18
16
- import java .util .ArrayList ;
17
- import java .util .Iterator ;
18
- import java .util .List ;
19
- import java .util .Optional ;
19
+ import java .util .*;
20
20
21
21
import static com .ztianzeng .apidoc .utils .RefUtils .constructRef ;
22
22
@@ -58,7 +58,7 @@ public Schema resolve(AnnotatedType annotatedType,
58
58
parentName = targetClass .getName ();
59
59
}
60
60
61
- schema . name ( parentName );
61
+
62
62
63
63
Schema resolvedModel = context .resolve (annotatedType );
64
64
if (resolvedModel != null ) {
@@ -69,9 +69,12 @@ public Schema resolve(AnnotatedType annotatedType,
69
69
70
70
71
71
// 转换成OpenApi定义的字段信息
72
- PrimitiveType parentType = PrimitiveType .fromType (parentName );
72
+ PrimitiveType parentType = PrimitiveType .fromType (targetClass . getFullyQualifiedName () );
73
73
schema .setType (Optional .ofNullable (parentType ).orElse (PrimitiveType .OBJECT ).getCommonName ());
74
-
74
+ if (DocUtils .isPrimitive (targetClass .getName ())) {
75
+ return schema ;
76
+ }
77
+ schema .name (parentName );
75
78
76
79
// 分析类的字段
77
80
List <JavaField > fields = new ArrayList <>();
@@ -83,9 +86,37 @@ public Schema resolve(AnnotatedType annotatedType,
83
86
cls = cls .getSuperJavaClass ();
84
87
}
85
88
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
+ }
89
120
// // 如果是集合类型,将类型向上抛出继续处理
90
121
// if (targetType.isContainerType()) {
91
122
// // 处理Map那种两种都有的
@@ -138,6 +169,9 @@ public Schema resolve(AnnotatedType annotatedType,
138
169
139
170
140
171
for (JavaField field : fields ) {
172
+ if (DocUtils .isPrimitive (field .getName ())) {
173
+ continue ;
174
+ }
141
175
142
176
AnnotatedType aType = new AnnotatedType ()
143
177
.javaClass (field .getType ())
0 commit comments