4
4
import io .swagger .codegen .v3 .CodegenModelFactory ;
5
5
import io .swagger .codegen .v3 .CodegenModelType ;
6
6
import io .swagger .codegen .v3 .CodegenProperty ;
7
+ import io .swagger .codegen .v3 .ISchemaHandler ;
7
8
import io .swagger .codegen .v3 .generators .util .OpenAPIUtil ;
9
+ import io .swagger .v3 .oas .models .media .ArraySchema ;
8
10
import io .swagger .v3 .oas .models .media .ComposedSchema ;
9
11
import io .swagger .v3 .oas .models .media .Schema ;
10
12
import org .apache .commons .lang3 .StringUtils ;
11
13
12
14
import java .util .ArrayList ;
13
15
import java .util .List ;
16
+ import java .util .Map ;
17
+ import java .util .Optional ;
14
18
15
- public class SchemaHandler {
19
+ public class SchemaHandler implements ISchemaHandler {
20
+
21
+ public static final String ONE_OF_PREFFIX = "OneOf" ;
22
+ public static final String ANY_OF_PREFFIX = "AnyOf" ;
23
+ public static final String ARRAY_ITEMS_SUFFIX = "Items" ;
16
24
17
25
protected DefaultCodegenConfig codegenConfig ;
26
+ private List <CodegenModel > composedModels = new ArrayList <>();
18
27
19
28
public SchemaHandler (DefaultCodegenConfig codegenConfig ) {
20
29
this .codegenConfig = codegenConfig ;
21
30
}
22
31
32
+ @ Override
33
+ public void processComposedSchemas (CodegenModel codegenModel , Schema schema , Map <String , CodegenModel > allModels ) {
34
+ if (schema instanceof ComposedSchema ) {
35
+ this .processComposedSchema (codegenModel , (ComposedSchema ) schema , allModels );
36
+ return ;
37
+ }
38
+ if (schema instanceof ArraySchema ) {
39
+ this .processArrayItemSchema (codegenModel , (ArraySchema ) schema , allModels );
40
+ return ;
41
+ }
42
+ final Map <String , Schema > properties = schema .getProperties ();
43
+ if (properties == null || properties .isEmpty ()) {
44
+ return ;
45
+ }
46
+ for (String name : properties .keySet ()) {
47
+ final Schema property = properties .get (name );
48
+ final Optional <CodegenProperty > optionalCodegenProperty = codegenModel .getVars ()
49
+ .stream ()
50
+ .filter (codegenProperty -> codegenProperty .baseName .equals (name ))
51
+ .findFirst ();
52
+ if (!optionalCodegenProperty .isPresent ()) {
53
+ continue ;
54
+ }
55
+ final CodegenProperty codegenProperty = optionalCodegenProperty .get ();
56
+ final String codegenName = codegenModel .getName () + codegenConfig .toModelName (codegenProperty .getName ());
57
+ if (property instanceof ComposedSchema ) {
58
+ processComposedSchema (codegenName , codegenProperty , (ComposedSchema ) property , allModels );
59
+ continue ;
60
+ }
61
+ if (schema instanceof ArraySchema ) {
62
+ this .processArrayItemSchema (codegenName , codegenProperty , (ArraySchema ) property , allModels );
63
+ continue ;
64
+ }
65
+ }
66
+ }
67
+
68
+ @ Override
69
+ public List <CodegenModel > getModels () {
70
+ return composedModels ;
71
+ }
72
+
73
+ protected void processComposedSchema (CodegenModel codegenModel , ComposedSchema composedSchema , Map <String , CodegenModel > allModels ) {
74
+ List <Schema > schemas = composedSchema .getOneOf ();
75
+ CodegenModel composedModel = this .createComposedModel (ONE_OF_PREFFIX + codegenModel .getName (), schemas );
76
+ if (composedModel == null ) {
77
+ schemas = composedSchema .getAnyOf ();
78
+ composedModel = this .createComposedModel (ANY_OF_PREFFIX + codegenModel .getName (), schemas );
79
+ if (composedModel == null ) {
80
+ return ;
81
+ }
82
+ }
83
+ this .addInterfaceModel (codegenModel , composedModel );
84
+ this .addInterfaces (schemas , composedModel , allModels );
85
+ composedModels .add (composedModel );
86
+ }
87
+
88
+ protected void processComposedSchema (String codegenModelName , CodegenProperty codegenProperty , ComposedSchema composedSchema , Map <String , CodegenModel > allModels ) {
89
+ List <Schema > schemas = composedSchema .getOneOf ();
90
+ CodegenModel composedModel = this .createComposedModel (ONE_OF_PREFFIX + codegenModelName , schemas );
91
+ if (composedModel == null ) {
92
+ schemas = composedSchema .getAnyOf ();
93
+ composedModel = this .createComposedModel (ANY_OF_PREFFIX + codegenModelName , schemas );
94
+ if (composedModel == null ) {
95
+ return ;
96
+ }
97
+ }
98
+ this .addInterfaces (schemas , composedModel , allModels );
99
+ codegenProperty .datatype = composedModel .getName ();
100
+ codegenProperty .datatypeWithEnum = composedModel .getName ();
101
+ codegenProperty .baseType = composedModel .getName ();
102
+ codegenProperty .complexType = composedModel .getName ();
103
+
104
+ composedModels .add (composedModel );
105
+ }
106
+
107
+ protected void processArrayItemSchema (CodegenModel codegenModel , ArraySchema arraySchema , Map <String , CodegenModel > allModels ) {
108
+ final Schema itemsSchema = arraySchema .getItems ();
109
+ if (itemsSchema instanceof ComposedSchema ) {
110
+ final CodegenModel itemsModel = CodegenModelFactory .newInstance (CodegenModelType .MODEL );
111
+ itemsModel .setName (codegenModel .name + ARRAY_ITEMS_SUFFIX );
112
+ this .processComposedSchema (itemsModel , (ComposedSchema ) itemsSchema , allModels );
113
+ }
114
+ }
115
+
116
+ protected void processArrayItemSchema (String codegenModelName , CodegenProperty codegenProperty , ArraySchema arraySchema , Map <String , CodegenModel > allModels ) {
117
+ final Schema itemsSchema = arraySchema .getItems ();
118
+ if (itemsSchema instanceof ComposedSchema ) {
119
+ this .processComposedSchema (codegenModelName , codegenProperty , (ComposedSchema ) itemsSchema , allModels );
120
+ }
121
+ }
122
+
123
+ protected CodegenModel createComposedModel (String name , List <Schema > schemas ) {
124
+ if (schemas == null || schemas .isEmpty ()) {
125
+ return null ;
126
+ }
127
+ final CodegenModel composedModel = CodegenModelFactory .newInstance (CodegenModelType .MODEL );
128
+ composedModel .setIsComposedModel (true );
129
+ composedModel .setInterfaces (new ArrayList <>());
130
+ this .configureModel (composedModel , name );
131
+
132
+ return composedModel ;
133
+ }
134
+
135
+ private void addInterfaceModel (CodegenModel codegenModel , CodegenModel interfaceModel ) {
136
+ if (codegenModel == null ) {
137
+ return ;
138
+ }
139
+ if (codegenModel .getInterfaceModels () == null ) {
140
+ codegenModel .setInterfaceModels (new ArrayList <>());
141
+ }
142
+ codegenModel .getInterfaceModels ().add (interfaceModel );
143
+ }
144
+
145
+ private void addInterfaces (List <Schema > schemas , CodegenModel codegenModel , Map <String , CodegenModel > allModels ) {
146
+ for (Schema interfaceSchema : schemas ) {
147
+ final String ref = interfaceSchema .get$ref ();
148
+ if (StringUtils .isBlank (ref )) {
149
+ continue ;
150
+ }
151
+ final String schemaName = ref .substring (ref .lastIndexOf ("/" ) + 1 );
152
+ this .addInterfaceModel (allModels .get (codegenConfig .toModelName (schemaName )), codegenModel );
153
+ }
154
+ }
23
155
24
156
/**
25
157
* creates a codegen model object based on a composed schema property.
26
158
* @param composedProperty
27
159
* @param codegenProperty
28
160
*/
29
- public void createCodegenModel (ComposedSchema composedProperty , CodegenProperty codegenProperty ) {
161
+ @ Deprecated
162
+ protected void createCodegenModel (ComposedSchema composedProperty , CodegenProperty codegenProperty ) {
30
163
final List <Schema > oneOf = composedProperty .getOneOf ();
31
164
final List <Schema > anyOf = composedProperty .getAnyOf ();
32
165
@@ -45,11 +178,13 @@ public void createCodegenModel(ComposedSchema composedProperty, CodegenProperty
45
178
46
179
}
47
180
48
- public void configureComposedModelFromSchemaItems (CodegenModel codegenModel , ComposedSchema items ) {
181
+ @ Deprecated
182
+ protected void configureComposedModelFromSchemaItems (CodegenModel codegenModel , ComposedSchema items ) {
49
183
List <Schema > oneOfList = items .getOneOf ();
50
184
if (oneOfList != null && !oneOfList .isEmpty ()){
51
185
String name = "OneOf" + codegenModel .name + "Items" ;
52
- final CodegenModel oneOfModel = createComposedModel (name );
186
+ final CodegenModel oneOfModel = CodegenModelFactory .newInstance (CodegenModelType .MODEL );
187
+ this .configureModel (oneOfModel , name );
53
188
// setting name to be used as instance type on composed model.
54
189
items .addExtension ("x-model-name" , codegenConfig .toModelName (name ));
55
190
@@ -68,7 +203,8 @@ public void configureComposedModelFromSchemaItems(CodegenModel codegenModel, Com
68
203
List <Schema > anyOfList = items .getAnyOf ();
69
204
if (anyOfList != null && !anyOfList .isEmpty ()){
70
205
String name = "AnyOf" + codegenModel .name + "Items" ;
71
- final CodegenModel anyOfModel = createComposedModel (name );
206
+ final CodegenModel anyOfModel = CodegenModelFactory .newInstance (CodegenModelType .MODEL );
207
+ this .configureModel (anyOfModel , name );
72
208
items .addExtension ("x-model-name" , codegenConfig .toModelName (name ));
73
209
74
210
final List <String > modelNames = new ArrayList <>();
@@ -85,8 +221,11 @@ public void configureComposedModelFromSchemaItems(CodegenModel codegenModel, Com
85
221
}
86
222
}
87
223
224
+ @ Deprecated
88
225
public void configureOneOfModel (CodegenModel codegenModel , List <Schema > oneOf ) {
89
- final CodegenModel oneOfModel = createComposedModel ("OneOf" + codegenModel .name );
226
+ final CodegenModel oneOfModel = CodegenModelFactory .newInstance (CodegenModelType .MODEL );
227
+ this .configureModel (oneOfModel , "OneOf" + codegenModel .name );
228
+
90
229
final List <String > modelNames = new ArrayList <>();
91
230
for (Schema interfaceSchema : oneOf ) {
92
231
if (StringUtils .isNotBlank (interfaceSchema .get$ref ())) {
@@ -104,8 +243,10 @@ public void configureOneOfModel(CodegenModel codegenModel, List<Schema> oneOf) {
104
243
}
105
244
}
106
245
246
+ @ Deprecated
107
247
public void configureAnyOfModel (CodegenModel codegenModel , List <Schema > anyOf ) {
108
- final CodegenModel anyOfModel = createComposedModel ("AnyOf" + codegenModel .name );
248
+ final CodegenModel anyOfModel = CodegenModelFactory .newInstance (CodegenModelType .MODEL );
249
+ this .configureModel (anyOfModel , "AnyOf" + codegenModel .name );
109
250
final List <String > modelNames = new ArrayList <>();
110
251
for (Schema interfaceSchema : anyOf ) {
111
252
if (StringUtils .isNotBlank (interfaceSchema .get$ref ())) {
@@ -123,7 +264,8 @@ public void configureAnyOfModel(CodegenModel codegenModel, List<Schema> anyOf) {
123
264
}
124
265
}
125
266
126
- public void configureOneOfModelFromProperty (CodegenProperty codegenProperty , CodegenModel codegenModel ) {
267
+ @ Deprecated
268
+ protected void configureOneOfModelFromProperty (CodegenProperty codegenProperty , CodegenModel codegenModel ) {
127
269
String name = "OneOf" + codegenConfig .toModelName (codegenModel .name );
128
270
name += codegenConfig .toModelName (codegenProperty .name );
129
271
CodegenModel oneOfModel = (CodegenModel ) codegenProperty .vendorExtensions .get ("oneOf-model" );
@@ -138,7 +280,8 @@ public void configureOneOfModelFromProperty(CodegenProperty codegenProperty, Cod
138
280
codegenModel .vendorExtensions .put ("oneOf-model" , oneOfModel );
139
281
}
140
282
141
- public void configureAnyOfModelFromProperty (CodegenProperty codegenProperty , CodegenModel codegenModel ) {
283
+ @ Deprecated
284
+ protected void configureAnyOfModelFromProperty (CodegenProperty codegenProperty , CodegenModel codegenModel ) {
142
285
String name = "AnyOf" + codegenConfig .toModelName (codegenModel .name );
143
286
name += codegenConfig .toModelName (codegenProperty .name );
144
287
CodegenModel anyOfModel = (CodegenModel ) codegenProperty .vendorExtensions .get ("anyOf-model" );
@@ -153,7 +296,8 @@ public void configureAnyOfModelFromProperty(CodegenProperty codegenProperty, Cod
153
296
codegenModel .vendorExtensions .put ("anyOf-model" , anyOfModel );
154
297
}
155
298
156
- private CodegenModel createFromOneOfSchemas (List <Schema > schemas ) {
299
+ @ Deprecated
300
+ protected CodegenModel createFromOneOfSchemas (List <Schema > schemas ) {
157
301
final CodegenModel codegenModel = CodegenModelFactory .newInstance (CodegenModelType .MODEL );
158
302
final List <String > modelNames = new ArrayList <>();
159
303
@@ -167,21 +311,14 @@ private CodegenModel createFromOneOfSchemas(List<Schema> schemas) {
167
311
return codegenModel ;
168
312
}
169
313
170
- private CodegenModel createComposedModel (String name ) {
171
- final CodegenModel composedModel = CodegenModelFactory .newInstance (CodegenModelType .MODEL );
172
- this .configureModel (composedModel , name );
173
- return composedModel ;
174
- }
175
-
176
- private void configureModel (CodegenModel codegenModel , String name ) {
314
+ protected void configureModel (CodegenModel codegenModel , String name ) {
177
315
codegenModel .name = name ;
178
316
codegenModel .classname = codegenConfig .toModelName (name );
179
317
codegenModel .classVarName = codegenConfig .toVarName (name );
180
318
codegenModel .classFilename = codegenConfig .toModelFilename (name );
181
- codegenModel .vendorExtensions .put ("x-is-composed-model" , Boolean .TRUE );
182
319
}
183
320
184
- private boolean hasNonObjectSchema (List <Schema > schemas ) {
321
+ protected boolean hasNonObjectSchema (List <Schema > schemas ) {
185
322
for (Schema schema : schemas ) {
186
323
if (!codegenConfig .isObjectSchema (schema )) {
187
324
return true ;
0 commit comments