1
1
package com .ztianzeng .apidoc ;
2
2
3
- import com .fasterxml .jackson .databind .ObjectMapper ;
4
3
import com .thoughtworks .qdox .JavaProjectBuilder ;
5
4
import com .thoughtworks .qdox .model .JavaClass ;
6
5
import com .thoughtworks .qdox .model .JavaField ;
31
30
*/
32
31
@ Slf4j
33
32
public class ModelResolver implements ModelConverter {
34
- private SourceBuilder sourceBuilder ;
35
33
private JavaProjectBuilder builder ;
36
34
37
35
38
- public ModelResolver (ObjectMapper mapper , SourceBuilder sourceBuilder ) {
39
- this .sourceBuilder = sourceBuilder ;
36
+ public ModelResolver (SourceBuilder sourceBuilder ) {
40
37
builder = sourceBuilder .getBuilder ();
41
38
}
42
39
@@ -48,6 +45,7 @@ public Schema resolve(AnnotatedType annotatedType,
48
45
return null ;
49
46
}
50
47
48
+
51
49
// 分析目标类信息
52
50
JavaClass targetClass = annotatedType .getJavaClass ();
53
51
@@ -67,7 +65,9 @@ public Schema resolve(AnnotatedType annotatedType,
67
65
}
68
66
}
69
67
70
-
68
+ if (targetClass instanceof DefaultJavaParameterizedType ) {
69
+ setRefType (targetClass , schema , annotatedType , targetClass , context );
70
+ }
71
71
// 转换成OpenApi定义的字段信息
72
72
PrimitiveType parentType = PrimitiveType .fromType (targetClass .getBinaryName ());
73
73
schema .setType (Optional .ofNullable (parentType ).orElse (PrimitiveType .OBJECT ).getCommonName ());
@@ -93,11 +93,11 @@ public Schema resolve(AnnotatedType annotatedType,
93
93
94
94
if (targetClass .isA (Map .class .getName ())) {
95
95
// 泛型信息
96
- List <JavaType > actualTypeArguments = ((DefaultJavaParameterizedType ) targetClass ).getActualTypeArguments ();
97
- if (actualTypeArguments .isEmpty ()) {
96
+ List <JavaType > tar = ((DefaultJavaParameterizedType ) targetClass ).getActualTypeArguments ();
97
+ if (tar .isEmpty ()) {
98
98
return null ;
99
99
}
100
- JavaType javaType = actualTypeArguments .get (1 );
100
+ JavaType javaType = tar .get (1 );
101
101
102
102
Schema addPropertiesSchema = context .resolve (
103
103
new AnnotatedType ()
@@ -127,11 +127,11 @@ public Schema resolve(AnnotatedType annotatedType,
127
127
} else if (targetClass .isA (Collection .class .getName ())
128
128
|| targetClass .isA (List .class .getName ())) {
129
129
// 泛型信息
130
- List <JavaType > actualTypeArguments = ((DefaultJavaParameterizedType ) targetClass ).getActualTypeArguments ();
131
- if (actualTypeArguments .isEmpty ()) {
130
+ List <JavaType > tar = ((DefaultJavaParameterizedType ) targetClass ).getActualTypeArguments ();
131
+ if (tar .isEmpty ()) {
132
132
return null ;
133
133
}
134
- JavaType javaType = actualTypeArguments .get (0 );
134
+ JavaType javaType = tar .get (0 );
135
135
// 处理集合
136
136
Schema items = context .resolve (new AnnotatedType ()
137
137
.javaClass (builder .getClassByName (javaType .getFullyQualifiedName ()))
@@ -147,64 +147,17 @@ public Schema resolve(AnnotatedType annotatedType,
147
147
}
148
148
schema = new ArraySchema ().items (items );
149
149
}
150
- // // 如果是集合类型,将类型向上抛出继续处理
151
- // if (targetType.isContainerType()) {
152
- // // 处理Map那种两种都有的
153
- // if (keyType != null && valueType != null) {
154
- // Schema addPropertiesSchema = context.resolve(
155
- // new AnnotatedType()
156
- // .type(valueType)
157
- // .schemaProperty(annotatedType.isSchemaProperty())
158
- // .skipSchemaName(true)
159
- // .resolveAsRef(annotatedType.isResolveAsRef())
160
- // .jsonViewAnnotation(annotatedType.getJsonViewAnnotation())
161
- // .propertyName(annotatedType.getPropertyName())
162
- // .parent(annotatedType.getParent())
163
- // );
164
- //
165
- // String pName = null;
166
- //
167
- // if (addPropertiesSchema != null) {
168
- // if (StringUtils.isNotBlank(addPropertiesSchema.getName())) {
169
- // pName = addPropertiesSchema.getName();
170
- // }
171
- // if ("object".equals(addPropertiesSchema.getType()) && pName != null) {
172
- // // create a reference for the items
173
- // if (context.getDefinedModels().containsKey(pName)) {
174
- // addPropertiesSchema = new Schema().$ref(constructRef(pName));
175
- // }
176
- // } else if (addPropertiesSchema.get$ref() != null) {
177
- // addPropertiesSchema = new Schema().$ref(StringUtils.isNotEmpty(addPropertiesSchema.get$ref()) ? addPropertiesSchema.get$ref() : addPropertiesSchema.getName());
178
- // }
179
- // }
180
- // schema = new MapSchema().additionalProperties(addPropertiesSchema);
181
- // } else if (valueType != null) {
182
- // // 处理Array
183
- // Schema items = context.resolve(new AnnotatedType()
184
- // .type(valueType)
185
- // .schemaProperty(annotatedType.isSchemaProperty())
186
- // .skipSchemaName(true)
187
- // .resolveAsRef(annotatedType.isResolveAsRef())
188
- // .propertyName(annotatedType.getPropertyName())
189
- // .jsonViewAnnotation(annotatedType.getJsonViewAnnotation())
190
- // .parent(annotatedType.getParent()));
191
- //
192
- // if (items == null) {
193
- // return null;
194
- // }
195
- // schema = new ArraySchema().items(items);
196
- //
197
- // }
198
- // }
199
150
200
151
201
152
for (JavaField field : fields ) {
202
153
if (DocUtils .isPrimitive (field .getName ())) {
203
154
continue ;
204
155
}
156
+ JavaClass type = field .getType ();
157
+ setRefType (type , schema , annotatedType , targetClass , context );
205
158
206
159
AnnotatedType aType = new AnnotatedType ()
207
- .javaClass (field . getType () )
160
+ .javaClass (type )
208
161
.parent (schema )
209
162
.resolveAsRef (annotatedType .isResolveAsRef ())
210
163
.jsonViewAnnotation (annotatedType .getJsonViewAnnotation ())
@@ -250,5 +203,31 @@ public Schema resolve(AnnotatedType annotatedType,
250
203
return schema ;
251
204
}
252
205
206
+ /**
207
+ * 设置泛型
208
+ *
209
+ * @param type
210
+ * @param schema
211
+ * @param annotatedType
212
+ * @param targetClass
213
+ * @param context
214
+ */
215
+ public void setRefType (JavaClass type , Schema schema , AnnotatedType annotatedType , JavaClass targetClass , ModelConverterContext context ) {
216
+ List <JavaType > ref = ((DefaultJavaParameterizedType ) type ).getActualTypeArguments ();
217
+ if (!ref .isEmpty ()) {
218
+ for (JavaType actualTypeArgument : ref ) {
219
+ type = builder .getClassByName (actualTypeArgument .getBinaryName ());
220
+ AnnotatedType aType = new AnnotatedType ()
221
+ .javaClass (type )
222
+ .parent (schema )
223
+ .resolveAsRef (annotatedType .isResolveAsRef ())
224
+ .jsonViewAnnotation (annotatedType .getJsonViewAnnotation ())
225
+ .skipSchemaName (true )
226
+ .schemaProperty (true )
227
+ .propertyName (targetClass .getName ());
228
+ schema = context .resolve (aType );
229
+ }
230
+ }
231
+ }
253
232
254
233
}
0 commit comments