23
23
import io .swagger .v3 .oas .models .media .Schema ;
24
24
import io .swagger .v3 .oas .models .media .StringSchema ;
25
25
import io .swagger .v3 .parser .util .SchemaTypeUtil ;
26
+ import joptsimple .internal .Strings ;
26
27
import org .openapitools .codegen .*;
27
28
import org .openapitools .codegen .meta .features .*;
28
29
import org .openapitools .codegen .model .ModelMap ;
38
39
import java .io .IOException ;
39
40
import java .io .Writer ;
40
41
import java .math .BigDecimal ;
42
+ import java .math .BigInteger ;
41
43
import java .util .*;
42
44
43
45
import static org .openapitools .codegen .utils .StringUtils .camelize ;
@@ -66,7 +68,6 @@ public class RustClientCodegen extends AbstractRustCodegen implements CodegenCon
66
68
protected String modelDocPath = "docs/" ;
67
69
protected String apiFolder = "src/apis" ;
68
70
protected String modelFolder = "src/models" ;
69
- protected Map <String , String > unsignedMapping ;
70
71
71
72
public CodegenType getTag () {
72
73
return CodegenType .CLIENT ;
@@ -160,12 +161,6 @@ public RustClientCodegen() {
160
161
typeMapping .put ("object" , "serde_json::Value" );
161
162
typeMapping .put ("AnyType" , "serde_json::Value" );
162
163
163
- unsignedMapping = new HashMap <>();
164
- unsignedMapping .put ("i8" , "u8" );
165
- unsignedMapping .put ("i16" , "u16" );
166
- unsignedMapping .put ("i32" , "u32" );
167
- unsignedMapping .put ("i64" , "u64" );
168
-
169
164
// no need for rust
170
165
//importMapping = new HashMap<String, String>();
171
166
@@ -455,38 +450,33 @@ public String getSchemaType(Schema p) {
455
450
String schemaType = super .getSchemaType (p );
456
451
String type = typeMapping .getOrDefault (schemaType , schemaType );
457
452
458
- boolean bestFit = convertPropertyToBoolean (BEST_FIT_INT );
459
- boolean unsigned = convertPropertyToBoolean (PREFER_UNSIGNED_INT );
460
-
461
- if (bestFit || unsigned ) {
462
- BigDecimal maximum = p .getMaximum ();
463
- BigDecimal minimum = p .getMinimum ();
464
-
465
- try {
466
- if (maximum != null && minimum != null ) {
467
- long max = maximum .longValueExact ();
468
- long min = minimum .longValueExact ();
469
-
470
- if (unsigned && bestFit && max <= (Byte .MAX_VALUE * 2 ) + 1 && min >= 0 ) {
471
- type = "u8" ;
472
- } else if (bestFit && max <= Byte .MAX_VALUE && min >= Byte .MIN_VALUE ) {
473
- type = "i8" ;
474
- } else if (unsigned && bestFit && max <= (Short .MAX_VALUE * 2 ) + 1 && min >= 0 ) {
475
- type = "u16" ;
476
- } else if (bestFit && max <= Short .MAX_VALUE && min >= Short .MIN_VALUE ) {
477
- type = "i16" ;
478
- } else if (unsigned && bestFit && max <= (Integer .MAX_VALUE * 2L ) + 1 && min >= 0 ) {
479
- type = "u32" ;
480
- } else if (bestFit && max <= Integer .MAX_VALUE && min >= Integer .MIN_VALUE ) {
481
- type = "i32" ;
482
- }
483
- }
484
- } catch (ArithmeticException a ) {
485
- // no-op; case will be handled in the next if block
486
- }
453
+ if (Objects .equals (p .getType (), "integer" )) {
454
+ boolean bestFit = convertPropertyToBoolean (BEST_FIT_INT );
455
+ boolean preferUnsigned = convertPropertyToBoolean (PREFER_UNSIGNED_INT );
456
+
457
+ BigInteger minimum = Optional .ofNullable (p .getMinimum ()).map (BigDecimal ::toBigInteger ).orElse (null );
458
+ boolean exclusiveMinimum = Optional .ofNullable (p .getExclusiveMinimum ()).orElse (false );
487
459
488
- if (unsigned && minimum != null && minimum .compareTo (BigDecimal .ZERO ) >= 0 && unsignedMapping .containsKey (type )) {
489
- type = unsignedMapping .get (type );
460
+ boolean unsigned = preferUnsigned && canFitIntoUnsigned (minimum , exclusiveMinimum );
461
+
462
+ if (Strings .isNullOrEmpty (p .getFormat ())) {
463
+ if (bestFit ) {
464
+ return bestFittingIntegerType (
465
+ minimum ,
466
+ exclusiveMinimum ,
467
+ Optional .ofNullable (p .getMaximum ()).map (BigDecimal ::toBigInteger ).orElse (null ),
468
+ Optional .ofNullable (p .getExclusiveMaximum ()).orElse (false ),
469
+ preferUnsigned );
470
+ } else {
471
+ return unsigned ? "u32" : "i32" ;
472
+ }
473
+ } else {
474
+ switch (p .getFormat ()) {
475
+ case "int32" :
476
+ return unsigned ? "u32" : "i32" ;
477
+ case "int64" :
478
+ return unsigned ? "u64" : "i64" ;
479
+ }
490
480
}
491
481
}
492
482
@@ -564,12 +554,6 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
564
554
return objs ;
565
555
}
566
556
567
- @ Override
568
- protected boolean needToImport (String type ) {
569
- return !defaultIncludes .contains (type )
570
- && !languageSpecificPrimitives .contains (type );
571
- }
572
-
573
557
public void setPackageName (String packageName ) {
574
558
this .packageName = packageName ;
575
559
}
@@ -578,17 +562,6 @@ public void setPackageVersion(String packageVersion) {
578
562
this .packageVersion = packageVersion ;
579
563
}
580
564
581
- @ Override
582
- public String escapeQuotationMark (String input ) {
583
- // remove " to avoid code injection
584
- return input .replace ("\" " , "" );
585
- }
586
-
587
- @ Override
588
- public String escapeUnsafeCharacters (String input ) {
589
- return input .replace ("*/" , "*_/" ).replace ("/*" , "/_*" );
590
- }
591
-
592
565
@ Override
593
566
public String toDefaultValue (Schema p ) {
594
567
if (p .getDefault () != null ) {
@@ -598,6 +571,4 @@ public String toDefaultValue(Schema p) {
598
571
}
599
572
}
600
573
601
- @ Override
602
- public GeneratorLanguage generatorLanguage () { return GeneratorLanguage .RUST ; }
603
574
}
0 commit comments