fix(rust): use serde_repr for integer property enums#23580
Open
ericbsantana wants to merge 2 commits intoOpenAPITools:masterfrom
Open
fix(rust): use serde_repr for integer property enums#23580ericbsantana wants to merge 2 commits intoOpenAPITools:masterfrom
ericbsantana wants to merge 2 commits intoOpenAPITools:masterfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When an OpenAPI schema defines a struct property with
type: integerand anenumconstraint, Rust generator was producing a string-renamed enum:#[serde(rename = "0")]expects the JSON string"0", but the API returns the integer0. Thus serialization fails.Issue related: #23450
Root cause
The
{{#vars}}{{#isEnum}}section inmodel.mustachehad only a string-rename path. TheisIntegerflag was already set correctly onCodegenPropertybut was never checked. Additionally,AbstractRustCodegen.toEnumValue()always escaped values as strings regardless of the property datatype.In order to fix this, we configured a similar approach as other strictly typed languages generators by returning the raw integer datatypes instead of escaping as a string (as it was being done). It also sets
x-rust-has-integer-property-enumvendor extension on any struct model that contains at least one integer-enum property.model.mustachewas changed too adding{{#isInteger}}/{{^isInteger}}branching inside the enum section.Generated output after fix
Schema that triggers the bug
Source: Fireblocks OpenAPI spec.
PR checklist
masterSummary by cubic
Fix Rust generator to serialize integer-backed property enums as numbers using
serde_repr, preventing JSON string/number mismatches. Models with such properties now emit numeric#[repr(i64)]enums.#[repr(i64)]enums withSerialize_repr/Deserialize_reprfor integer property enums; variants use numeric discriminants.toEnumValuefor numeric datatypes.x-rust-has-integer-property-enumto importserde_repronce per model and branch enum rendering onisInteger; string enums unchanged.serde_reprusage and absence of string renames.Written for commit 1ccaaff. Summary will update on new commits.