2016-03-08 95 views
2

是否可以爲原始類型定義一個swagger定義/模型?例如,請考慮以下Swagger原始類型的定義

definitions: 
    program_name: 
    type: string 
    description: Unique string id 

然而,上面的例子回來了很多錯誤(招搖預計例如properties場),並沒有例子採用任何定義,它的type: object不是。

其目的是將其用作另一個模型的子組件以及參數 - 以某種方式重用已定義的組件。

認爲這是一個招搖的typedef

編輯:根據規範,一個定義對象是一樣的架構對象http://swagger.io/specification/#schemaObject,其中指出,它可以接受基本類型。另外,假設基本類型可以存在於定義部分,那麼簡單參數類型(查詢,路徑等)是否可以引用基元定義類型?

回答

0

甲原始模式作爲在你的例子是有效的,並且可以在每當Schema對象預期被使用,例如,作爲另一模式的子組件:

definitions: 
    Program: 
    type: object 
    properties: 
     program_name: 
     $ref: "#/definitions/program_name" 

    # This becomes: 
    # type: object 
    # properties: 
    # program_name: 
    #  type: string 
    #  description: Unique string id 

或作爲操作體參數或響應架構:

paths: 
    /something: 
    post: 
     summary: POSTs and returns a string 
     parameters: 
     - in: body 
      name: program_name 
      schema: 
      $ref: "#/definitions/program_name" 
     responses: 
     "200": 
      description: OK 
      schema: 
      $ref: "#/definitions/program_name" 

然而,簡單的參數 - 路徑,查詢,報頭和形式 - 不使用schema關鍵字(它們使用type代替),並因此它們不能從definitions參考架構。

這將在下一個版本中更改,OpenAPI 3.0,其中schema將被用於all parameter types