swagger-api / swagger-api/swagger-codegen

[Java] Inherited properties are evicted when using oneOf.

Open
#8,442 8 comments 3 reactions 1 assignee View on GitHub

@gracekarina is already working on this.

Since Apr 22, 2020.

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

Description

Description

IF Pet object defines a petType property
AND Cat and Dog include Pet through the allOf
AND APet object includes both Cat and Dog through the oneOf
THEN property petType is missing from the object APet

Swagger-codegen version

Online version AND swagger-codegen-cli-3.0.0-20180717.153005-90.jar

Swagger declaration
openapi: 3.0.0
info:
  version: 0.0.0
  title: Example
paths:
  /randomPet:
    get:
      responses:
        '200':
          description: Random pet
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/APet'
components:
  schemas:
    APet:
      oneOf:
        - $ref: '#/components/schemas/Cat'
        - $ref: '#/components/schemas/Dog'
      discriminator:
        propertyName: petType
    Pet:
      type: object
      required:
        - petType
      properties:
        petType: { type: string }
    Dog:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          properties:
            bark: { type: boolean }
    Cat:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          properties:
            age: { type: number }
Resulting Java Bean

(with everything else stripped)

public class APet {
  private BigDecimal age = null;
  private Boolean bark = null;
}
Expected Java Bean

This part is tricky. In principle, I'd rather have just a Pet object that could be further casted into a Cat or a Dog. In this case the object APet should not even exist. But at the very minimum, the property must not be lost:

public class APet {
  private BigDecimal age = null;
  private Boolean bark = null;
  private String petType = null;
}

or

public class APet extends Pet {
  private BigDecimal age = null;
  private Boolean bark = null;
}
Command line used for generation

java -jar swagger-codegen-cli-3.0.0-20180717.153005-90.jar generate -DhideGenerationTimestamp=true -l java -i openapi.yaml

Related issue:

https://github.com/swagger-api/swagger-codegen/issues/8441

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.