OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA][RETROFIT2] Generates unusable class for anyOf

Open
#19,932 3 comments 0 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 (example)?
  • 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?
Description

The java generator/retrofit2 library does not appear to support anyOf. It generates a valid but empty Java class, which is missing all infrastructure for the specified types.

openapi-generator version

Version 7.9.0

OpenAPI declaration file content or url
openapi: 3.0.3

info:
  title: Test
  version: 1

paths:
  /forms:
    get:
      responses:
        '200':
          description: 'Test'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Processor'

components:
  schemas:
    KnownProcessorName:
      type: string
      enum: ['PerformPostponedActionService']

    Processor:
      type: object
      properties:
        name:
          $ref: '#/components/schemas/ProcessorName'
        timestampProcessed:
          type: string
          format: date-time

    ProcessorName:
      anyOf:
        - type: string
        - $ref: '#/components/schemas/KnownProcessorName'
Generation Details
docker run --rm -v "${pwd}:/local" openapitools/openapi-generator-cli generate -i /local/test.yaml -g java -o /local/out/java --library=retrofit2 --additional-properties=annotationLibrary=none,openapiNullable=false 
Steps to reproduce
  • Save the provided yaml as test.yaml
  • Run the specified docker command
  • Observe the ProcessorName class. It compiles, but only contains some generic Java basics, no way to specify either a string or a KnownProcessorName

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 provided test.yaml and run the Docker generation command for the java generator with the retrofit2 library. Inspect the generated ProcessorName class and how the anyOf schema is handled. Done means the generated class provides usable infrastructure for either a string or KnownProcessorName, rather than an effectively empty class.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.