Skip to content

fix(rust): use serde_repr for integer property enums#23580

Open
ericbsantana wants to merge 1 commit intoOpenAPITools:masterfrom
ericbsantana:master
Open

fix(rust): use serde_repr for integer property enums#23580
ericbsantana wants to merge 1 commit intoOpenAPITools:masterfrom
ericbsantana:master

Conversation

@ericbsantana
Copy link
Copy Markdown

@ericbsantana ericbsantana commented Apr 17, 2026

Problem

When an OpenAPI schema defines a struct property with type: integer and an enum constraint, Rust generator was producing a string-renamed enum:

// Generated (wrong)
#[derive(... Serialize, Deserialize)]
pub enum V {
    #[serde(rename = "0")]
    Variant0,
    #[serde(rename = "1")]
    Variant1,
}

#[serde(rename = "0")] expects the JSON string "0", but the API returns the integer 0. Thus serialization fails.

Issue related: #23450

Root cause

The {{#vars}}{{#isEnum}} section in model.mustache had only a string-rename path. The isInteger flag was already set correctly on CodegenProperty but 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-enum vendor extension on any struct model that contains at least one integer-enum property. model.mustache was changed too adding {{#isInteger}} / {{^isInteger}} branching inside the enum section.

Generated output after fix

use serde_repr::{Serialize_repr,Deserialize_repr};

pub struct SignedMessageSignature {
    pub v: Option<V>,
    // ...
}

#[repr(i64)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize_repr, Deserialize_repr)]
pub enum V {
    Variant0 = 0,
    Variant1 = 1,
}

Schema that triggers the bug

SignedMessageSignature:
  type: object
  properties:
    v:
      type: integer
      enum: [0, 1]

Source: Fireblocks OpenAPI spec.


PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    Commit all changed files.
  • File the PR against the master
  • @mention the Rust technical committee members for review: @frol @farcaller @richardwhiuk @paladinzh @jacob-pro @dsteeley

Summary by cubic

Fix Rust generator to serialize integer-backed property enums as numbers using serde_repr, avoiding JSON string/number mismatches. Models with such properties now emit #[repr(i64)] enums with numeric discriminants.

  • Bug Fixes
    • Generate #[repr(i64)] enums with Serialize_repr/Deserialize_repr for integer property enums; variants assigned numeric values.
    • Update toEnumValue to return raw numbers for numeric datatypes instead of escaped strings.
    • Add x-rust-has-integer-property-enum vendor extension to import serde_repr once per model and branch enum rendering on isInteger.
    • Keep string enums unchanged.
    • Add test and sample spec to verify serde_repr usage and absence of string renames.

Written for commit ff0b8db. Summary will update on new commits.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 5 files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant