Skip to content

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

Open
@cptaffe

Description

@cptaffe

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
# generated by Reslang v6.2.8
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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions