Skip to content

[Java][okhttp-gson][retrofit2] Avoid ambiguous references for date and time classes (#18416) - #20230

Merged
wing328 merged 1 commit into
OpenAPITools:masterfrom
jbrugge:fix_issue18416
Dec 21, 2024
Merged

[Java][okhttp-gson][retrofit2] Avoid ambiguous references for date and time classes (#18416)#20230
wing328 merged 1 commit into
OpenAPITools:masterfrom
jbrugge:fix_issue18416

Conversation

@jbrugge

@jbrugge jbrugge commented Dec 3, 2024

Copy link
Copy Markdown
Contributor

This change adds some missing guards in Mustache templates to avoid having imports of conflicting date/time classes when using the Joda library. These guards were on the methods that used the differing date/time classes, but were not on the corresponding imports. Of the various Java library templates, only those for okhttp-gson and retrofit2 had these date/time class import patterns. It looks like these guards were once there but got accidentally removed as part of #11547.

I used the following command line to reproduce the issue on master using the Petstore API spec:

~/work/openapi-generator [master] $ java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
            -g java \
            --library okhttp-gson \
            --additional-properties=dateLibrary=joda \
            -i modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml \
            -o ~/tmp/openapi-generator-master/
~/tmp/openapi-generator-master  $ mvn compile
...
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/ApiClient.java:[465,39] error: reference to DateTimeFormatter is ambiguous
  both class org.joda.time.format.DateTimeFormatter in org.joda.time.format and class java.time.format.DateTimeFormatter in java.time.format match
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/ApiClient.java:[470,40] error: reference to DateTimeFormatter is ambiguous
  both class org.joda.time.format.DateTimeFormatter in org.joda.time.format and class java.time.format.DateTimeFormatter in java.time.format match
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/JSON.java:[484,65] error: reference to LocalDate is ambiguous
  both class org.joda.time.LocalDate in org.joda.time and class java.time.LocalDate in java.time match
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/JSON.java:[522,41] error: reference to DateTimeFormatter is ambiguous
 #...

When I ran the same commands against the PR branch, the ambiguous class reference issues went away.

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.

@wing328

wing328 commented Dec 4, 2024

Copy link
Copy Markdown
Member

thanks for the PR

I did a test with the command you provided but still got errors:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project openapi-java-client: Compilation failure: Compilation failure:
[ERROR] C:\Users\wing3\tmp\openapi-generator-master\src\main\java\org\openapitools\client\model\PropertyNameCollision.java:[58,29] error: variable SERIALIZED_NAME_TYPE is already defined in class PropertyNameCollision
[ERROR] C:\Users\wing3\tmp\openapi-generator-master\src\main\java\org\openapitools\client\model\PropertyNameCollision.java:[61,17] error: variable type is already defined in class PropertyNameCollision
[ERROR] C:\Users\wing3\tmp\openapi-generator-master\src\main\java\org\openapitools\client\model\PropertyNameCollision.java:[63,29] error: variable SERIALIZED_NAME_TYPE is already defined in class PropertyNameCollision
[ERROR] C:\Users\wing3\tmp\openapi-generator-master\src\main\java\org\openapitools\client\model\PropertyNameCollision.java:[66,17] error: variable type is already defined in class PropertyNameCollision
[ERROR] C:\Users\wing3\tmp\openapi-generator-master\src\main\java\org\openapitools\client\model\PropertyNameCollision.java:[90,31] error: method type(String) is already defined in class PropertyNameCollision
[ERROR] C:\Users\wing3\tmp\openapi-generator-master\src\main\java\org\openapitools\client\model\PropertyNameCollision.java:[100,16] error: method getType() is already defined in class PropertyNameCollision
[ERROR] C:\Users\wing3\tmp\openapi-generator-master\src\main\java\org\openapitools\client\model\PropertyNameCollision.java:[104,14] error: method setType(String) is already defined in class PropertyNameCollision
[ERROR] C:\Users\wing3\tmp\openapi-generator-master\src\main\java\org\openapitools\client\model\PropertyNameCollision.java:[109,31] error: method type(String) is already defined in class PropertyNameCollision
[ERROR] C:\Users\wing3\tmp\openapi-generator-master\src\main\java\org\openapitools\client\model\PropertyNameCollision.java:[119,16] error: method getType() is already defined in class PropertyNameCollision
[ERROR] C:\Users\wing3\tmp\openapi-generator-master\src\main\java\org\openapitools\client\model\PropertyNameCollision.java:[123,14] error: method setType(String) is already defined in class PropertyNameCollision

Does it work for you locally in your environment?

@wing328

wing328 commented Dec 4, 2024

Copy link
Copy Markdown
Member

cc
@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)

@jbrugge

jbrugge commented Dec 4, 2024 via email

Copy link
Copy Markdown
Contributor Author

@wing328

wing328 commented Dec 5, 2024

Copy link
Copy Markdown
Member

can you please share the spec you used to reproduce the issue and confirmed the fix?

@jbrugge

jbrugge commented Dec 5, 2024

Copy link
Copy Markdown
Contributor Author

I had chosen one of the Petstore YAML files from the project without looking too closely; when I went back and looked for one that appeared like it was for more general testing use, I found modules/openapi-generator/src/test/resources/3_0/petstore.yaml. When I used that in the commands above then I got a clean compile of the generated Java code with the fix branch, and I got compilation errors of the generated Java code with the master branch.

Sorry for the confusion on my first attempt.

Here is what I now can run from the master branch to show the original error:

~/work/openapi-generator [master] $ java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
            -g java \
            --library okhttp-gson \
            --additional-properties=dateLibrary=joda \
            -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml \
            -o ~/tmp/openapi-generator-master/
~/tmp/openapi-generator-master  $ mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< org.openapitools:openapi-java-client >----------------
[INFO] Building openapi-java-client 1.0.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-enforcer-plugin:3.4.1:enforce (enforce-maven) @ openapi-java-client ---
[INFO] Rule 0: org.apache.maven.enforcer.rules.version.RequireMavenVersion passed
[INFO]
[INFO] --- build-helper-maven-plugin:3.5.0:add-source (add_sources) @ openapi-java-client ---
[INFO] Source directory: /Users/johnbrugge/tmp/openapi-generator-master/src/main/java added.
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ openapi-java-client ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/johnbrugge/tmp/openapi-generator-master/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ openapi-java-client ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 31 source files to /Users/johnbrugge/tmp/openapi-generator-master/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/ApiClient.java:[413,39] error: reference to DateTimeFormatter is ambiguous
  both class org.joda.time.format.DateTimeFormatter in org.joda.time.format and class java.time.format.DateTimeFormatter in java.time.format match
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/ApiClient.java:[418,40] error: reference to DateTimeFormatter is ambiguous
  both class org.joda.time.format.DateTimeFormatter in org.joda.time.format and class java.time.format.DateTimeFormatter in java.time.format match
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/JSON.java:[246,65] error: reference to LocalDate is ambiguous
  both class org.joda.time.LocalDate in org.joda.time and class java.time.LocalDate in java.time match
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/JSON.java:[284,41] error: reference to DateTimeFormatter is ambiguous
  both class org.joda.time.format.DateTimeFormatter in org.joda.time.format and class java.time.format.DateTimeFormatter in java.time.format match
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/JSON.java:[288,42] error: reference to DateTimeFormatter is ambiguous
  both class org.joda.time.format.DateTimeFormatter in org.joda.time.format and class java.time.format.DateTimeFormatter in java.time.format match
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/JSON.java:[248,16] error: reference to DateTimeFormatter is ambiguous
  both class org.joda.time.format.DateTimeFormatter in org.joda.time.format and class java.time.format.DateTimeFormatter in java.time.format match
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/JSON.java:[254,36] error: reference to DateTimeFormatter is ambiguous
  both class org.joda.time.format.DateTimeFormatter in org.joda.time.format and class java.time.format.DateTimeFormatter in java.time.format match
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/JSON.java:[258,30] error: reference to DateTimeFormatter is ambiguous
  both class org.joda.time.format.DateTimeFormatter in org.joda.time.format and class java.time.format.DateTimeFormatter in java.time.format match
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/JSON.java:[263,42] error: reference to LocalDate is ambiguous
  both class org.joda.time.LocalDate in org.joda.time and class java.time.LocalDate in java.time match
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/JSON.java:[272,15] error: reference to LocalDate is ambiguous
  both class org.joda.time.LocalDate in org.joda.time and class java.time.LocalDate in java.time match
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/JSON.java:[205,16] error: reference to DateTimeFormatter is ambiguous
  both class org.joda.time.format.DateTimeFormatter in org.joda.time.format and class java.time.format.DateTimeFormatter in java.time.format match
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/JSON.java:[213,35] error: reference to DateTimeFormatter is ambiguous
  both class org.joda.time.format.DateTimeFormatter in org.joda.time.format and class java.time.format.DateTimeFormatter in java.time.format match
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/JSON.java:[217,30] error: reference to DateTimeFormatter is ambiguous
  both class org.joda.time.format.DateTimeFormatter in org.joda.time.format and class java.time.format.DateTimeFormatter in java.time.format match
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/ApiClient.java:[49] error: a type with the same simple name is already defined by the single-type-import of LocalDate
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/ApiClient.java:[51] error: a type with the same simple name is already defined by the single-type-import of DateTimeFormatter
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/JSON.java:[40] error: a type with the same simple name is already defined by the single-type-import of LocalDate
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/JSON.java:[42] error: a type with the same simple name is already defined by the single-type-import of DateTimeFormatter
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/ApiClient.java:[734,90] error: reference to LocalDate is ambiguous
  both class org.joda.time.LocalDate in org.joda.time and class java.time.LocalDate in java.time match
[ERROR] /Users/johnbrugge/tmp/openapi-generator-master/src/main/java/org/openapitools/client/JSON.java:[99,40] error: reference to LocalDate is ambiguous
  both class org.joda.time.LocalDate in org.joda.time and class java.time.LocalDate in java.time match
[INFO] 19 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.454 s
[INFO] Finished at: 2024-12-05T07:39:02-06:00
[INFO] ------------------------------------------------------------------------
...

And here is what I can run from the PR branch to confirm the fix:

~/work/openapi-generator [fix_issue18416] $ java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
            -g java \
            --library okhttp-gson \
            --additional-properties=dateLibrary=joda \
            -i modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml \
            -o ~/tmp/openapi-generator-fix_issue18416/
~/tmp/openapi-generator-fix_issue18416  $ mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< org.openapitools:openapi-java-client >----------------
[INFO] Building openapi-java-client 1.0.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-enforcer-plugin:3.4.1:enforce (enforce-maven) @ openapi-java-client ---
[INFO] Rule 0: org.apache.maven.enforcer.rules.version.RequireMavenVersion passed
[INFO]
[INFO] --- build-helper-maven-plugin:3.5.0:add-source (add_sources) @ openapi-java-client ---
[INFO] Source directory: /Users/johnbrugge/tmp/openapi-generator-fix_issue18416/src/main/java added.
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ openapi-java-client ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/johnbrugge/tmp/openapi-generator-fix_issue18416/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ openapi-java-client ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 31 source files to /Users/johnbrugge/tmp/openapi-generator-fix_issue18416/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.883 s
[INFO] Finished at: 2024-12-05T07:35:18-06:00
[INFO] ------------------------------------------------------------------------

Is that what you need?

@wing328 wing328 added this to the 7.11.0 milestone Dec 21, 2024
@wing328

wing328 commented Dec 21, 2024

Copy link
Copy Markdown
Member

Did another round of tests and the result looks good. Likely I didn't test it correctly when I first reviewed the change.

Thanks again for your contribution.

@wing328
wing328 merged commit 4ea7f60 into OpenAPITools:master Dec 21, 2024
timon-sbr pushed a commit to timon-sbr/openapi-generator that referenced this pull request Mar 13, 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.

2 participants