Selva
Selva

Reputation: 1670

All Of is not working properly in Open API Swagger 3.0

In my project I am using Open API 3.0 to for generating mmodel classes.

My requirement is to I have to reuse the parent class attribute to sub class.

Ex:

public class Student {
  private String id;
  private String name;
}

public class Address {
  private String id;
  private String name;
  private String city;
  private String state;
}

But the problem is allOf generating the bean class like below instead of above one.

public class Address {
  public Student student;
  private String city;
  private String state;
}

Below is my schema.yaml

Student:
    properties :
    id:
      type: integer
      format: int64
      description: The ID of the new account
    name:
      type: string
      description: The human-readable description of this account
    

Address:
    properties : 
    allOf:
        $ref : '#/Student'
    city:
       type: string
      description: City
    state:
      type: string
      description: State

  

How to make sure the properties are duplicating instead of creating Object.

Any help will be greatly appreciated!!!

Upvotes: 1

Views: 4606

Answers (1)

Jack
Jack

Reputation: 3059

You can define separate sections under the allOf element. First section is the reference to your parent object. Second section is an object with the additional properties.

The following is correctly parsed in https://editor.swagger.io :

openapi: 3.0.1
info:
  title: "example"
  version: "1.0"
paths:
  /example:
    post:
      responses:
        "200":
          description: example
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Address'
components:
  schemas:
    Student:
        properties:
          id:
            type: integer
            format: int64
            description: The ID of the new account
          name:
            type: string
            description: The human-readable description of this account
        
    Address:
      allOf:
      - $ref : '#/components/schemas/Student'
      - type: object
        properties:
          city:
             type: string
             description: City
          state:
            type: string
            description: State

Upvotes: 2

Related Questions