swagger-api / swagger-api/swagger-codegen

[JAVA] Bug array of oneOf uses concrete type instead of interface

Open
#9,914 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

reference to oneOf uses concrete type instead of interface

Swagger-codegen version

3.0.14 (can't get master to work due to jackson/yaml parsing issues)

Swagger declaration file content or url
    TypeA:
      properties:
        nameA:
          type: string
    TypeB:
      properties:
        nameB:
          type: string

    WrapperType:
      oneOf:
        - $ref: '#/components/schemas/TypeA'
        - $ref: '#/components/schemas/TypeB'

    RootType:
      properties:
        reference:
          type: array
          items:
            $ref: '#/components/schemas/WrapperType'
Command line used for generation

java -jar swagger-codegen-cli-3.0.14.jar generate -l spring -i test_swagger.yaml -o project

Steps to reproduce
  • Include the above in an OpenAPI contract such as https://gist.github.com/andrewbasterfield/774fa272d4de8da2e239acd1799bf5b5
  • generate as java/spring
  • Observe that java classes TypeA, TypeB and WrapperType all implement OneOfWrapperType but RootType.reference is List<WrapperType> not List<OneOfWrapperType>. TypeA or TypeB cannot be injected into a relationship with RootType.

if one eliminates intermediate WrapperType entirely as

    TypeA:
      properties:
        nameA:
          type: string
    TypeB:
      properties:
        nameB:
          type: string

    RootType:
      properties:
        reference:
          type: array
          items:
            oneOf:
              - $ref: '#/components/schemas/TypeA'
              - $ref: '#/components/schemas/TypeB'

Then RootType.reference degrades further to List<Object> however this is the only way we can have a relationship of TypeA or TypeB to RootType in this loosest possible sense

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

Reproduce the issue with the provided OpenAPI YAML and the swagger-codegen-cli 3.0.14 command using the spring generator. Trace Java/Spring model type resolution for array items containing oneOf, then verify that RootType.reference is generated as List and that inline oneOf handling is not reduced to List.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring
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.