Forum Discussion

Princezna's avatar
Princezna
New Member
3 years ago

OAS2 | How to use allOf to combine properties (one model w/ different required elements in array)

Hi, can someone please confirm usage allOf for defining array items by $ref + required elements (see sample in response part)?

 

 

swagger: '2.0'
info:
  description: 'Sample for allOf and combine $ref w/ required attrs'
  version: '001'
  title: MEMBERS
  termsOfService: http://swagger.io/terms/
basePath: /MEMBERS
paths:
  /:
    post:
      produces:
        - application/json
      parameters:
        - in: body
          name: Input
          required: false
          schema:
            type: object
            properties:
              relations:
                type: array
                items:
                  $ref: '#/definitions/relations'
      responses:
        '200':
          description: ''
          schema:
            type: object
            properties:
              relations:
                type: array
                items:
                  allOf:
                    - $ref: '#/definitions/relations'
                    - required:
                        - rel_key
                        - prim_member_key
                        - scnd_member_key
                        - rel_type
definitions:
  relations:
    type: object
    properties:
      rel_key:
        type: integer
        format: int64
        example: 123
      prim_member_key:
        type: integer
        format: int64
        example: 1234
      rel_type:
        type: string
        enum:
          - is_parent
          - is_sibling
          - is_child
        example: is_sibling
      scnd_member_key:
        type: integer
        format: int64
        example: 12345
      communal_household_flag:
        type: boolean
        example: true
      broken_relation_flag:
        type: boolean
        example: false

 

 

or how to redefine it using by one relations model in OAS2? (in OAS3 this works see https://blog.stoplight.io/reuse-openapi-descriptions)

But issue is when this sample is converted from .json to .yaml format by swagger-codegen-cli-2

 

 

{
  "swagger": "2.0",
  "info": {
    "description": "Sample for allOf and combine $ref w/ required attrs",
    "version": "001",
    "title": "MEMBERS",
    "termsOfService": "http://swagger.io/terms/"
  },
  "basePath": "/MEMBERS",
  "paths": {
    "/": {
      "post": {
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "in": "body",
            "name": "Input",
            "required": false,
            "schema": {
              "type": "object",
              "properties": {
                "relations": {
                  "type": "array",
                  "items": {
                    "$ref": "#/definitions/relations"
                  }
                }
              }
            }
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "schema": {
              "type": "object",
              "properties": {
                "relations": {
                  "type": "array",
                  "items": {
                    "allOf": [
                      {
                        "$ref": "#/definitions/relations"
                      },
                      {
                        "required": [
                          "rel_key",
                          "prim_member_key",
                          "scnd_member_key",
                          "rel_type"
                        ]
                      }
                    ]
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "definitions": {
    "relations": {
      "type": "object",
      "properties": {
        "rel_key": {
          "type": "integer",
          "format": "int64",
          "example": 123
        },
        "prim_member_key": {
          "type": "integer",
          "format": "int64",
          "example": 1234
        },
        "rel_type": {
          "type": "string",
          "enum": [
            "is_parent",
            "is_sibling",
            "is_child"
          ],
          "example": "is_sibling"
        },
        "scnd_member_key": {
          "type": "integer",
          "format": "int64",
          "example": 12345
        },
        "communal_household_flag": {
          "type": "boolean",
          "example": true
        },
        "broken_relation_flag": {
          "type": "boolean",
          "example": false
        }
      }
    }
  }
}

 

 

 

 

swagger: '2.0'
info:
  description: 'Sample for allOf and combine $ref w/ required attrs'
  version: '001'
  title: MEMBERS
  termsOfService: http://swagger.io/terms/
basePath: /MEMBERS
paths:
  /:
    post:
      produces:
        - application/json
      parameters:
        - in: body
          name: Input
          required: false
          schema:
            $ref: '#/definitions/Input'
      responses:
        '200':
          description: ''
          schema:
            $ref: '#/definitions/inline_response'
definitions:
  relations:
    type: object
    properties:
      rel_key:
        type: integer
        format: int64
        example: 123
      prim_member_key:
        type: integer
        format: int64
        example: 1234
      rel_type:
        type: string
        enum:
          - is_parent
          - is_sibling
          - is_child
        example: is_sibling
      scnd_member_key:
        type: integer
        format: int64
        example: 12345
      communal_household_flag:
        type: boolean
        example: true
      broken_relation_flag:
        type: boolean
        example: false
  Input:
    type: object
    properties:
      relations:
        type: array
        items:
          $ref: '#/definitions/relations'
  inline_response:
    type: object
    properties:
      relations:
        type: array
        items:
          type: object
          allOf:
            - $ref: '#/definitions/relations'
            - {}

 

 

where is missing definition for mandatory output field elements (in inline_response model) replaced by emptyObject.

No RepliesBe the first to reply