是否有可能创建差异体,并在操作过程中选择我想要的那个?

时间:2017-08-30 11:54:11

标签: swagger swagger-ui swagger-2.0 swagger-editor

例如类似的东西,但在身体部分。我可以做一些事情,让我在结构1或结构之间做出选择,如enum。这也是我的结构和结构1.我们有像选择器或每次我应该创建的东西每个结构都有新的POST或PUT?可能还有另一种方法吗?如果招摇,我们有没有?

  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: apiteam@swagger.io
  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 个答案:

答案 0 :(得分:1)

可以使用oneOf定义请求正文的备用架构,但它仅在OpenAPI 3.0中受支持,而在OpenAPI / Swagger 2.0中不受支持。

在OpenAPI / Swagger 2.0中,您可以做的最多就是使用允许任意属性的自由格式对象体:

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

在OpenAPI 3.0中,您可以像这样使用oneOf

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:
      ...