Skip to content

Commit 0a71018

Browse files
fix(rust-axum): align unsigned handling with RustClient and RustServer behavior
1 parent 841b221 commit 0a71018

2 files changed

Lines changed: 10 additions & 40 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustAxumServerCodegen.java

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,16 +1028,9 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
10281028
private String getIntegerDataType(String format,
10291029
BigInteger minimum,
10301030
boolean exclusiveMinimum,
1031-
BigInteger maximum,
1032-
boolean exclusiveMaximum,
1033-
boolean explicitUnsigned) {
1034-
boolean unsigned = explicitUnsigned || canFitIntoUnsigned(minimum, exclusiveMinimum);
1035-
1036-
if (explicitUnsigned && !canFitIntoUnsigned(minimum, exclusiveMinimum)) {
1037-
// Preserve explicit unsigned intent (e.g. x-unsigned) even when no lower bound is provided.
1038-
minimum = BigInteger.ZERO;
1039-
exclusiveMinimum = false;
1040-
}
1031+
final BigInteger maximum,
1032+
final boolean exclusiveMaximum) {
1033+
final boolean unsigned = canFitIntoUnsigned(minimum, exclusiveMinimum);
10411034

10421035
if (StringUtils.isEmpty(format)) {
10431036
return bestFittingIntegerType(
@@ -1069,26 +1062,18 @@ private String getIntegerDataType(String format,
10691062
}
10701063
}
10711064

1072-
private boolean hasExplicitUnsignedExtension(Map<String, Object> extensions) {
1073-
return extensions != null && Boolean.TRUE.equals(extensions.get("x-unsigned"));
1074-
}
1075-
10761065
@Override
10771066
public String getSchemaType(Schema p) {
10781067
if (Objects.equals(p.getType(), "integer")) {
1079-
BigInteger minimum = Optional.ofNullable(p.getMinimum()).map(BigDecimal::toBigInteger).orElse(null);
1080-
BigInteger maximum = Optional.ofNullable(p.getMaximum()).map(BigDecimal::toBigInteger).orElse(null);
1081-
boolean explicitUnsigned = ModelUtils.isUnsignedIntegerSchema(p)
1082-
|| ModelUtils.isUnsignedLongSchema(p)
1083-
|| hasExplicitUnsignedExtension(p.getExtensions());
1068+
final BigInteger minimum = Optional.ofNullable(p.getMinimum()).map(BigDecimal::toBigInteger).orElse(null);
1069+
final BigInteger maximum = Optional.ofNullable(p.getMaximum()).map(BigDecimal::toBigInteger).orElse(null);
10841070

10851071
return getIntegerDataType(
10861072
p.getFormat(),
10871073
minimum,
10881074
Optional.ofNullable(p.getExclusiveMinimum()).orElse(false),
10891075
maximum,
1090-
Optional.ofNullable(p.getExclusiveMaximum()).orElse(false),
1091-
explicitUnsigned);
1076+
Optional.ofNullable(p.getExclusiveMaximum()).orElse(false));
10921077
}
10931078

10941079
return super.getSchemaType(p);
@@ -1184,18 +1169,14 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
11841169

11851170
// Integer type fitting
11861171
if (Boolean.TRUE.equals(property.isInteger) || Boolean.TRUE.equals(property.isLong) || Objects.equals(property.baseType, "UnsignedInteger") || Objects.equals(property.baseType, "UnsignedLong")) {
1187-
BigInteger minimum = Optional.ofNullable(property.getMinimum()).map(BigInteger::new).orElse(null);
1188-
BigInteger maximum = Optional.ofNullable(property.getMaximum()).map(BigInteger::new).orElse(null);
1189-
boolean explicitUnsigned = Objects.equals(property.baseType, "UnsignedInteger")
1190-
|| Objects.equals(property.baseType, "UnsignedLong")
1191-
|| hasExplicitUnsignedExtension(property.vendorExtensions);
1172+
final BigInteger minimum = Optional.ofNullable(property.getMinimum()).map(BigInteger::new).orElse(null);
1173+
final BigInteger maximum = Optional.ofNullable(property.getMaximum()).map(BigInteger::new).orElse(null);
11921174
property.dataType = getIntegerDataType(
11931175
property.dataFormat,
11941176
minimum,
11951177
property.getExclusiveMinimum(),
11961178
maximum,
1197-
property.getExclusiveMaximum(),
1198-
explicitUnsigned);
1179+
property.getExclusiveMaximum());
11991180
}
12001181

12011182
property.name = underscore(property.name);

modules/openapi-generator/src/test/java/org/openapitools/codegen/rust/RustAxumServerCodegenTest.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,6 @@ public void testIntegerSchemaTypeMapping() {
8282
schema.setMinimum(BigDecimal.ZERO);
8383
schema.setMaximum(BigDecimal.valueOf(255));
8484
Assert.assertEquals(codegen.getSchemaType(schema), "u8");
85-
86-
schema = new IntegerSchema();
87-
schema.setExtensions(new HashMap<>());
88-
schema.getExtensions().put("x-unsigned", true);
89-
Assert.assertEquals(codegen.getSchemaType(schema), "u32");
90-
91-
schema = new IntegerSchema();
92-
schema.setFormat("custom");
93-
schema.setExtensions(new HashMap<>());
94-
schema.getExtensions().put("x-unsigned", true);
95-
Assert.assertEquals(codegen.getSchemaType(schema), "u32");
9685
}
9786

9887
@Test
@@ -113,7 +102,7 @@ public void testGeneratedIntegerTypes() throws IOException {
113102
TestUtils.assertFileContains(modelsPath, "pub positive_int32: u32");
114103
TestUtils.assertFileContains(modelsPath, "pub positive_int64: u64");
115104
TestUtils.assertFileContains(modelsPath, "pub small_positive: u8");
116-
TestUtils.assertFileContains(modelsPath, "pub explicit_unsigned: u32");
105+
TestUtils.assertFileContains(modelsPath, "pub explicit_unsigned: i32");
117106
TestUtils.assertFileContains(modelsPath, "pub struct GetIntegersQueryParams");
118107
}
119108
}

0 commit comments

Comments
 (0)