Skip to content

Commit 841b221

Browse files
fix(rust-axum): preserve explicit unsigned intent in integer fallback
1 parent ec51326 commit 841b221

3 files changed

Lines changed: 42 additions & 5 deletions

File tree

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,13 +1033,19 @@ private String getIntegerDataType(String format,
10331033
boolean explicitUnsigned) {
10341034
boolean unsigned = explicitUnsigned || canFitIntoUnsigned(minimum, exclusiveMinimum);
10351035

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+
}
1041+
10361042
if (StringUtils.isEmpty(format)) {
10371043
return bestFittingIntegerType(
10381044
minimum,
10391045
exclusiveMinimum,
10401046
maximum,
10411047
exclusiveMaximum,
1042-
true);
1048+
unsigned);
10431049
}
10441050

10451051
switch (format) {
@@ -1059,16 +1065,22 @@ private String getIntegerDataType(String format,
10591065
exclusiveMinimum,
10601066
maximum,
10611067
exclusiveMaximum,
1062-
true);
1068+
unsigned);
10631069
}
10641070
}
10651071

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

10731085
return getIntegerDataType(
10741086
p.getFormat(),
@@ -1174,7 +1186,9 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
11741186
if (Boolean.TRUE.equals(property.isInteger) || Boolean.TRUE.equals(property.isLong) || Objects.equals(property.baseType, "UnsignedInteger") || Objects.equals(property.baseType, "UnsignedLong")) {
11751187
BigInteger minimum = Optional.ofNullable(property.getMinimum()).map(BigInteger::new).orElse(null);
11761188
BigInteger maximum = Optional.ofNullable(property.getMaximum()).map(BigInteger::new).orElse(null);
1177-
boolean explicitUnsigned = Objects.equals(property.baseType, "UnsignedInteger") || Objects.equals(property.baseType, "UnsignedLong");
1189+
boolean explicitUnsigned = Objects.equals(property.baseType, "UnsignedInteger")
1190+
|| Objects.equals(property.baseType, "UnsignedLong")
1191+
|| hasExplicitUnsignedExtension(property.vendorExtensions);
11781192
property.dataType = getIntegerDataType(
11791193
property.dataFormat,
11801194
minimum,

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,35 @@ public void testIntegerSchemaTypeMapping() {
6363
schema.setFormat("uint32");
6464
Assert.assertEquals(codegen.getSchemaType(schema), "u32");
6565

66+
schema = new IntegerSchema();
6667
schema.setFormat("uint64");
6768
Assert.assertEquals(codegen.getSchemaType(schema), "u64");
6869

70+
schema = new IntegerSchema();
6971
schema.setFormat("int32");
7072
schema.setMinimum(BigDecimal.ZERO);
7173
Assert.assertEquals(codegen.getSchemaType(schema), "u32");
7274

75+
schema = new IntegerSchema();
7376
schema.setFormat("int64");
77+
schema.setMinimum(BigDecimal.ZERO);
7478
Assert.assertEquals(codegen.getSchemaType(schema), "u64");
7579

80+
schema = new IntegerSchema();
7681
schema.setFormat(null);
82+
schema.setMinimum(BigDecimal.ZERO);
7783
schema.setMaximum(BigDecimal.valueOf(255));
7884
Assert.assertEquals(codegen.getSchemaType(schema), "u8");
7985

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");
8093
schema.setExtensions(new HashMap<>());
8194
schema.getExtensions().put("x-unsigned", true);
82-
schema.setMaximum(null);
8395
Assert.assertEquals(codegen.getSchemaType(schema), "u32");
8496
}
8597

@@ -101,6 +113,7 @@ public void testGeneratedIntegerTypes() throws IOException {
101113
TestUtils.assertFileContains(modelsPath, "pub positive_int32: u32");
102114
TestUtils.assertFileContains(modelsPath, "pub positive_int64: u64");
103115
TestUtils.assertFileContains(modelsPath, "pub small_positive: u8");
116+
TestUtils.assertFileContains(modelsPath, "pub explicit_unsigned: u32");
104117
TestUtils.assertFileContains(modelsPath, "pub struct GetIntegersQueryParams");
105118
}
106119
}

modules/openapi-generator/src/test/resources/3_0/rust-axum/integer-types.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ paths:
4040
type: integer
4141
minimum: 0
4242
maximum: 255
43+
- name: explicit_unsigned
44+
in: query
45+
required: true
46+
schema:
47+
type: integer
48+
x-unsigned: true
4349
responses:
4450
'200':
4551
description: OK
@@ -57,6 +63,7 @@ components:
5763
- positive_int32
5864
- positive_int64
5965
- small_positive
66+
- explicit_unsigned
6067
properties:
6168
legacy_uint32:
6269
type: integer
@@ -76,3 +83,6 @@ components:
7683
type: integer
7784
minimum: 0
7885
maximum: 255
86+
explicit_unsigned:
87+
type: integer
88+
x-unsigned: true

0 commit comments

Comments
 (0)