OpenAPITools / OpenAPITools/openapi-generator

[BUG][Dart] Incorrectly handling of `type: string` combined with `format: decimal`

Open
#14,484 3 comments 1 reaction 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

The Dart generator maps properties with type: string and format: decimal as doubles when parsing JSON. The API returns a string, as described in the schema. Given the type mismatch, the mapValueOfType helper method sets the field to null.

If the field is required, a runtime error occurs because the fromJson method uses the bang operator (!) to assert the field is not null but mapValueOfType returns null.

openapi-generator version

The incorrect mapping of decimal to double has existed since at least 5.4.0. The runtime issue appeared in 6.0.0 with the introduction of the bang operator (!).

I am currently using 6.2.1.

OpenAPI declaration file content or url

https://raw.githubusercontent.com/alpacahq/bkdocs/564d49f52bc91bb180b15b2b900253a1e7c53846/assets/openapi.yaml

Generation Details
java -jar openapi-generator-cli.jar generate -i https://raw.githubusercontent.com/alpacahq/bkdocs/564d49f52bc91bb180b15b2b900253a1e7c53846/assets/openapi.yaml -g dart --global-property apiDocs=false,modelDocs=false -o tmp
Steps to reproduce
  1. Generate the client library.
  2. Note the use of lastEquity: mapValueOfType<double>(json, r'last_equity') in tmp/lib/model/account_extended.dart.
  3. Call AccountExtended.fromJson() with last_equity set to a string value.
  4. Observe that the return value for lastEquity is null.
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/10836

Suggest a fix

Consider using an actual Decimal type when parsing decimal values: https://pub.dev/packages/decimal.

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 the generation command and the generated tmp/lib/model/account_extended.dart, focusing on the lastEquity mapValueOfType call and the referenced OpenAPI schema. Reproduce with a string value for last_equity; done means the generated Dart client handles the declared string/decimal field without returning null or triggering a required-field runtime error.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.