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

fix #17419 useOneOfInterfaces option for java clients & servers

Open Nicklas2751 opened this issue 11 months ago • 7 comments

Some java generators generated wrong, not compiling code, when using the option useOneOfInterfaces. The bug was caused by old/wrong model.mustachefiles without the part supporting the useOneOfInterfaces option. These clients were affected:

  • jersey2
  • jersey3
  • microprofile
  • native
  • okhttp-gson

as well as the following servers:

  • helidon
  • jaxrs
  • mirconaut
  • microprofile
  • play
  • pkmst

This PR fixes all of them 😎

It also fixes a compile error of the java native client generator when using anyOf with generic types.

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.

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

Nicklas2751 avatar Mar 08 '25 21:03 Nicklas2751

@wing328 Seems like the others don't have any feedback on this 😅

Nicklas2751 avatar Mar 19 '25 20:03 Nicklas2751

@martin-mfg org.openapitools.client.model.TestResponse.CustomTypeAdapterFactory shouldn't exists and org.openapitools.client.model.TestResponse should be an interface.

I added another test to JavaClientCodegenTest with the OpenAPI schema you named and it worked. But when I use the CLI it doesn't work like you said. I added some more test but couldn't recreate the problem. But then I saw that I missed to regenerate the CLI Jar so I cleared the target folder and ran ./mvnw clean package again. Now the CLI works correctly too.

This is how I started the CLI:

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
  -i https://github.com/OpenAPITools/openapi-generator/raw/fef84d956e23dad2cb6ef49b0519b7427a13857a/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/oneOf.yaml \
  -g java \
  --library okhttp-gson \
  --additional-properties useOneOfInterfaces=true \
  -o test/okhttp-gson

tl;dr I had to regenerate the CLI then it generated the Interface as expected and without any compile errors.

Nicklas2751 avatar Mar 29 '25 14:03 Nicklas2751

@martin-mfg The second problem was in the doc generation of jersey2, jersey3 and native. I fixed it & added an test for it :)

Nicklas2751 avatar Mar 29 '25 15:03 Nicklas2751

tl;dr I had to regenerate the CLI then it generated the Interface as expected and without any compile errors.

It seems we get different results, or maybe we're talking about different things. So I extended a unit test to show the problem I get - please see https://github.com/martin-mfg/openapi-generator/pull/77/commits/554bb00927331a2e30520917a1bece31b9d93a2b. The test fails, as seen in this pipeline.

As you said, org.openapitools.client.model.TestResponse.CustomTypeAdapterFactory indeed doesn't exist. But still it is being referenced from src/main/java/org/openapitools/client/JSON.java.

martin-mfg avatar Mar 30 '25 08:03 martin-mfg

@martin-mfg Ah, I see! Added another test for it & fixed it :)

Nicklas2751 avatar Mar 30 '25 17:03 Nicklas2751

@martin-mfg ping 🔔 :)

Nicklas2751 avatar Apr 25 '25 16:04 Nicklas2751

I got the newest changes from master branch and cleaned up the commit history of this branch.

@martin-mfg Is there anything I can do to make it easier for you to review this?

Nicklas2751 avatar May 25 '25 12:05 Nicklas2751