OpenAPITools / OpenAPITools/openapi-generator

[BUG][java] oneOf in "anonymous" schemas: model not generated.

Open
#5,903 16 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Java Inline Schema Handling 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)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

As mentioned in #5706, if the openapi spec file defines a oneOf schema in responses "as is", the generator references a model in the DefaultApi but the model doesn't get generated. See examples below.
edit: the same issue happens to all the places where oneOf is used without being directly under a named schema (e.g. responses, array items etc.). See the comments.

openapi-generator version

latest master: cc09118ffc2f13ca5d251049dc3d7552f08b670a

OpenAPI declaration file content or url

OpenApi spec file:

openapi: 3.0.3
info:
  title: oneOf test
  version: '1.0'
paths:
  /state:
    get:
      operationId: getState
      responses:
        '200':
          description: get states
          content:
            application/json:
              schema: # doesn't work
                oneOf:
                  - $ref: '#/components/schemas/ObjA'
                  - $ref: '#/components/schemas/ObjB'
                discriminator:
                  propertyName: realtype
  /state_ref:
    get:
      operationId: getStateRef
      responses:
        '200':
          description: 'get states using $ref'
          content:
            application/json:
              schema: # logically same as above but works
                $ref: '#/components/schemas/ObjAOrObjB'
components:
  schemas:
    ObjA:
      type: object
      properties:
        realtype:
          type: string
        message:
          type: string
    ObjB:
      type: object
      properties:
        realtype:
          type: string
        code:
          type: integer
    ObjAOrObjB:
      oneOf:
        - $ref: '#/components/schemas/ObjA'
        - $ref: '#/components/schemas/ObjB'
      discriminator:
        propertyName: realtype
Command line used for generation

openapi-generator generate -i spec.yaml -g java --library native -o ./out/java_oneof

Steps to reproduce
  • use spec file above
  • run command above
  • notice how src/main/java/org/openapitools/client/api/DefaultApi.java features
    import org.openapitools.client.model.GetState200;
    import org.openapitools.client.model.ObjAOrObjB;
    
    but the GetState200 model is not present in src/main/java/org/openapitools/client/model while ObjAOrObjB is.

@bbdouglas @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger @karismann @Zomzog @lwlee2608 @bkabrda

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

Run the supplied OpenAPI YAML with openapi-generator generate -i spec.yaml -g java --library native -o ./out/java_oneof. Compare src/main/java/org/openapitools/client/api/DefaultApi.java with src/main/java/org/openapitools/client/model, focusing on the imported GetState200 and the generated ObjAOrObjB; done means the referenced response model is present and the generated client is consistent.

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
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.