Skip to content

[BUG] [RUST] type: string, format: number causes compiler error #20626

Description

@ranger-ross

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

For fields with type: string, format: number the output does not compile due to a non-existent model.

see compiler error:

error[E0412]: cannot find type `Decimal` in module `models`
  --> src/models/foo.rs:18:35
   |
18 |     pub my_number: Option<models::Decimal>,
   |                                   ^^^^^^^ not found in `models`
openapi-generator version

v7.11.0

OpenAPI declaration file content or url
openapi: 3.0.0
servers:
  - url: 'http://petstore.swagger.io/v2'
info:
  description: >-
    This is a sample server Petstore server. For this sample, you can use the api key
    `special-key` to test the authorization filters.
  version: 1.0.0
  title: OpenAPI Petstore
  license:
    name: Apache-2.0
    url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
tags:
  - name: pet
    description: Everything about your Pets
paths:
  /pet:
    get:
      tags:
        - pet
      summary: Add a new pet to the store
      description: This is the description for the addPet operation
      operationId: addPet
      responses:
        '200':
          description: successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Foo'
        '405':
          description: Invalid input
components:
  schemas:
    Foo:
      title: Test
      description: test
      type: object
      properties:
        myNumber:
          type: string
          format: number

Generation Details
generatorName: rust
outputDir: example
library: reqwest-trait
inputSpec: example-input.yaml
additionalProperties:
  topLevelApiClient: true
  packageName: petstore-reqwest
  mockall: true
Steps to reproduce

Run openapi-generator-cli generate -c config.yaml where config.yaml is the config above and example-input.yaml is the example spec provided above. Then cd into the generated client and attempt to build with cargo build

Suggest a fix

The issue is that Rust does not have an equivalent to Java's BigDecimal in it's standard library. There are a handful of popular libraries in the Rust ecosystem for dealing with precise numbers. I did some research and there are 2 crates that stand out to me as contenders to be used in client gen.

  • rust_decimal
    • Last updated Aug 2024
    • 31,000,000 downloads
    • Download Size: 126 KiB
    • License: MIT
    • stable release: YES
  • bigdecimal
    • Last updated Dec 2024
    • 26,000,000 downloads
    • Download Size: 87.2 KiB
    • License: MIT or Apache 2.0
    • stable release: NO

Both crates are well maintained and heavily used by the community.
The big difference is that rust_decimal uses a 96 bit integer with a scaling factor while bigdecimal uses a heap allocated arbitrary precision number.

In my experience rust_decimal is a bit more ergonomic to work with and will cover most usecases so I would favor using this crate.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions