openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[JAVA][BUG] Do not use valueOf for numeric types for generating inner enums Current Master

Open timon-sbr opened this issue 9 months ago • 6 comments

PR checklist

  • [x] Read the contribution guidelines.
  • [x] 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.
  • [x] 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.
  • [x] 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)
  • [x] 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

timon-sbr avatar Apr 08 '25 15:04 timon-sbr

Hi @martin-mfg I've created a new PR based on the current Master. Could you please check if I've cherry picked your changes correctly? Thank you :-)

timon-sbr avatar Apr 09 '25 06:04 timon-sbr

Hi @timon-sbr @martin-mfg

This PR is great but I have seen that despite the changes added in the PR, there are still compilation errors in some case related to Object.valueOf which is wrong

For example:

  • Case 1:

    omponents:
     schemas:
       MyDto:
         description: test
         type: object
         properties:
           OrderStatus:
             $ref: './destinationUrl.yml'
    

    destinationUrl.yml:

    itle: "The Destination URL Object"
    ype: object
    escription: Destination URL.
    roperties:
     taskType:
       enum:
           - PENDING
           - PROCESSING
     owner:
       type: string
    

    Generated code:

    ublic class DestinationUrlDTO implements Serializable {
     private static final long serialVersionUID = 1L;
    
     /**
      * Gets or Sets taskType
      */
     public enum TaskTypeEnum {
       PENDING(Object.valueOf("PENDING")),
    
       PROCESSING(Object.valueOf("PROCESSING"));
    
  • Case 2:

    components:
      schemas:
        MyDto:
          description: test
          type: object
          properties:
    	    OrderStatus:
    	      type: string
    	      description: Order status
    	      additionalProperties: false
    	      enum:
    	        - PENDING
    	        - PROCESSING
    

    Generated code:

    ublic class MyDtoDTO implements Serializable {
     private static final long serialVersionUID = 1L;
    
     /**
      * Order status
      */
     public enum OrderStatusEnum {
       PENDING(Object.valueOf("PENDING")),
    
       PROCESSING(Object.valueOf("PROCESSING"));
    

This problem can be solved by adding this check (isFreeFormObject) to what you have added in your PR:

    {{{name}}}({{^isUri}}{{^isNumeric}}{{^isFreeFormObject}}{{dataType}}.valueOf({{/isFreeFormObject}}{{/isNumeric}}{{/isUri}}{{{value}}}{{^isUri}}{{^isNumeric}}{{^isFreeFormObject}}){{/isFreeFormObject}}{{/isNumeric}}{{/isUri}}){{^-last}},

@timon-sbr @martin-mfg @wing328 What do you think?

jorgerod avatar Apr 15 '25 10:04 jorgerod

Hi @jorgerod, thanks for the suggestion! Could either you (in a new PR) or @timon-sbr (in the current PR) please make the suggested changes, apply it to all relevant *.mustache files, and merge the latest master? Thanks!

@timon-sbr Do you want to make them yourself?

jorgerod avatar May 06 '25 14:05 jorgerod

Hi @jorgerod, thanks for the suggestion! Could either you (in a new PR) or @timon-sbr (in the current PR) please make the suggested changes, apply it to all relevant *.mustache files, and merge the latest master? Thanks!

@timon-sbr Do you want to make them yourself?

Hi @jorgerod sorry for the late reply. I will integrate the suggested changes in this pr and regenerate the samples.

timon-sbr avatar Jun 02 '25 13:06 timon-sbr

Hi @martin-mfg and @jorgerod I've integrated the suggested changes in all relevant template files and added a String type enum to petstore API for testing. Could you please review the changes?

timon-sbr avatar Jun 02 '25 15:06 timon-sbr

please review the CI failures when you've time, e.g. https://github.com/OpenAPITools/openapi-generator/actions/runs/15417950869/job/43385774859?pr=21055

wing328 avatar Jun 03 '25 15:06 wing328

Hi I recently had the same issue and I found this PR that should solve it. But is anyone still looking at this PR?

schophil avatar Jul 07 '25 11:07 schophil