16
16
import io .swagger .codegen .v3 .CodegenProperty ;
17
17
import io .swagger .codegen .v3 .CodegenResponse ;
18
18
import io .swagger .codegen .v3 .CodegenSecurity ;
19
+ import io .swagger .codegen .v3 .ISchemaHandler ;
19
20
import io .swagger .codegen .v3 .SupportingFile ;
20
21
import io .swagger .codegen .v3 .generators .examples .ExampleGenerator ;
21
22
import io .swagger .codegen .v3 .generators .handlebars .BaseItemsHelper ;
@@ -160,7 +161,6 @@ public abstract class DefaultCodegenConfig implements CodegenConfig {
160
161
protected String httpUserAgent ;
161
162
protected Boolean hideGenerationTimestamp = true ;
162
163
protected TemplateEngine templateEngine = new HandlebarTemplateEngine (this );
163
- protected SchemaHandler schemaHandler = new SchemaHandler (this );
164
164
// How to encode special characters like $
165
165
// They are translated to words like "Dollar" and prefixed with '
166
166
// Then translated back during JSON encoding and decoding
@@ -171,6 +171,7 @@ public abstract class DefaultCodegenConfig implements CodegenConfig {
171
171
protected String ignoreFilePathOverride ;
172
172
protected boolean useOas2 = false ;
173
173
protected boolean copyFistAllOfProperties = false ;
174
+ protected boolean ignoreImportMapping ;
174
175
175
176
public List <CliOption > cliOptions () {
176
177
return cliOptions ;
@@ -181,6 +182,12 @@ public void processOpts() {
181
182
this .setTemplateDir ((String ) additionalProperties .get (CodegenConstants .TEMPLATE_DIR ));
182
183
}
183
184
185
+ if (additionalProperties .get (CodegenConstants .IGNORE_IMPORT_MAPPING_OPTION ) != null ) {
186
+ setIgnoreImportMapping (Boolean .parseBoolean ( additionalProperties .get (CodegenConstants .IGNORE_IMPORT_MAPPING_OPTION ).toString ()));
187
+ } else {
188
+ setIgnoreImportMapping (defaultIgnoreImportMappingOption ());
189
+ }
190
+
184
191
if (additionalProperties .containsKey (CodegenConstants .TEMPLATE_VERSION )) {
185
192
this .setTemplateVersion ((String ) additionalProperties .get (CodegenConstants .TEMPLATE_VERSION ));
186
193
}
@@ -260,28 +267,6 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> processedMod
260
267
if (supportsInheritance ) {
261
268
processCodegenModels (allModels );
262
269
}
263
- for (String modelName : allModels .keySet ()) {
264
- final CodegenModel codegenModel = allModels .get (modelName );
265
- if (!codegenModel .vendorExtensions .containsKey ("x-is-composed-model" )) {
266
- continue ;
267
- }
268
- List <String > modelNames = (List <String >) codegenModel .vendorExtensions .get ("x-model-names" );
269
- if (modelNames == null || modelNames .isEmpty ()) {
270
- continue ;
271
- }
272
- for (String name : modelNames ) {
273
- final CodegenModel model = allModels .get (name );
274
- if (model == null ) {
275
- continue ;
276
- }
277
- if (model .interfaceModels == null ) {
278
- model .interfaceModels = new ArrayList <>();
279
- }
280
- if (!model .interfaceModels .stream ().anyMatch (value -> value .name .equalsIgnoreCase (modelName ))) {
281
- model .interfaceModels .add (codegenModel );
282
- }
283
- }
284
- }
285
270
return processedModels ;
286
271
}
287
272
@@ -1077,7 +1062,7 @@ private static String getTypeOfSchema(Schema schema) {
1077
1062
} else if (schema instanceof BinarySchema ) {
1078
1063
return SchemaTypeUtil .BINARY_FORMAT ;
1079
1064
} else if (schema instanceof FileSchema ) {
1080
- return "file" ; // FIXME: this type does not exist in the OpenAPI 3.0 specification
1065
+ return SchemaTypeUtil . BINARY_FORMAT ;
1081
1066
} else if (schema instanceof BooleanSchema ) {
1082
1067
return SchemaTypeUtil .BOOLEAN_TYPE ;
1083
1068
} else if (schema instanceof DateSchema ) {
@@ -1292,12 +1277,6 @@ public CodegenModel fromModel(String name, Schema schema, Map<String, Schema> al
1292
1277
if (schema instanceof ArraySchema ) {
1293
1278
codegenModel .getVendorExtensions ().put (IS_ARRAY_MODEL_EXT_NAME , Boolean .TRUE );
1294
1279
codegenModel .getVendorExtensions ().put (IS_CONTAINER_EXT_NAME , Boolean .TRUE );
1295
-
1296
- final Schema items = ((ArraySchema ) schema ).getItems ();
1297
- if (items != null && items instanceof ComposedSchema ) {
1298
- schemaHandler .configureComposedModelFromSchemaItems (codegenModel , ((ComposedSchema ) items ));
1299
- }
1300
-
1301
1280
codegenModel .arrayModelType = fromProperty (name , schema ).complexType ;
1302
1281
addParentContainer (codegenModel , name , schema );
1303
1282
}
@@ -1350,16 +1329,9 @@ else if (schema instanceof ComposedSchema) {
1350
1329
// parent model
1351
1330
final String parentName = getParentName (composed );
1352
1331
final Schema parent = StringUtils .isBlank (parentName ) ? null : allDefinitions .get (parentName );
1353
-
1354
1332
final List <Schema > allOf = composed .getAllOf ();
1355
- final List <Schema > oneOf = composed .getOneOf ();
1356
- final List <Schema > anyOf = composed .getAnyOf ();
1357
-
1358
1333
// interfaces (intermediate models)
1359
1334
if (allOf != null && !allOf .isEmpty ()) {
1360
- if (codegenModel .interfaces == null ) {
1361
- codegenModel .interfaces = new ArrayList <String >();
1362
- }
1363
1335
for (int i = 0 ; i < allOf .size (); i ++) {
1364
1336
if (i == 0 && !copyFistAllOfProperties ) {
1365
1337
continue ;
@@ -1374,7 +1346,6 @@ else if (schema instanceof ComposedSchema) {
1374
1346
refSchema = allDefinitions .get (ref );
1375
1347
}
1376
1348
final String modelName = toModelName (ref );
1377
- codegenModel .interfaces .add (modelName );
1378
1349
addImport (codegenModel , modelName );
1379
1350
if (allDefinitions != null && refSchema != null ) {
1380
1351
if (!supportsMixins ) {
@@ -1386,14 +1357,6 @@ else if (schema instanceof ComposedSchema) {
1386
1357
}
1387
1358
}
1388
1359
}
1389
- if (oneOf != null && !oneOf .isEmpty ()) {
1390
- codegenModel .dataType = getSchemaType (composed );
1391
- this .schemaHandler .configureOneOfModel (codegenModel , oneOf );
1392
- }
1393
- if (anyOf != null && !anyOf .isEmpty ()) {
1394
- codegenModel .dataType = getSchemaType (composed );
1395
- this .schemaHandler .configureAnyOfModel (codegenModel , anyOf );
1396
- }
1397
1360
if (parent != null ) {
1398
1361
codegenModel .parentSchema = parentName ;
1399
1362
codegenModel .parent = toModelName (parentName );
@@ -1701,9 +1664,6 @@ public CodegenProperty fromProperty(String name, Schema propertySchema) {
1701
1664
// handle inner property
1702
1665
CodegenProperty cp = fromProperty ("inner" , (Schema ) propertySchema .getAdditionalProperties ());
1703
1666
updatePropertyForMap (codegenProperty , cp );
1704
- } else if (propertySchema instanceof ComposedSchema ) {
1705
- ComposedSchema composedProperty = (ComposedSchema ) propertySchema ;
1706
- this .schemaHandler .createCodegenModel (composedProperty , codegenProperty );
1707
1667
} else if (propertySchema instanceof MapSchema && hasTrueAdditionalProperties (propertySchema )) {
1708
1668
1709
1669
codegenProperty .getVendorExtensions ().put (CodegenConstants .IS_CONTAINER_EXT_NAME , Boolean .TRUE );
@@ -1717,6 +1677,9 @@ public CodegenProperty fromProperty(String name, Schema propertySchema) {
1717
1677
CodegenProperty cp = fromProperty ("inner" , new ObjectSchema ());
1718
1678
updatePropertyForMap (codegenProperty , cp );
1719
1679
} else {
1680
+ if (isObjectSchema (propertySchema )) {
1681
+ codegenProperty .getVendorExtensions ().put ("x-is-object" , Boolean .TRUE );
1682
+ }
1720
1683
setNonArrayMapProperty (codegenProperty , type );
1721
1684
}
1722
1685
return codegenProperty ;
@@ -3095,14 +3058,6 @@ private void addVars(CodegenModel codegenModel, List<CodegenProperty> vars, Map<
3095
3058
final CodegenProperty codegenProperty = fromProperty (key , propertySchema );
3096
3059
codegenProperty .required = mandatory .contains (key );
3097
3060
3098
- if (codegenProperty .vendorExtensions .containsKey ("oneOf-model" )) {
3099
- this .schemaHandler .configureOneOfModelFromProperty (codegenProperty , codegenModel );
3100
- }
3101
-
3102
- if (codegenProperty .vendorExtensions .containsKey ("anyOf-model" )) {
3103
- this .schemaHandler .configureAnyOfModelFromProperty (codegenProperty , codegenModel );
3104
- }
3105
-
3106
3061
if (propertySchema .get$ref () != null ) {
3107
3062
if (this .openAPI == null ) {
3108
3063
LOGGER .warn ("open api utility object was not properly set." );
@@ -3164,6 +3119,29 @@ private void addVars(CodegenModel codegenModel, List<CodegenProperty> vars, Map<
3164
3119
codegenModel .readWriteVars .add (codegenProperty );
3165
3120
}
3166
3121
}
3122
+ // check if one of the property is a object and has import mapping.
3123
+ List <CodegenProperty > modelProperties = vars .stream ()
3124
+ .filter (codegenProperty -> getBooleanValue (codegenProperty , "x-is-object" ) && importMapping .containsKey (codegenProperty .baseType ))
3125
+ .collect (Collectors .toList ());
3126
+ if (modelProperties == null || modelProperties .isEmpty ()) {
3127
+ return ;
3128
+ }
3129
+
3130
+ for (CodegenProperty modelProperty : modelProperties ) {
3131
+ List <CodegenProperty > codegenProperties = vars .stream ()
3132
+ .filter (codegenProperty -> !getBooleanValue (codegenProperty , "x-is-object" )
3133
+ && importMapping .containsKey (codegenProperty .baseType )
3134
+ && codegenProperty .baseType .equals (modelProperty .baseType ))
3135
+ .collect (Collectors .toList ());
3136
+ if (codegenProperties == null || codegenProperties .isEmpty ()) {
3137
+ continue ;
3138
+ }
3139
+ for (CodegenProperty codegenProperty : codegenProperties ) {
3140
+ codegenModel .imports .remove (codegenProperty .baseType );
3141
+ codegenProperty .datatype = importMapping .get (codegenProperty .baseType );
3142
+ codegenProperty .datatypeWithEnum = codegenProperty .datatype ;
3143
+ }
3144
+ }
3167
3145
}
3168
3146
3169
3147
/**
@@ -4163,7 +4141,10 @@ else if (Parameter.StyleEnum.SPACEDELIMITED.equals(parameter.getStyle())) {
4163
4141
}
4164
4142
4165
4143
public boolean isObjectSchema (Schema schema ) {
4166
- if (schema instanceof ObjectSchema ||schema instanceof ComposedSchema ) {
4144
+ if (schema == null ) {
4145
+ return false ;
4146
+ }
4147
+ if (schema instanceof ObjectSchema || schema instanceof ComposedSchema ) {
4167
4148
return true ;
4168
4149
}
4169
4150
if (SchemaTypeUtil .OBJECT_TYPE .equalsIgnoreCase (schema .getType ()) && !(schema instanceof MapSchema )) {
@@ -4257,4 +4238,20 @@ public boolean needsUnflattenedSpec() {
4257
4238
public void setUnflattenedOpenAPI (OpenAPI unflattenedOpenAPI ) {
4258
4239
this .unflattenedOpenAPI = unflattenedOpenAPI ;
4259
4240
}
4241
+
4242
+ public boolean getIgnoreImportMapping () {
4243
+ return ignoreImportMapping ;
4244
+ }
4245
+
4246
+ public void setIgnoreImportMapping (boolean ignoreImportMapping ) {
4247
+ this .ignoreImportMapping = ignoreImportMapping ;
4248
+ }
4249
+
4250
+ public boolean defaultIgnoreImportMappingOption () {
4251
+ return false ;
4252
+ }
4253
+
4254
+ public ISchemaHandler getSchemaHandler () {
4255
+ return new SchemaHandler (this );
4256
+ }
4260
4257
}
0 commit comments