5
5
import io .swagger .codegen .v3 .CodegenModelType ;
6
6
import io .swagger .codegen .v3 .CodegenProperty ;
7
7
import io .swagger .codegen .v3 .ISchemaHandler ;
8
- import io .swagger .codegen .v3 .generators .util .OpenAPIUtil ;
9
8
import io .swagger .v3 .oas .models .media .ArraySchema ;
10
9
import io .swagger .v3 .oas .models .media .ComposedSchema ;
11
10
import io .swagger .v3 .oas .models .media .Schema ;
@@ -32,11 +31,11 @@ public SchemaHandler(DefaultCodegenConfig codegenConfig) {
32
31
@ Override
33
32
public void processComposedSchemas (CodegenModel codegenModel , Schema schema , Map <String , CodegenModel > allModels ) {
34
33
if (schema instanceof ComposedSchema ) {
35
- this .processComposedSchema (codegenModel , (ComposedSchema ) schema , allModels );
34
+ this .addComposedModel ( this . processComposedSchema (codegenModel , (ComposedSchema ) schema , allModels ) );
36
35
return ;
37
36
}
38
37
if (schema instanceof ArraySchema ) {
39
- this .processArrayItemSchema (codegenModel , (ArraySchema ) schema , allModels );
38
+ this .addComposedModel ( this . processArrayItemSchema (codegenModel , (ArraySchema ) schema , allModels ) );
40
39
return ;
41
40
}
42
41
final Map <String , Schema > properties = schema .getProperties ();
@@ -55,11 +54,11 @@ public void processComposedSchemas(CodegenModel codegenModel, Schema schema, Map
55
54
final CodegenProperty codegenProperty = optionalCodegenProperty .get ();
56
55
final String codegenName = codegenModel .getName () + codegenConfig .toModelName (codegenProperty .getName ());
57
56
if (property instanceof ComposedSchema ) {
58
- processComposedSchema (codegenName , codegenProperty , (ComposedSchema ) property , allModels );
57
+ this . addComposedModel ( this . processComposedSchema (codegenName , codegenProperty , (ComposedSchema ) property , allModels ) );
59
58
continue ;
60
59
}
61
60
if (property instanceof ArraySchema ) {
62
- this .processArrayItemSchema (codegenName , codegenProperty , (ArraySchema ) property , allModels );
61
+ this .addComposedModel ( this . processArrayItemSchema (codegenName , codegenProperty , (ArraySchema ) property , allModels ) );
63
62
continue ;
64
63
}
65
64
}
@@ -70,54 +69,74 @@ public List<CodegenModel> getModels() {
70
69
return composedModels ;
71
70
}
72
71
73
- protected void processComposedSchema (CodegenModel codegenModel , ComposedSchema composedSchema , Map <String , CodegenModel > allModels ) {
72
+ protected CodegenModel processComposedSchema (CodegenModel codegenModel , ComposedSchema composedSchema , Map <String , CodegenModel > allModels ) {
74
73
List <Schema > schemas = composedSchema .getOneOf ();
75
74
CodegenModel composedModel = this .createComposedModel (ONE_OF_PREFFIX + codegenModel .getName (), schemas );
76
75
if (composedModel == null ) {
77
76
schemas = composedSchema .getAnyOf ();
78
77
composedModel = this .createComposedModel (ANY_OF_PREFFIX + codegenModel .getName (), schemas );
79
78
if (composedModel == null ) {
80
- return ;
79
+ return null ;
81
80
}
82
81
}
83
82
this .addInterfaceModel (codegenModel , composedModel );
84
83
this .addInterfaces (schemas , composedModel , allModels );
85
- composedModels .add (composedModel );
84
+ return composedModel ;
85
+ }
86
+
87
+ protected CodegenModel processComposedSchema (String name , ComposedSchema composedSchema , Map <String , CodegenModel > allModels ) {
88
+ List <Schema > schemas = composedSchema .getOneOf ();
89
+ CodegenModel composedModel = this .createComposedModel (ONE_OF_PREFFIX + name , schemas );
90
+ if (composedModel == null ) {
91
+ schemas = composedSchema .getAnyOf ();
92
+ composedModel = this .createComposedModel (ANY_OF_PREFFIX + name , schemas );
93
+ if (composedModel == null ) {
94
+ return null ;
95
+ }
96
+ }
97
+ this .addInterfaces (schemas , composedModel , allModels );
98
+ return composedModel ;
86
99
}
87
100
88
- protected void processComposedSchema (String codegenModelName , CodegenProperty codegenProperty , ComposedSchema composedSchema , Map <String , CodegenModel > allModels ) {
101
+ protected CodegenModel processComposedSchema (String codegenModelName , CodegenProperty codegenProperty , ComposedSchema composedSchema , Map <String , CodegenModel > allModels ) {
89
102
List <Schema > schemas = composedSchema .getOneOf ();
90
103
CodegenModel composedModel = this .createComposedModel (ONE_OF_PREFFIX + codegenModelName , schemas );
91
104
if (composedModel == null ) {
92
105
schemas = composedSchema .getAnyOf ();
93
106
composedModel = this .createComposedModel (ANY_OF_PREFFIX + codegenModelName , schemas );
94
107
if (composedModel == null ) {
95
- return ;
108
+ return null ;
96
109
}
97
110
}
98
111
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 );
112
+ codegenProperty .datatype = composedModel .getClassname ();
113
+ codegenProperty .datatypeWithEnum = composedModel .getClassname ();
114
+ codegenProperty .baseType = composedModel .getClassname ();
115
+ codegenProperty .complexType = composedModel .getClassname ();
116
+ return composedModel ;
105
117
}
106
118
107
- protected void processArrayItemSchema (CodegenModel codegenModel , ArraySchema arraySchema , Map <String , CodegenModel > allModels ) {
119
+ protected CodegenModel processArrayItemSchema (CodegenModel codegenModel , ArraySchema arraySchema , Map <String , CodegenModel > allModels ) {
108
120
final Schema itemsSchema = arraySchema .getItems ();
109
121
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 ) ;
122
+ final CodegenModel composedModel = this . processComposedSchema ( codegenModel . name + ARRAY_ITEMS_SUFFIX , ( ComposedSchema ) itemsSchema , allModels );
123
+ this . updateArrayModel (codegenModel , composedModel .name , arraySchema );
124
+ return composedModel ;
113
125
}
126
+ return null ;
114
127
}
115
128
116
- protected void processArrayItemSchema (String codegenModelName , CodegenProperty codegenProperty , ArraySchema arraySchema , Map <String , CodegenModel > allModels ) {
129
+ protected CodegenModel processArrayItemSchema (String codegenModelName , CodegenProperty codegenProperty , ArraySchema arraySchema , Map <String , CodegenModel > allModels ) {
117
130
final Schema itemsSchema = arraySchema .getItems ();
118
131
if (itemsSchema instanceof ComposedSchema ) {
119
- this .processComposedSchema (codegenModelName , codegenProperty , (ComposedSchema ) itemsSchema , allModels );
132
+ final CodegenModel composedModel = this .processComposedSchema (codegenModelName + ARRAY_ITEMS_SUFFIX , codegenProperty .items , (ComposedSchema ) itemsSchema , allModels );
133
+ if (composedModel == null ) {
134
+ return null ;
135
+ }
136
+ this .updatePropertyDataType (codegenProperty , composedModel .name , arraySchema );
137
+ return composedModel ;
120
138
}
139
+ return null ;
121
140
}
122
141
123
142
protected CodegenModel createComposedModel (String name , List <Schema > schemas ) {
@@ -168,4 +187,38 @@ protected boolean hasNonObjectSchema(List<Schema> schemas) {
168
187
}
169
188
return false ;
170
189
}
190
+
191
+ protected void addComposedModel (CodegenModel composedModel ) {
192
+ if (composedModel == null ) {
193
+ return ;
194
+ }
195
+ this .composedModels .add (composedModel );
196
+ }
197
+
198
+ protected void updatePropertyDataType (CodegenProperty codegenProperty , String schemaName , ArraySchema arraySchema ) {
199
+ final Schema items = arraySchema .getItems ();
200
+ final Schema refSchema = new Schema ();
201
+ refSchema .set$ref ("#/components/schemas/" + schemaName );
202
+ arraySchema .setItems (refSchema );
203
+ codegenProperty .setDatatype (this .codegenConfig .getTypeDeclaration (arraySchema ));
204
+ codegenProperty .setDatatypeWithEnum (codegenProperty .getDatatype ());
205
+
206
+ codegenProperty .defaultValue = this .codegenConfig .toDefaultValue (arraySchema );
207
+ codegenProperty .defaultValueWithParam = this .codegenConfig .toDefaultValueWithParam (codegenProperty .baseName , arraySchema );
208
+
209
+ arraySchema .setItems (items );
210
+ }
211
+
212
+ protected void updateArrayModel (CodegenModel codegenModel , String schemaName , ArraySchema arraySchema ) {
213
+ final Schema items = arraySchema .getItems ();
214
+ final Schema refSchema = new Schema ();
215
+ refSchema .set$ref ("#/components/schemas/" + schemaName );
216
+ arraySchema .setItems (refSchema );
217
+
218
+ this .codegenConfig .addParentContainer (codegenModel , codegenModel .name , arraySchema );
219
+ codegenModel .defaultValue = this .codegenConfig .toDefaultValue (arraySchema );
220
+ codegenModel .arrayModelType = this .codegenConfig .fromProperty (codegenModel .name , arraySchema ).complexType ;
221
+
222
+ arraySchema .setItems (items );
223
+ }
171
224
}
0 commit comments