-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
[rust-axum] Fix uint32/uint64 type mapping #23547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
ec51326
841b221
0a71018
859f939
39730e7
b62f4c3
72fbe56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,7 @@ | |
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.io.File; | ||
| import java.math.BigDecimal; | ||
| import java.math.BigInteger; | ||
| import java.nio.file.Path; | ||
| import java.util.*; | ||
|
|
@@ -1024,6 +1025,75 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S | |
| return codegenParameter; | ||
| } | ||
|
|
||
| private String getIntegerDataType(String format, | ||
| BigInteger minimum, | ||
| boolean exclusiveMinimum, | ||
| BigInteger maximum, | ||
| boolean exclusiveMaximum, | ||
| boolean explicitUnsigned) { | ||
| boolean unsigned = explicitUnsigned || canFitIntoUnsigned(minimum, exclusiveMinimum); | ||
|
|
||
| if (explicitUnsigned && !canFitIntoUnsigned(minimum, exclusiveMinimum)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder what happen if:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated rust-axum to align with
This avoids generating an invalid unsigned type for a negative-only range and keeps behavior consistent across Rust generators.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the explanation |
||
| // Preserve explicit unsigned intent (e.g. x-unsigned) even when no lower bound is provided. | ||
| minimum = BigInteger.ZERO; | ||
| exclusiveMinimum = false; | ||
| } | ||
|
|
||
| if (StringUtils.isEmpty(format)) { | ||
| return bestFittingIntegerType( | ||
| minimum, | ||
| exclusiveMinimum, | ||
| maximum, | ||
| exclusiveMaximum, | ||
| unsigned); | ||
| } | ||
|
|
||
| switch (format) { | ||
| // custom integer formats (legacy) | ||
| case "uint32": | ||
| return "u32"; | ||
| case "uint64": | ||
| return "u64"; | ||
| case "int32": | ||
| return unsigned ? "u32" : "i32"; | ||
| case "int64": | ||
| return unsigned ? "u64" : "i64"; | ||
| default: | ||
| LOGGER.warn("The integer format '{}' is not recognized and will be ignored.", format); | ||
| return bestFittingIntegerType( | ||
| minimum, | ||
| exclusiveMinimum, | ||
| maximum, | ||
| exclusiveMaximum, | ||
| unsigned); | ||
| } | ||
| } | ||
|
|
||
| private boolean hasExplicitUnsignedExtension(Map<String, Object> extensions) { | ||
|
LSX-s-Software marked this conversation as resolved.
Outdated
|
||
| return extensions != null && Boolean.TRUE.equals(extensions.get("x-unsigned")); | ||
| } | ||
|
|
||
| @Override | ||
| public String getSchemaType(Schema p) { | ||
| if (Objects.equals(p.getType(), "integer")) { | ||
| BigInteger minimum = Optional.ofNullable(p.getMinimum()).map(BigDecimal::toBigInteger).orElse(null); | ||
|
LSX-s-Software marked this conversation as resolved.
Outdated
|
||
| BigInteger maximum = Optional.ofNullable(p.getMaximum()).map(BigDecimal::toBigInteger).orElse(null); | ||
|
LSX-s-Software marked this conversation as resolved.
Outdated
|
||
| boolean explicitUnsigned = ModelUtils.isUnsignedIntegerSchema(p) | ||
|
LSX-s-Software marked this conversation as resolved.
Outdated
|
||
| || ModelUtils.isUnsignedLongSchema(p) | ||
| || hasExplicitUnsignedExtension(p.getExtensions()); | ||
|
|
||
| return getIntegerDataType( | ||
| p.getFormat(), | ||
| minimum, | ||
| Optional.ofNullable(p.getExclusiveMinimum()).orElse(false), | ||
| maximum, | ||
| Optional.ofNullable(p.getExclusiveMaximum()).orElse(false), | ||
| explicitUnsigned); | ||
| } | ||
|
|
||
| return super.getSchemaType(p); | ||
| } | ||
|
|
||
| @Override | ||
| public String toInstantiationType(final Schema p) { | ||
| if (ModelUtils.isArraySchema(p)) { | ||
|
|
@@ -1113,13 +1183,19 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert | |
| } | ||
|
|
||
| // Integer type fitting | ||
| if (Objects.equals(property.baseType, "integer")) { | ||
| if (Boolean.TRUE.equals(property.isInteger) || Boolean.TRUE.equals(property.isLong) || Objects.equals(property.baseType, "UnsignedInteger") || Objects.equals(property.baseType, "UnsignedLong")) { | ||
| BigInteger minimum = Optional.ofNullable(property.getMinimum()).map(BigInteger::new).orElse(null); | ||
| BigInteger maximum = Optional.ofNullable(property.getMaximum()).map(BigInteger::new).orElse(null); | ||
| property.dataType = bestFittingIntegerType( | ||
| minimum, property.getExclusiveMinimum(), | ||
| maximum, property.getExclusiveMaximum(), | ||
| true); | ||
| boolean explicitUnsigned = Objects.equals(property.baseType, "UnsignedInteger") | ||
|
LSX-s-Software marked this conversation as resolved.
Outdated
|
||
| || Objects.equals(property.baseType, "UnsignedLong") | ||
| || hasExplicitUnsignedExtension(property.vendorExtensions); | ||
| property.dataType = getIntegerDataType( | ||
| property.dataFormat, | ||
| minimum, | ||
| property.getExclusiveMinimum(), | ||
| maximum, | ||
| property.getExclusiveMaximum(), | ||
| explicitUnsigned); | ||
| } | ||
|
|
||
| property.name = underscore(property.name); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| openapi: 3.0.3 | ||
| info: | ||
| title: Rust Axum Integer Type Mapping Test | ||
| version: 1.0.0 | ||
| paths: | ||
| /integers: | ||
| get: | ||
| operationId: getIntegers | ||
| parameters: | ||
| - name: legacy_uint32 | ||
| in: query | ||
| required: true | ||
| schema: | ||
| type: integer | ||
| format: uint32 | ||
| - name: legacy_uint64 | ||
| in: query | ||
| required: true | ||
| schema: | ||
| type: integer | ||
| format: uint64 | ||
| - name: positive_int32 | ||
| in: query | ||
| required: true | ||
| schema: | ||
| type: integer | ||
| format: int32 | ||
| minimum: 0 | ||
| - name: positive_int64 | ||
| in: query | ||
| required: true | ||
| schema: | ||
| type: integer | ||
| format: int64 | ||
| minimum: 0 | ||
| - name: small_positive | ||
| in: query | ||
| required: true | ||
| schema: | ||
| type: integer | ||
| minimum: 0 | ||
| maximum: 255 | ||
| - name: explicit_unsigned | ||
| in: query | ||
| required: true | ||
| schema: | ||
| type: integer | ||
| x-unsigned: true | ||
| responses: | ||
| '200': | ||
| description: OK | ||
| content: | ||
| application/json: | ||
| schema: | ||
| $ref: '#/components/schemas/IntegerTypes' | ||
| components: | ||
| schemas: | ||
| IntegerTypes: | ||
| type: object | ||
| required: | ||
| - legacy_uint32 | ||
| - legacy_uint64 | ||
| - positive_int32 | ||
| - positive_int64 | ||
| - small_positive | ||
| - explicit_unsigned | ||
| properties: | ||
| legacy_uint32: | ||
| type: integer | ||
| format: uint32 | ||
| legacy_uint64: | ||
| type: integer | ||
| format: uint64 | ||
| positive_int32: | ||
| type: integer | ||
| format: int32 | ||
| minimum: 0 | ||
| positive_int64: | ||
| type: integer | ||
| format: int64 | ||
| minimum: 0 | ||
| small_positive: | ||
| type: integer | ||
| minimum: 0 | ||
| maximum: 255 | ||
| explicit_unsigned: | ||
| type: integer | ||
| x-unsigned: true |
Uh oh!
There was an error while loading. Please reload this page.