111
111
import static io .swagger .codegen .v3 .generators .handlebars .ExtensionHelper .getBooleanValue ;
112
112
113
113
public abstract class DefaultCodegenConfig implements CodegenConfig {
114
- private static final Logger LOGGER = LoggerFactory .getLogger (DefaultCodegenConfig .class );
114
+ protected static final Logger LOGGER = LoggerFactory .getLogger (DefaultCodegenConfig .class );
115
115
116
116
public static final String DEFAULT_CONTENT_TYPE = "application/json" ;
117
117
public static final String REQUEST_BODY_NAME = "body" ;
@@ -358,12 +358,24 @@ public void processModelEnums(Map<String, Object> objs) {
358
358
}
359
359
cm .allowableValues .put ("enumVars" , enumVars );
360
360
}
361
+ updateCodegenModelEnumVars (cm );
362
+ }
363
+ }
361
364
362
- // update codegen property enum with proper naming convention
363
- // and handling of numbers, special characters
364
- for (CodegenProperty var : cm .vars ) {
365
- updateCodegenPropertyEnum (var );
366
- }
365
+ public boolean isPrimivite (String datatype ) {
366
+ return "number" .equalsIgnoreCase (datatype )
367
+ || "integer" .equalsIgnoreCase (datatype )
368
+ || "boolean" .equalsIgnoreCase (datatype );
369
+ }
370
+
371
+ /**
372
+ * update codegen property enum with proper naming convention
373
+ * and handling of numbers, special characters
374
+ * @param codegenModel
375
+ */
376
+ protected void updateCodegenModelEnumVars (CodegenModel codegenModel ) {
377
+ for (CodegenProperty var : codegenModel .vars ) {
378
+ updateCodegenPropertyEnum (var );
367
379
}
368
380
}
369
381
@@ -1327,6 +1339,8 @@ public CodegenModel fromModel(String name, Schema schema, Map<String, Schema> al
1327
1339
}
1328
1340
codegenModel .getVendorExtensions ().put (CodegenConstants .IS_ALIAS_EXT_NAME , typeAliases .containsKey (name ));
1329
1341
1342
+ codegenModel .discriminator = schema .getDiscriminator ();
1343
+
1330
1344
if (schema .getXml () != null ) {
1331
1345
codegenModel .xmlPrefix = schema .getXml ().getPrefix ();
1332
1346
codegenModel .xmlNamespace = schema .getXml ().getNamespace ();
@@ -1392,11 +1406,8 @@ else if (schema instanceof ComposedSchema) {
1392
1406
// interfaces (intermediate models)
1393
1407
if (allOf != null && !allOf .isEmpty ()) {
1394
1408
1395
- if (schema .getDiscriminator () != null ) {
1396
- codegenModel .discriminator = schema .getDiscriminator ();
1397
- if (codegenModel .discriminator != null && codegenModel .discriminator .getPropertyName () != null ) {
1398
- codegenModel .discriminator .setPropertyName (toVarName (codegenModel .discriminator .getPropertyName ()));
1399
- }
1409
+ if (codegenModel .discriminator != null && codegenModel .discriminator .getPropertyName () != null ) {
1410
+ codegenModel .discriminator .setPropertyName (toVarName (codegenModel .discriminator .getPropertyName ()));
1400
1411
}
1401
1412
1402
1413
for (int i = 0 ; i < allOf .size (); i ++) {
@@ -1460,6 +1471,9 @@ else if (schema instanceof ComposedSchema) {
1460
1471
// comment out below as allowableValues is not set in post processing model enum
1461
1472
codegenModel .allowableValues = new HashMap <String , Object >();
1462
1473
codegenModel .allowableValues .put ("values" , schema .getEnum ());
1474
+ if (codegenModel .dataType .equals ("BigDecimal" )) {
1475
+ addImport (codegenModel , "BigDecimal" );
1476
+ }
1463
1477
}
1464
1478
addVars (codegenModel , schema .getProperties (), schema .getRequired ());
1465
1479
}
@@ -1469,6 +1483,7 @@ else if (schema instanceof ComposedSchema) {
1469
1483
postProcessModelProperty (codegenModel , prop );
1470
1484
}
1471
1485
}
1486
+
1472
1487
return codegenModel ;
1473
1488
}
1474
1489
@@ -2290,7 +2305,26 @@ public CodegenResponse fromResponse(String responseCode, ApiResponse response) {
2290
2305
final Schema responseSchema = getSchemaFromResponse (response );
2291
2306
codegenResponse .schema = responseSchema ;
2292
2307
codegenResponse .message = escapeText (response .getDescription ());
2293
- // TODO: codegenResponse.examples = toExamples(response.getExamples());
2308
+
2309
+ if (response .getContent ()!= null ) {
2310
+ Map <String , Object > examples = new HashMap <>();
2311
+ for (String name : response .getContent ().keySet ()) {
2312
+ if (response .getContent ().get (name ) != null ) {
2313
+
2314
+ if (response .getContent ().get (name ).getExample () != null ) {
2315
+ examples .put (name , response .getContent ().get (name ).getExample ());
2316
+ }
2317
+ if (response .getContent ().get (name ).getExamples () != null ) {
2318
+
2319
+ for (String exampleName : response .getContent ().get (name ).getExamples ().keySet ()) {
2320
+ examples .put (exampleName , response .getContent ().get (name ).getExamples ().get (exampleName ).getValue ());
2321
+ }
2322
+ }
2323
+ }
2324
+ }
2325
+ codegenResponse .examples = toExamples (examples );
2326
+ }
2327
+
2294
2328
codegenResponse .jsonSchema = Json .pretty (response );
2295
2329
if (response .getExtensions () != null && !response .getExtensions ().isEmpty ()) {
2296
2330
codegenResponse .vendorExtensions .putAll (response .getExtensions ());
@@ -2697,7 +2731,9 @@ else if (schema instanceof ArraySchema) {
2697
2731
if (codegenProperty .complexType != null ) {
2698
2732
imports .add (codegenProperty .complexType );
2699
2733
}
2700
- imports .add (codegenProperty .baseType );
2734
+ if (codegenParameter .baseType != null ) {
2735
+ imports .add (codegenProperty .baseType );
2736
+ }
2701
2737
CodegenProperty innerCp = codegenProperty ;
2702
2738
while (innerCp != null ) {
2703
2739
if (innerCp .complexType != null ) {
@@ -2717,7 +2753,9 @@ else if (schema instanceof ArraySchema) {
2717
2753
setParameterNullable (codegenParameter , codegenProperty );
2718
2754
2719
2755
while (codegenProperty != null ) {
2720
- imports .add (codegenProperty .baseType );
2756
+ if (codegenProperty .baseType != null ) {
2757
+ imports .add (codegenProperty .baseType );
2758
+ }
2721
2759
codegenProperty = codegenProperty .items ;
2722
2760
}
2723
2761
}
@@ -3837,7 +3875,7 @@ public void updateCodegenPropertyEnum(CodegenProperty var) {
3837
3875
}
3838
3876
3839
3877
// put "enumVars" map into `allowableValues", including `name` and `value`
3840
- List <Map <String , String >> enumVars = new ArrayList <Map < String , String > >();
3878
+ List <Map <String , String >> enumVars = new ArrayList <>();
3841
3879
String commonPrefix = findCommonPrefixOfVars (values );
3842
3880
int truncateIdx = commonPrefix .length ();
3843
3881
for (Object value : values ) {
@@ -3853,6 +3891,24 @@ public void updateCodegenPropertyEnum(CodegenProperty var) {
3853
3891
}
3854
3892
allowableValues .put ("enumVars" , enumVars );
3855
3893
3894
+ // check repeated enum var names
3895
+ if (enumVars != null & !enumVars .isEmpty ()) {
3896
+ for (int i = 0 ; i < enumVars .size (); i ++) {
3897
+ final Map <String , String > enumVarList = enumVars .get (i );
3898
+ final String enumVarName = enumVarList .get ("name" );
3899
+ for (int j = 0 ; j < enumVars .size (); j ++) {
3900
+ if (i == j ) {
3901
+ continue ;
3902
+ }
3903
+ final Map <String , String > enumVarToCheckList = enumVars .get (j );
3904
+ final String enumVarNameToCheck = enumVarToCheckList .get ("name" );
3905
+ if (enumVarName .equals (enumVarNameToCheck )) {
3906
+ enumVarToCheckList .put ("name" , enumVarName + "_" + j );
3907
+ }
3908
+ }
3909
+ }
3910
+ }
3911
+
3856
3912
// handle default value for enum, e.g. available => StatusEnum.AVAILABLE
3857
3913
if (var .defaultValue != null ) {
3858
3914
String enumName = null ;
@@ -4313,6 +4369,7 @@ protected void addParameters(CodegenContent codegenContent, List<CodegenParamete
4313
4369
protected void addCodegenContentParameters (CodegenOperation codegenOperation , List <CodegenContent > codegenContents ) {
4314
4370
for (CodegenContent content : codegenContents ) {
4315
4371
addParameters (content , codegenOperation .bodyParams );
4372
+ addParameters (content , codegenOperation .formParams );
4316
4373
addParameters (content , codegenOperation .headerParams );
4317
4374
addParameters (content , codegenOperation .queryParams );
4318
4375
addParameters (content , codegenOperation .pathParams );
0 commit comments