OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Java] Invalid code generation for anyOf types

Open Beginner friendly
#21,433 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The generator generates invalid code when anyOf includes a type: object with additionalProperties: true.

openapi-generator version
OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: Example
  version: 1.0.0
paths:
  /example:
    post:
      requestBody:
        content:
          application/json:
            schema:
              anyOf:
                - type: string
                - type: object
                  additionalProperties: true
      responses:
        '201':
          description: success
          content:
            application/json:
              schema:
                type: object
                properties:
                  id:
                    type: string
Generation Details

The above example will produce a Java class with invalid identifiers:

final TypeAdapter<Map<String, Object>> adapterMap<String, Object> = gson.getDelegateAdapter(this, TypeToken.get(Map<String, Object>.class));
Steps to reproduce
docker run --rm -v .:/local openapitools/openapi-generator-cli:latest generate -i local/openapi.yaml -g java -o /local/out/java
docker run --rm -v .:/local -w /local/out/java gradle:latest gradle build

The build will result in this failure:

Starting a Gradle Daemon (subsequent builds will be faster)

> Task :compileJava
warning: [options] source value 8 is obsolete and will be removed in a future release
warning: [options] target value 8 is obsolete and will be removed in a future release
warning: [options] To suppress warnings about obsolete options, use -Xlint:-options.
/local/out/java/src/main/java/org/openapitools/client/model/ExamplePostRequest.java:67: error: ';' expected
            final TypeAdapter<Map<String, Object>> adapterMap<String, Object> = gson.getDelegateAdapter(this, TypeToken.get(Map<String, Object>.class));
                                                             ^
/local/out/java/src/main/java/org/openapitools/client/model/ExamplePostRequest.java:67: error: illegal start of expression
            final TypeAdapter<Map<String, Object>> adapterMap<String, Object> = gson.getDelegateAdapter(this, TypeToken.get(Map<String, Object>.class));
                                                                              ^
/local/out/java/src/main/java/org/openapitools/client/model/ExamplePostRequest.java:67: error: <identifier> expected
            final TypeAdapter<Map<String, Object>> adapterMap<String, Object> = gson.getDelegateAdapter(this, TypeToken.get(Map<String, Object>.class));
                                                                                                                                                ^
/local/out/java/src/main/java/org/openapitools/client/model/ExamplePostRequest.java:67: error: <identifier> expected
            final TypeAdapter<Map<String, Object>> adapterMap<String, Object> = gson.getDelegateAdapter(this, TypeToken.get(Map<String, Object>.class));
                                                                                                                                                     ^
4 errors
3 warnings

> Task :compileJava FAILED

[Incubating] Problems report is available at: file:///local/out/java/build/reports/problems/problems-report.html

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler output below.
  /local/out/java/src/main/java/org/openapitools/client/model/ExamplePostRequest.java:67: error: illegal start of expression
              final TypeAdapter<Map<String, Object>> adapterMap<String, Object> = gson.getDelegateAdapter(this, TypeToken.get(Map<String, Object>.class));
                                                                                ^
  warning: [options] target value 8 is obsolete and will be removed in a future release
  warning: [options] source value 8 is obsolete and will be removed in a future release
  warning: [options] To suppress warnings about obsolete options, use -Xlint:-options.
  /local/out/java/src/main/java/org/openapitools/client/model/ExamplePostRequest.java:67: error: ';' expected
              final TypeAdapter<Map<String, Object>> adapterMap<String, Object> = gson.getDelegateAdapter(this, TypeToken.get(Map<String, Object>.class));
                                                               ^
  /local/out/java/src/main/java/org/openapitools/client/model/ExamplePostRequest.java:67: error: <identifier> expected
              final TypeAdapter<Map<String, Object>> adapterMap<String, Object> = gson.getDelegateAdapter(this, TypeToken.get(Map<String, Object>.class));
                                                                                                                                                  ^
  /local/out/java/src/main/java/org/openapitools/client/model/ExamplePostRequest.java:67: error: <identifier> expected
              final TypeAdapter<Map<String, Object>> adapterMap<String, Object> = gson.getDelegateAdapter(this, TypeToken.get(Map<String, Object>.class));
                                                                                                                                                       ^
  4 errors
  3 warnings

* Try:
> Check your code and dependencies to fix the compilation error(s)
> Run with --scan to get full insights.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/8.14.2/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
1 actionable task: 1 executed

BUILD FAILED in 38s
Related issues/PRs

Same bug as #18517, but for anyOf.

Suggest a fix

Apply fix from #18544 to the anyof_model.mustache template file.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the anyof_model.mustache template and compare it with the fix from related issue #18544. Reproduce the report using the supplied OpenAPI YAML and Docker commands, then run the generated project's Gradle build; done means the Java client compiles without the invalid adapter identifier.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.