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

[Swfit4] better support for type=string, format=number #3910

Merged
merged 3 commits into from
Sep 25, 2019

Conversation

siilats
Copy link
Contributor

@siilats siilats commented Sep 18, 2019

PR checklist

  • [ x] Read the contribution guidelines.
  • [ x] Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh, ./bin/openapi3/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\. If contributing template-only or documentation-only changes which will change sample output, be sure to build the project first.
  • [x ] Filed the PR against the correct branch: master, 4.1.x, 5.0.x. Default: master.
  • [x ] Copied the technical committee to review the pull request if your PR is targeting a particular programming language.

Description of the PR

(details of the change, additional tests that have been done, reference to the issue for tracking, etc)

@siilats
Copy link
Contributor Author

siilats commented Sep 18, 2019

@lemoinem
Copy link
Contributor

Great idea!

But before supporting decimal properly in one generator, maybe we should add something so the base Codegen supports them properly.
Currently, there is a long list of warning when using a OpenAPI spec with decimal in it.

You should add a DECIMAL_FORMAT in SchemaTypeUtils to return "decimal" as a primitive type. This would allow us to avoid the warning at https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java#L1561-L1570 for decimals.

The default typeMapping associated with decimal should be the same as for number. This would allow us to avoid the warnings and properly support the decimal type in other generators as well.

@siilats
Copy link
Contributor Author

siilats commented Sep 19, 2019 via email

@wing328
Copy link
Member

wing328 commented Sep 20, 2019

It does not return error but BigDecimal, its a bit of hack calling it a
string but that hack is currently in master, so

Right, agreed it's a bit of hack as only string format supports any value according to the spec. In other words, we can't do "type: number, format: decimal" as it's invalid according to the spec.

Ref: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#data-types

@lemoinem
Copy link
Contributor

It does not return error but BigDecimal, its a bit of hack calling it a
string but that hack is currently in master, so

Right, agreed it's a bit of hack as only string format supports any value according to the spec. In other words, we can't do "type: number, format: decimal" as it's invalid according to the spec.

Ref: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#data-types

From your reference:

OAS uses several known formats to define in fine detail the data type being used. However, to support documentation needs, the format property is an open string-valued property, and can have any value.
And
Tools that do not recognize a specific format MAY default back to the type alone, as if the format is not specified.

So, according to the spec, any type support arbitrary format. There is no restriction. The list below the text, is only the set of format that should be supported by all implementations.

@siilats
Copy link
Contributor Author

siilats commented Sep 20, 2019 via email

@wing328
Copy link
Member

wing328 commented Sep 20, 2019

So, according to the spec, any type support arbitrary format. There is no restriction. The list below the text, is only the set of format that should be supported by all implementations.

Good point. I re-read it and you're right. My answer is based on the discussion for OpenAPI/Swagger spec 2.0 a few years ago. Don't recall why we used "string" for type instead of number back then.

What is your recommendation? I like using string:decimal and alias of
BigDecimal to Decimal in swift4.

I suggest we stick with this implementation as this is what other generators are using at the moment.

v5.x is a better candidate to revisit the mapping and whether we should add other format to better support "number", "integer", etc

@siilats
Copy link
Contributor Author

siilats commented Sep 20, 2019 via email

@@ -1068,7 +1068,7 @@ public DefaultCodegen() {
typeMapping.put("file", "File");
typeMapping.put("UUID", "UUID");
typeMapping.put("URI", "URI");
//typeMapping.put("BigDecimal", "BigDecimal"); //TODO need the mapping?
typeMapping.put("BigDecimal", "BigDecimal"); //TODO need the mapping?
Copy link
Member

@wing328 wing328 Sep 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@siilats why do you need to uncomment the mapping in the default codegen as it's already done in Swfit4Codegen:

as part of this PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you remove this, I'm happy to merge the PR and discuss the mapping of BigDecimal in the default codegen if needed.

@siilats
Copy link
Contributor Author

siilats commented Sep 23, 2019 via email

@wing328
Copy link
Member

wing328 commented Sep 23, 2019

Not sure why it needs to be there, but if its not there, then it fails.

OK. I'll take another look later this week to see if I can figure out why.

@wing328 wing328 merged commit e20af77 into OpenAPITools:master Sep 25, 2019
@wing328 wing328 changed the title Decimal in swift4 [Swfit4] better support for type=string, format=number Sep 25, 2019
@wing328
Copy link
Member

wing328 commented Sep 25, 2019

That line is needed for Java generators. I'll add a test case in a separate PR to better test type=string, format=number

jimschubert added a commit that referenced this pull request Sep 30, 2019
* master: (207 commits)
  Add missing enum processing in C++ codegen, already present for Qt5 (#3986)
  [C++] [Pistache] Removed deprecated warnings (#3985)
  [C++][Pistache] Simplified model template (#3417)
  add go oas3 petstore to ensure up-to-date (#3979)
  replace gitter with slack in the doc (#3977)
  Fix wrong variable name in LessThan and LessThanOrEqual asserts (#3971)
  #3957 - Removed hardcoded baseUrl (#3964)
  Regenerate go openapi3 samples (#3975)
  [rust] Make it easier to test rust client generator (#3543)
  Fix issue3635 (#3948)
  add gradle repository (#3867)
  [java] allow to use setArtifactVersion() programmatically (#3907)
  Add a link to DevRelCon SF 2019 (#3961)
  Add a link to a medium blog post (#3960)
  update maven-compiler-plugin version (#3956)
  fix generateAliasAsModels in default generator (#3951)
  Implement BigDecimal to Decimal in swift4 for currency data as type=string format=number (#3910)
  Add F# Functions server generator (#3933)
  [python-experimental] generate model if type != object if enums/validations exist (#2757)
  [scala] add [date-time] field to codegen unit test (#3939)
  ...
Jesse0Michael pushed a commit to Jesse0Michael/openapi-generator that referenced this pull request Oct 3, 2019
jimschubert added a commit that referenced this pull request Oct 4, 2019
* master: (110 commits)
  [golang] Regenerate all go samples  (#3988)
  Better tests for string (number) (#3953)
  Add missing enum processing in C++ codegen, already present for Qt5 (#3986)
  [C++] [Pistache] Removed deprecated warnings (#3985)
  [C++][Pistache] Simplified model template (#3417)
  add go oas3 petstore to ensure up-to-date (#3979)
  replace gitter with slack in the doc (#3977)
  Fix wrong variable name in LessThan and LessThanOrEqual asserts (#3971)
  #3957 - Removed hardcoded baseUrl (#3964)
  Regenerate go openapi3 samples (#3975)
  [rust] Make it easier to test rust client generator (#3543)
  Fix issue3635 (#3948)
  add gradle repository (#3867)
  [java] allow to use setArtifactVersion() programmatically (#3907)
  Add a link to DevRelCon SF 2019 (#3961)
  Add a link to a medium blog post (#3960)
  update maven-compiler-plugin version (#3956)
  fix generateAliasAsModels in default generator (#3951)
  Implement BigDecimal to Decimal in swift4 for currency data as type=string format=number (#3910)
  Add F# Functions server generator (#3933)
  ...
@wing328
Copy link
Member

wing328 commented Oct 4, 2019

@siilats thanks for the PR, which has been included in the v4.1.3 release: https://twitter.com/oas_generator/status/1180123829626003456

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants