Open zh4ngx opened 2 years ago
I did find these lines here:
This was merged in a PR back in Nov 2020 https://github.com/OpenAPITools/openapi-generator/pull/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;
}
I believe the fix would result in "string"
being added to this line the the mustache template:
{{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}}`
Bug Report Checklist
Description
When handling properties with
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 RunThen
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 afloat64
.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
to
Can this be modified in the template or generator logic?