27
27
import java .lang .reflect .Modifier ;
28
28
import java .util .ArrayList ;
29
29
import java .util .Collection ;
30
+ import java .util .Collections ;
30
31
import java .util .Iterator ;
31
32
import java .util .List ;
32
- import java .util .stream .Collectors ;
33
33
34
34
import com .fasterxml .jackson .databind .JavaType ;
35
35
import io .swagger .v3 .core .converter .AnnotatedType ;
43
43
44
44
/**
45
45
* The type Polymorphic model converter.
46
+ *
46
47
* @author bnasslahsen
47
48
*/
48
49
public class PolymorphicModelConverter implements ModelConverter {
@@ -52,6 +53,17 @@ public class PolymorphicModelConverter implements ModelConverter {
52
53
*/
53
54
private final ObjectMapperProvider springDocObjectMapper ;
54
55
56
+ /**
57
+ * The constant PARENT_TYPES_TO_IGNORE.
58
+ */
59
+ private static final List <String > PARENT_TYPES_TO_IGNORE = Collections .synchronizedList (new ArrayList <>());
60
+
61
+ static {
62
+ PARENT_TYPES_TO_IGNORE .add ("JsonSchema" );
63
+ PARENT_TYPES_TO_IGNORE .add ("Pageable" );
64
+ PARENT_TYPES_TO_IGNORE .add ("EntityModel" );
65
+ }
66
+
55
67
/**
56
68
* Instantiates a new Polymorphic model converter.
57
69
*
@@ -61,12 +73,21 @@ public PolymorphicModelConverter(ObjectMapperProvider springDocObjectMapper) {
61
73
this .springDocObjectMapper = springDocObjectMapper ;
62
74
}
63
75
64
- private static Schema <?> getResolvedSchema (JavaType javaType , Schema <?> resolvedSchema ) {
76
+ /**
77
+ * Add parent type.
78
+ *
79
+ * @param parentTypes the parent types
80
+ */
81
+ public static void addParentType (String ... parentTypes ) {
82
+ PARENT_TYPES_TO_IGNORE .addAll (List .of (parentTypes ));
83
+ }
84
+
85
+ private Schema <?> getResolvedSchema (JavaType javaType , Schema <?> resolvedSchema ) {
65
86
if (resolvedSchema instanceof ObjectSchema && resolvedSchema .getProperties () != null ) {
66
- if (resolvedSchema .getProperties ().containsKey (javaType .getRawClass ().getName ())){
87
+ if (resolvedSchema .getProperties ().containsKey (javaType .getRawClass ().getName ())) {
67
88
resolvedSchema = resolvedSchema .getProperties ().get (javaType .getRawClass ().getName ());
68
89
}
69
- else if (resolvedSchema .getProperties ().containsKey (javaType .getRawClass ().getSimpleName ())){
90
+ else if (resolvedSchema .getProperties ().containsKey (javaType .getRawClass ().getSimpleName ())) {
70
91
resolvedSchema = resolvedSchema .getProperties ().get (javaType .getRawClass ().getSimpleName ());
71
92
}
72
93
}
@@ -78,6 +99,9 @@ public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterato
78
99
JavaType javaType = springDocObjectMapper .jsonMapper ().constructType (type .getType ());
79
100
if (javaType != null ) {
80
101
if (chain .hasNext ()) {
102
+ if (!type .isResolveAsRef () && type .getParent () != null
103
+ && PARENT_TYPES_TO_IGNORE .stream ().noneMatch (ignore -> type .getParent ().getName ().startsWith (ignore )))
104
+ type .resolveAsRef (true );
81
105
Schema <?> resolvedSchema = chain .next ().resolve (type , context , chain );
82
106
resolvedSchema = getResolvedSchema (javaType , resolvedSchema );
83
107
if (resolvedSchema == null || resolvedSchema .get$ref () == null )
@@ -91,8 +115,8 @@ public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterato
91
115
/**
92
116
* Compose polymorphic schema.
93
117
*
94
- * @param type the type
95
- * @param schema the schema
118
+ * @param type the type
119
+ * @param schema the schema
96
120
* @param schemas the schemas
97
121
* @return the schema
98
122
*/
@@ -111,7 +135,7 @@ private Schema composePolymorphicSchema(AnnotatedType type, Schema schema, Colle
111
135
/**
112
136
* Find composed schemas recursively.
113
137
*
114
- * @param ref the reference of the schema
138
+ * @param ref the reference of the schema
115
139
* @param schemas the collection of schemas to search in
116
140
* @return the list of composed schemas
117
141
*/
@@ -133,6 +157,7 @@ private List<Schema> findComposedSchemas(String ref, Collection<Schema> schemas)
133
157
134
158
return resultSchemas ;
135
159
}
160
+
136
161
/**
137
162
* Is concrete class boolean.
138
163
*
0 commit comments