OpenAPITools / OpenAPITools/openapi-generator

Go Generator Incorrectly handles string with decimal format

Open
#10,836 2 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

When handling properties with

type: string
format: decimal

the golang generator incorrectly assumes a JSON float/numeric type.
This causes an Unmarshall error when encountering a string in the API response.

openapi-generator version

5.3.0

OpenAPI declaration file content or url

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

Generation Details
Steps to reproduce

Start a new golang project in go 1.16 or higher.
Run go mod init in the project
Create a folder for the openapi package and cd to the folder
Run

openapi-generator-cli generate -g go -i https://raw.githubusercontent.com/alpacahq/bkdocs/564d49f52bc91bb180b15b2b900253a1e7c53846/assets/openapi.yaml -o .

Then go mod tidy

IF you have an Alpaca Broker account, then you can reproduce this fully. Otherwise, here is what happens:

You make an API call that would return fields annotated as above. You will get an Unmarshall error because you are attempting to Unmarshall a string into a float64.

One thing to note - generating for akka-scala seems to build in the correct conversions. BigDecimal is used, but it correctly parses a string response from the API.

Related issues/PRs

Could not find anything.

Suggest a fix

Fixing the struct tag should do the trick -

from

DollarBills float64 `json:"dollarBills"` 

to

DollarBills float64 `json:"dollarBills,string"` 

Can this be modified in the template or generator logic?

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 Go generator command and the linked OpenAPI declaration, focusing on properties declared as type string with format decimal. Inspect the generated Go model and its JSON unmarshalling behavior using the reproduction steps. Done means the generated client accepts the documented string response without an unmarshalling error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.