@@ -47,18 +47,18 @@ public class PythonNextgenClientCodegen extends AbstractPythonCodegen implements
47
47
public static final String PACKAGE_URL = "packageUrl" ;
48
48
public static final String DEFAULT_LIBRARY = "urllib3" ;
49
49
public static final String RECURSION_LIMIT = "recursionLimit" ;
50
- public static final String FLOAT_STRICT_TYPE = "floatStrictType" ;
51
50
public static final String DATETIME_FORMAT = "datetimeFormat" ;
52
51
public static final String DATE_FORMAT = "dateFormat" ;
52
+ public static final String MAP_NUMBER_TO = "mapNumberTo" ;
53
53
54
54
protected String packageUrl ;
55
55
protected String apiDocPath = "docs" + File .separator ;
56
56
protected String modelDocPath = "docs" + File .separator ;
57
57
protected boolean hasModelsToImport = Boolean .FALSE ;
58
58
protected boolean useOneOfDiscriminatorLookup = false ; // use oneOf discriminator's mapping for model lookup
59
- protected boolean floatStrictType = true ;
60
59
protected String datetimeFormat = "%Y-%m-%dT%H:%M:%S.%f%z" ;
61
60
protected String dateFormat = "%Y-%m-%d" ;
61
+ protected String mapNumberTo = "Union[StrictFloat, StrictInt]" ;
62
62
63
63
protected Map <Character , String > regexModifiers ;
64
64
@@ -177,8 +177,8 @@ public PythonNextgenClientCodegen() {
177
177
cliOptions .add (new CliOption (CodegenConstants .SOURCECODEONLY_GENERATION , CodegenConstants .SOURCECODEONLY_GENERATION_DESC )
178
178
.defaultValue (Boolean .FALSE .toString ()));
179
179
cliOptions .add (new CliOption (RECURSION_LIMIT , "Set the recursion limit. If not set, use the system default value." ));
180
- cliOptions .add (new CliOption (FLOAT_STRICT_TYPE , "Use strict type for float, i.e. StrictFloat or confloat(strict=true, ...) " )
181
- .defaultValue (Boolean . TRUE . toString () ));
180
+ cliOptions .add (new CliOption (MAP_NUMBER_TO , "Map number to Union[StrictFloat, StrictInt], StrictStr or float. " )
181
+ .defaultValue ("Union[StrictFloat, StrictInt]" ));
182
182
cliOptions .add (new CliOption (DATETIME_FORMAT , "datetime format for query parameters" )
183
183
.defaultValue ("%Y-%m-%dT%H:%M:%S%z" ));
184
184
cliOptions .add (new CliOption (DATE_FORMAT , "date format for query parameters" )
@@ -281,8 +281,8 @@ public void processOpts() {
281
281
additionalProperties .put (CodegenConstants .USE_ONEOF_DISCRIMINATOR_LOOKUP , useOneOfDiscriminatorLookup );
282
282
}
283
283
284
- if (additionalProperties .containsKey (FLOAT_STRICT_TYPE )) {
285
- setFloatStrictType ( convertPropertyToBooleanAndWriteBack ( FLOAT_STRICT_TYPE ));
284
+ if (additionalProperties .containsKey (MAP_NUMBER_TO )) {
285
+ setMapNumberTo ( String . valueOf ( additionalProperties . get ( MAP_NUMBER_TO ) ));
286
286
}
287
287
288
288
if (additionalProperties .containsKey (DATETIME_FORMAT )) {
@@ -478,34 +478,59 @@ private String getPydanticType(CodegenParameter cp,
478
478
} else if (cp .isNumber || cp .isFloat || cp .isDouble ) {
479
479
if (cp .hasValidation ) {
480
480
List <String > fieldCustomization = new ArrayList <>();
481
+ List <String > intFieldCustomization = new ArrayList <>();
482
+
481
483
// e.g. confloat(ge=10, le=100, strict=True)
482
484
if (cp .getMaximum () != null ) {
483
485
if (cp .getExclusiveMaximum ()) {
484
- fieldCustomization .add ("gt=" + cp .getMaximum ());
486
+ fieldCustomization .add ("lt=" + cp .getMaximum ());
487
+ intFieldCustomization .add ("lt=" + Math .ceil (Double .valueOf (cp .getMaximum ()))); // e.g. < 7.59 becomes < 8
485
488
} else {
486
- fieldCustomization .add ("ge=" + cp .getMaximum ());
489
+ fieldCustomization .add ("le=" + cp .getMaximum ());
490
+ intFieldCustomization .add ("le=" + Math .floor (Double .valueOf (cp .getMaximum ()))); // e.g. <= 7.59 becomes <= 7
487
491
}
488
492
}
489
493
if (cp .getMinimum () != null ) {
490
494
if (cp .getExclusiveMinimum ()) {
491
- fieldCustomization .add ("lt=" + cp .getMinimum ());
495
+ fieldCustomization .add ("gt=" + cp .getMinimum ());
496
+ intFieldCustomization .add ("gt=" + Math .floor (Double .valueOf (cp .getMinimum ()))); // e.g. > 7.59 becomes > 7
492
497
} else {
493
- fieldCustomization .add ("le=" + cp .getMinimum ());
498
+ fieldCustomization .add ("ge=" + cp .getMinimum ());
499
+ intFieldCustomization .add ("ge=" + Math .ceil (Double .valueOf (cp .getMinimum ()))); // e.g. >= 7.59 becomes >= 8
494
500
}
495
501
}
496
502
if (cp .getMultipleOf () != null ) {
497
503
fieldCustomization .add ("multiple_of=" + cp .getMultipleOf ());
498
504
}
499
505
500
- if (floatStrictType ) {
506
+ if ("Union[StrictFloat, StrictInt]" .equals (mapNumberTo )) {
507
+ fieldCustomization .add ("strict=True" );
508
+ intFieldCustomization .add ("strict=True" );
509
+ pydanticImports .add ("confloat" );
510
+ pydanticImports .add ("conint" );
511
+ typingImports .add ("Union" );
512
+ return String .format (Locale .ROOT , "Union[%s(%s), %s(%s)]" , "confloat" ,
513
+ StringUtils .join (fieldCustomization , ", " ),
514
+ "conint" ,
515
+ StringUtils .join (intFieldCustomization , ", " )
516
+ );
517
+ } else if ("StrictFloat" .equals (mapNumberTo )) {
501
518
fieldCustomization .add ("strict=True" );
519
+ pydanticImports .add ("confloat" );
520
+ return String .format (Locale .ROOT , "%s(%s)" , "confloat" ,
521
+ StringUtils .join (fieldCustomization , ", " ));
522
+ } else { // float
523
+ pydanticImports .add ("confloat" );
524
+ return String .format (Locale .ROOT , "%s(%s)" , "confloat" ,
525
+ StringUtils .join (fieldCustomization , ", " ));
502
526
}
503
-
504
- pydanticImports .add ("confloat" );
505
- return String .format (Locale .ROOT , "%s(%s)" , "confloat" ,
506
- StringUtils .join (fieldCustomization , ", " ));
507
527
} else {
508
- if (floatStrictType ) {
528
+ if ("Union[StrictFloat, StrictInt]" .equals (mapNumberTo )) {
529
+ typingImports .add ("Union" );
530
+ pydanticImports .add ("StrictFloat" );
531
+ pydanticImports .add ("StrictInt" );
532
+ return "Union[StrictFloat, StrictInt]" ;
533
+ } else if ("StrictFloat" .equals (mapNumberTo )) {
509
534
pydanticImports .add ("StrictFloat" );
510
535
return "StrictFloat" ;
511
536
} else {
@@ -723,34 +748,59 @@ private String getPydanticType(CodegenProperty cp,
723
748
} else if (cp .isNumber || cp .isFloat || cp .isDouble ) {
724
749
if (cp .hasValidation ) {
725
750
List <String > fieldCustomization = new ArrayList <>();
751
+ List <String > intFieldCustomization = new ArrayList <>();
752
+
726
753
// e.g. confloat(ge=10, le=100, strict=True)
727
754
if (cp .getMaximum () != null ) {
728
755
if (cp .getExclusiveMaximum ()) {
729
756
fieldCustomization .add ("lt=" + cp .getMaximum ());
757
+ intFieldCustomization .add ("lt=" + (int ) Math .ceil (Double .valueOf (cp .getMaximum ()))); // e.g. < 7.59 => < 8
730
758
} else {
731
759
fieldCustomization .add ("le=" + cp .getMaximum ());
760
+ intFieldCustomization .add ("le=" + (int ) Math .floor (Double .valueOf (cp .getMaximum ()))); // e.g. <= 7.59 => <= 7
732
761
}
733
762
}
734
763
if (cp .getMinimum () != null ) {
735
764
if (cp .getExclusiveMinimum ()) {
736
765
fieldCustomization .add ("gt=" + cp .getMinimum ());
766
+ intFieldCustomization .add ("gt=" + (int ) Math .floor (Double .valueOf (cp .getMinimum ()))); // e.g. > 7.59 => > 7
737
767
} else {
738
768
fieldCustomization .add ("ge=" + cp .getMinimum ());
769
+ intFieldCustomization .add ("ge=" + (int ) Math .ceil (Double .valueOf (cp .getMinimum ()))); // e.g. >= 7.59 => >= 8
739
770
}
740
771
}
741
772
if (cp .getMultipleOf () != null ) {
742
773
fieldCustomization .add ("multiple_of=" + cp .getMultipleOf ());
743
774
}
744
775
745
- if (floatStrictType ) {
776
+ if ("Union[StrictFloat, StrictInt]" .equals (mapNumberTo )) {
777
+ fieldCustomization .add ("strict=True" );
778
+ intFieldCustomization .add ("strict=True" );
779
+ pydanticImports .add ("confloat" );
780
+ pydanticImports .add ("conint" );
781
+ typingImports .add ("Union" );
782
+ return String .format (Locale .ROOT , "Union[%s(%s), %s(%s)]" , "confloat" ,
783
+ StringUtils .join (fieldCustomization , ", " ),
784
+ "conint" ,
785
+ StringUtils .join (intFieldCustomization , ", " )
786
+ );
787
+ } else if ("StrictFloat" .equals (mapNumberTo )) {
746
788
fieldCustomization .add ("strict=True" );
789
+ pydanticImports .add ("confloat" );
790
+ return String .format (Locale .ROOT , "%s(%s)" , "confloat" ,
791
+ StringUtils .join (fieldCustomization , ", " ));
792
+ } else { // float
793
+ pydanticImports .add ("confloat" );
794
+ return String .format (Locale .ROOT , "%s(%s)" , "confloat" ,
795
+ StringUtils .join (fieldCustomization , ", " ));
747
796
}
748
-
749
- pydanticImports .add ("confloat" );
750
- return String .format (Locale .ROOT , "%s(%s)" , "confloat" ,
751
- StringUtils .join (fieldCustomization , ", " ));
752
797
} else {
753
- if (floatStrictType ) {
798
+ if ("Union[StrictFloat, StrictInt]" .equals (mapNumberTo )) {
799
+ typingImports .add ("Union" );
800
+ pydanticImports .add ("StrictFloat" );
801
+ pydanticImports .add ("StrictInt" );
802
+ return "Union[StrictFloat, StrictInt]" ;
803
+ } else if ("StrictFloat" .equals (mapNumberTo )) {
754
804
pydanticImports .add ("StrictFloat" );
755
805
return "StrictFloat" ;
756
806
} else {
@@ -1334,8 +1384,8 @@ public void postProcessPattern(String pattern, Map<String, Object> vendorExtensi
1334
1384
}
1335
1385
}
1336
1386
1337
- vendorExtensions .put ("x-regex" , regex .replace ("\" " ,"\\ \" " ));
1338
- vendorExtensions .put ("x-pattern" , pattern .replace ("\" " ,"\\ \" " ));
1387
+ vendorExtensions .put ("x-regex" , regex .replace ("\" " , "\\ \" " ));
1388
+ vendorExtensions .put ("x-pattern" , pattern .replace ("\" " , "\\ \" " ));
1339
1389
vendorExtensions .put ("x-modifiers" , modifiers );
1340
1390
}
1341
1391
}
@@ -1529,8 +1579,14 @@ public String escapeReservedWord(String name) {
1529
1579
return "var_" + name ;
1530
1580
}
1531
1581
1532
- public void setFloatStrictType (boolean floatStrictType ) {
1533
- this .floatStrictType = floatStrictType ;
1582
+ public void setMapNumberTo (String mapNumberTo ) {
1583
+ if ("Union[StrictFloat, StrictInt]" .equals (mapNumberTo )
1584
+ || "StrictFloat" .equals (mapNumberTo )
1585
+ || "float" .equals (mapNumberTo )) {
1586
+ this .mapNumberTo = mapNumberTo ;
1587
+ } else {
1588
+ throw new IllegalArgumentException ("mapNumberTo value must be Union[StrictFloat, StrictInt], StrictStr or float" );
1589
+ }
1534
1590
}
1535
1591
1536
1592
public void setDatetimeFormat (String datetimeFormat ) {
0 commit comments