OpenAPITools / OpenAPITools/openapi-generator

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

Open
#20,626 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with config.yaml and example-input.yaml, generate the Rust client, and inspect the generated src/models/foo.rs and its dependency declarations. Run cargo build to reproduce the missing models::Decimal error. Done means the provided specification generates a client that compiles, with the chosen precise-number handling documented by the generated project configuration.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, rust
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.