Skip to content

[JAVA][BUG] Do not use valueOf for numeric types for generating inner enums #20293

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

Conversation

timon-sbr
Copy link

@timon-sbr timon-sbr commented Dec 11, 2024

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Technical Comittee

@bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10) @martin-mfg (2023/08)

Description

The template file src/main/resources/Java/modelInnerEnum.mustache was using the valueOf() method to create instances for enum fields. This does not work for every Object, because valueOf() is not implemented in every possible Object. BigDecimal for example does not provide the valueOf(BigDecimal) method. This behaviour results in invalid code through generation.
To fix this, I decided to only use the valueOf method, for non numeric types. It seems the decision to use the valueOf method was, to fix a bug for Boolean values (see: PR-19815 ). So this fix should be non breaking.

This PR closes #20188

…do not use valueOf factory method for BigDecimals.
@timon-sbr timon-sbr changed the title bugfix-20188: [JAVA][BUG] Do not use valueOf for numeric types for generating inner enums [JAVA][BUG] Do not use valueOf for numeric types for generating inner enums Dec 12, 2024
@wing328 wing328 added this to the 7.11.0 milestone Dec 14, 2024
@wing328
Copy link
Member

wing328 commented Dec 15, 2024

@timon-sbr thanks for the PR.

Can you please add a test case or 2 in modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml and then regenerate the samples?

@timon-sbr
Copy link
Author

@wing328 thanks for the review. I've added some test data to modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml and regenerated the samples. Can you please check, if this is enough? I'm not sure, if I've also have to add java code to test the behaviour.

@wing328
Copy link
Member

wing328 commented Dec 30, 2024

i did a test with https://raw.githubusercontent.com/OpenAPITools/openapi-generator/a8d3e7da44e268d32818e6d02d0a111d58f7bbea/modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml using the latest master CLI JAR but the output compiles without issue

is my understanding correct that the test spec (with the newly added test cases you just added) should generate output with compilation errors as described in the issue?

@timon-sbr
Copy link
Author

Hi @wing328 the Test Spec I've added tests the bugfix, so the output should generate a valid result, which is the fix of the issue.

@wing328
Copy link
Member

wing328 commented Jan 3, 2025

In the issue, you mentioned

The generated Code produces compiler errors, because it tries to create instances of BigDecimal with

but I couldn't repeat the compilation errors using the test spec you updated in this PR: https://raw.githubusercontent.com/OpenAPITools/openapi-generator/a8d3e7da44e268d32818e6d02d0a111d58f7bbea/modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml

in other words, how did you produce the compilation errors?

@timon-sbr
Copy link
Author

timon-sbr commented Jan 15, 2025

Hi @wing328 sorry for the late response, I was in vacation.
You can reproduce the issue if you run the samples generation at master with the updated spec:
https://raw.githubusercontent.com/OpenAPITools/openapi-generator/a8d3e7da44e268d32818e6d02d0a111d58f7bbea/modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml

One important note: You also have to use resteasy as library to create the error (bin/configs/java-okhttp-gson.yaml):

generatorName: java
outputDir: samples/client/petstore/java/okhttp-gson
library: okhttp-gson
inputSpec: modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml
templateDir: modules/openapi-generator/src/main/resources/Java
nameMappings:
  _type: underscoreType
  type_: typeWithUnderscore
parameterNameMappings:
  _type: underscoreType
  type_: typeWithUnderscore
additionalProperties:
  artifactId: petstore-okhttp-gson
  hideGenerationTimestamp: true
  useOneOfDiscriminatorLookup: true
  disallowAdditionalPropertiesIfNotPresent: false
  useReflectionEqualsHashCode:: true
  library: resteasy #This line is important to reproduce the error
enumNameMappings:
  s: LOWER_CASE_S
  S: UPPER_CASE_S
operationIdNameMappings:
  getArrayOfEnums: getFakeArrayofenums
  fakeHealthGet: getFakeHealth

@wing328 wing328 modified the milestones: 7.11.0, 7.12.0 Jan 20, 2025
Copy link
Contributor

@martin-mfg martin-mfg left a comment

Choose a reason for hiding this comment

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

Hi @timon-sbr, thanks for the PR!

After reviewing your changes I extended them a bit. You can find the extended version, consisting of your commits and my commits here: martin-mfg#63

I assume that you want to see a successfully completed PR to OpenAPI Generator on your Github profile. Therefore I didn't open a competing PR of our combined changes to OpenAPI Generator. Instead, you can include my changes into your branch by merging this. I will then approve your PR here and it can get merged and you get the credit. :)

What did I change on top of your changes?

  • applied your fix to more Java libraries
  • fixed a problem in your yaml changes ("double" is not a valid type, but "number" is)
  • copied your yaml changes to an additional test input file, thereby fulfilling @wing328's request discussed in the comments above
  • removed one of the yaml changes in each of the two changed yaml files, because they triggered bugs in some sample configurations. Those bugs are not related to the problem fixed in this PR.
  • merged master branch to resolve a merge conflict

@timon-sbr
Copy link
Author

Hi @martin-mfg thank you very much for the explanation and extending this bugfix to other libraries. I've merged your changes right now :)

@wing328 wing328 removed this from the 7.12.0 milestone Feb 28, 2025
devhl-labs and others added 26 commits March 13, 2025 09:30
* removed .net6 and .net7

* build docs

* build docs
* set removeEnumValuePrefix to false by default

* update tests
fix OpenAPITools#20337 (OpenAPITools#20396)

* prevent all name clashes by using a prefix for all parameters, this way they CANNOT clash with anything locally, as our hardcoded stuff in mustache files doesnt start with "p_" ,
when using the grouped option, we just use the params directly and dont unpack the variables at all, prevending furthur name clashes.

* get rid of "local_var" prefix, now that we no longer clash with paramater names

* fix a typo and remove the r# generated to the paramName when we create the identifier

* java code formatting and added a fake-endpoint parameter test

* update rust samples
… 508 (OpenAPITools#20458)

Co-authored-by: Pascal Bachor <bachorp@users.noreply.github.com>
* [C][Client] Generate CMakeLists.txt for all C sample clients

The sample client defined in bin/configs/c.yaml has CMakeLists.txt in
its .openapi-generator-ignore. It's is the only C client that doesn't
(re-)generate CMakeLists.txt, the remaining two seem fine regardless.

* [C][Client] Allow user-defined CMAKE_C_FLAGS

* [C][Client] Remove useless message() calls from CMakeLists.txt

find_package() prints success/failure messages, no need to do it twice.
…e) (OpenAPITools#20461)

* better handling of schema with just description

* update
… type string (OpenAPITools#20467)

* modules/openapi-generator/src/main/resources/go-server: alternative initialization of variable, to avoid extra checks and custom code

* propagated changes to samples
…ITools#20474)

* add test for primitive request/response

* add new files
* update spec 3.1 support message

* update faq
* add restsharp net9 samples

* add httpclient net9 samples

* add unity net9 samples

* update workflow

* comment out unity test
…APITools#20401)

* Added useCentralPackageVersionManagement option for aspnetcore

* Removed debug code from template

* Added option to opt out of centralized version management

* Added sample for centralized version management

* Removed changes for .net9

* Updated template file for model project

* Code formatting

* Corrected sample config for aspnetcore

* Regenerated Samples

* update samples, docs

* Fixed models project mustache file

* Regenerated samples and docs

* Regenerated smaples and doc

* Changed options to enum WIP

* fix option

* [ASPNETCORE] Add support for central package version management OpenAPITools#20192 (OpenAPITools#20403)

* Fixed default value for usePackageVersions

* Regenerated docs

* update workflow

* add props file

---------

Co-authored-by: Urganot <ballingsimon@googlemail.com>
… clients (OpenAPITools#20466)

* Address [rawtypes] and [this-escape] warnings in generated API clients

* Commit updated sample files
* Revert "v7.11.0 release (OpenAPITools#20508)"

This reverts commit a7240ee.

* prepare 7.12.0 snapshot version

* update samples

* update readme
* Fix enum discriminator default value

* Remove system out call

* Add case when discriminator type is ref

* Use correct schema

* Handle different use cases of mappings

* Add missing enum type Lizzy

* Make it more robust

* Add missing test for Sedan

* Refactor some code to make it cleaner

* Initialize discriminator enum field

* Don't override existing default value

* Fix issue with finding discriminators

* Move setIsEnum back to its original location

* Be smarter about figuring out the model name

* Fix final warnings

* Add javadocs to introduced methods
…penAPITools#20497)

* move validator import into GoClientCodegen

* add new example for oneof discriminator lookup

* regenerate samples

* add validator.v2 to go.mod if it is imported

* regenerate samples
* [php-nextgen] Explicitly declare nullable parameters explicitly

* Fix some deprecation warnings in other php generators

* [php-nextgen] Fix PHP 8.4 deprecation warnings with nullable/optional array parameters
- Fix enum default value generation. Default values were falling back to the default code generator and were being generated improperly for Julia.
- Add more validations to client and server API call handlers. These validations were already being generated for models, but were missed out for API calls.
* add ClickIT as bronze sponsor

* update url
@timon-sbr timon-sbr force-pushed the issue-20188-java-fix-inner-enum-template branch from c24c95c to bf0044e Compare March 13, 2025 08:32
@wing328
Copy link
Member

wing328 commented Mar 13, 2025

looks like a rebase failed and therefore this PR includes commits from others.

can you please file a new one based on the latest master?

@timon-sbr
Copy link
Author

Hi @wing328 I've created a new PR based on the current master:
#21055

I will close this PR.

@timon-sbr timon-sbr closed this Apr 8, 2025
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.

[BUG][JAVA] Inner Enum generation with BigDecimal values results in compilation error