[Java][okhttp-gson] Fix UUID inner enum compilation

Merged
KA
1mo ago
+121-3

Fixes compilation of UUID inner enums in Java clients generated with okhttp-gson.

Description

Generating issue_20952_inner_enum_comparison.yaml with the Java okhttp-gson client succeeded, but compiling the generated Example.java failed with three errors in its UuidEnum code:

Generated codeCompilation error
jsonWriter.value(enumeration.getValue());getValue() returns a UUID, but Gson has no value(UUID) overload.
UUID value = jsonReader.nextUUID();JsonReader has no nextUUID() method.
UUID value = jsonElement.getAsUUID();JsonElement has no getAsUUID() method.

This change adds UUID-specific handling to the inner-enum template:

jsonWriter.value(enumeration.getValue().toString());
UUID value = UUID.fromString(jsonReader.nextString());
UUID value = UUID.fromString(jsonElement.getAsString());

These compilation errors were reported during review of #24937. They were pre-existing and unrelated to that PR's changes; this PR addresses them separately.

Testing

Added JavaOkHttpGsonUuidEnumTest, reusing issue_20952_inner_enum_comparison.yaml. It compiles the complete generated UUID enum, including its Gson adapter and validation method, against Gson 2.10.1, which is added as a test-only dependency.

With useEnumCaseInsensitive both enabled and disabled, the test checks that:

  • Both declared UUID values serialize to the expected JSON strings and deserialize to the same enum constants.
  • Validation accepts both declared UUID values.
  • Deserialization and validation reject malformed UUID strings and valid UUIDs not declared in the enum.

Reproduced all three compilation errors before the fix. Regenerated the same spec using the updated template and successfully compiled the generated client with Java 17 and Gson 2.10.1:

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
  -g java --library okhttp-gson \
  -t modules/openapi-generator/src/main/resources/Java \
  -i modules/openapi-generator/src/test/resources/3_0/issue_20952_inner_enum_comparison.yaml \
  -o out/issue_20952_uuid_fixed
./mvnw -f out/issue_20952_uuid_fixed/pom.xml test

Result: BUILD SUCCESS.

Limitation

This change breaks UUID inner enums when using --type-mappings UUID=String, because the template still emits UUID.fromString(...) for the mapped String type. This PR addresses the default UUID mapping; support for overridden types is deferred to a follow-up.

PR checklist

  • Read the contribution guidelines.
  • 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 WSL)
    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.
  • 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) @KannaKim (2026/07)

Summary by cubic

Fixes compilation errors in UUID inner enums for Java clients generated with okhttp-gson by serializing UUID values as strings and parsing them with UUID.fromString(), replacing unsupported JsonWriter.value(UUID), nextUUID(), and getAsUUID() calls while preserving the enum's Java UUID type.

Verified by regenerating from issue_20952_inner_enum_comparison.yaml and successfully compiling the generated client with Java 17 and Gson 2.10.1; all three pre-existing errors reproduced before the fix.