Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go Generator Incorrectly handles string with decimal format #10836

Open
5 of 6 tasks
zh4ngx opened this issue Nov 11, 2021 · 2 comments
Open
5 of 6 tasks

Go Generator Incorrectly handles string with decimal format #10836

zh4ngx opened this issue Nov 11, 2021 · 2 comments

Comments

@zh4ngx
Copy link

zh4ngx commented Nov 11, 2021

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?

@zh4ngx
Copy link
Author

zh4ngx commented Nov 11, 2021

I did find these lines here:

} else if (ModelUtils.isDecimalSchema(schema)) {
// special handle of type: string, format: number
return "decimal";
} else if (ModelUtils.isByteArraySchema(schema)) {

This was merged in a PR back in Nov 2020
#7808

In this file:

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java

we have this:

    public static boolean isDecimalSchema(Schema schema) {
        if (SchemaTypeUtil.STRING_TYPE.equals(schema.getType()) // type: string
                && "number".equals(schema.getFormat())) { // format: number
            return true;
        }
        return false;
    }

@zh4ngx
Copy link
Author

zh4ngx commented Nov 11, 2021

I believe the fix would result in "string" being added to this line the the mustache template:

https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/go/model_simple.mustache#L22

	{{name}} {{^required}}{{^isNullable}}*{{/isNullable}}{{/required}}{{{dataType}}} `json:"{{baseName}}{{^required}},omitempty{{/required}}"{{#withXml}} xml:"{{baseName}}{{#isXmlAttribute}},attr{{/isXmlAttribute}}"{{/withXml}}{{#vendorExtensions.x-go-custom-tag}} {{{.}}}{{/vendorExtensions.x-go-custom-tag}}`

JasonTashtego pushed a commit to JasonTashtego/openapi-generator that referenced this issue Nov 3, 2022
Allows specification of the source type to the Go JSON marshaller.

Example json: "myValue": "0.10"

YAML:

myValue:
   type: number
      format: double
      x-go-srctype-tag:
      - string

Generated Go code with tag:

MyValue *float64  `json:"myValue,omitempty,string"`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant