2017-08-30 68 views
1

例如類似的東西,但在身體的一部分。我可以做一些事情,讓我選擇之間的結構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: [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 
+0

我聽說過oneOf,但在swagger 2.0中不支持它 – Dima

+0

你可以添加你想用作替代品的身體示例嗎? – Helen

+0

謝謝你的回答。我補充說。 – Dima

回答

1

用於請求體替代模式可以使用oneOf來定義,但它僅在所述OpenAPI 3.0且不在所述OpenAPI /揚鞭2.0支持。

在所述OpenAPI /揚鞭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: 
     ... 
+1

是的,我想在2.0的東西,但它是不可能的,那麼我會開始使用3.0.Thanks – Dima

+0

我做到了,但它不工作。當我寫allOf它是開玩笑,但只有一個,你可以看看嗎?它不會給出任何錯誤 – Dima