Is it possible to create difference body and during the operation to select which one I want?

455 Views Asked by At

For example something like that but in body part.Can I do something which will give me choice between structure1 or structure like in enum.Here is also my structure and structure1.Do we have something like selector or every times I should create new POST or PUT for every structure?MAy be there is another way to do with if?Do we have if in swagger?

  openapi: 3.0.0
servers:
  - url: 'http://petstore.swagger.io/v2'
x-origin:
  - url: 'http://petstore.swagger.io/v2/swagger.json'
    format: swagger
    version: '2.0'
    converter:
      url: 'https://github.com/mermade/swagger2openapi'
      version: 2.2.0
info:
  description: 'This is a sample server Petstore server.  You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).  For this sample, you can use the api key `special-key` to test the authorization filters.'
  version: 1.0.0
  title: Swagger Petstore
  termsOfService: 'http://swagger.io/terms/'
  contact:
    email: [email protected]
  license:
    name: Apache 2.0
    url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
tags:
  - name: pet
    description: Everything about your Pets
    externalDocs:
      description: Find out more
      url: 'http://swagger.io'
  - name: store
    description: Access to Petstore orders
  - name: user
    description: Operations about user
    externalDocs:
      description: Find out more about our store
      url: 'http://swagger.io'
paths:
  /something:
     post:
        requestBody:
         required: true
         content:
           application/json:
             schema:
               oneOf:
                 - $ref: '#/components/schemas/Dog'
                 - $ref: '#/components/schemas/Cat'
        responses:
          '200':
            description: Updated         
components:
  schemas:
    Dog:
      type: object
      properties:
        bark:
          type: boolean
        breed:
          type: string
          enum: [Dingo, Husky, Retriever, Shepherd]
    Cat:
      type: object
      properties:
        hunts:
          type: boolean
        age:
          type: integer
1

There are 1 best solutions below

2
On

Alternate schemas for the request body can be defined using oneOf, but it's only supported in OpenAPI 3.0 and not in OpenAPI/Swagger 2.0.

In OpenAPI/Swagger 2.0, the most you can do is use a free-form object body that allows arbitrary properties:

- in: body
  name: body
  description: Add what do you wnat to add
  required: true
  schema:
    type: object

In OpenAPI 3.0, you can use oneOf like this:

paths:
  /something:
     post:
       requestBody:
         required: true
         content:
           application/json:
             schema:
               oneOf:
                 - $ref: '#/components/schemas/Structure'
                 - $ref: '#/components/schemas/Structure1'
       responses:
         ...

# "definitions" were replaced with "components.schemas"
components:
  schemas:
    Structure:
      ...
    Structure1:
      ...