OpenAPITools / OpenAPITools/openapi-generator
[BUG][Dart] Incorrectly handling of `type: string` combined with `format: decimal`
Nobody has claimed this yet.
- 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
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
- Generate the client library.
- Note the use of
lastEquity: mapValueOfType<double>(json, r'last_equity')intmp/lib/model/account_extended.dart. - Call
AccountExtended.fromJson()withlast_equityset to a string value. - Observe that the return value for
lastEquityisnull.
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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