2016-08-26 47 views
1

在以下Swagger定義中,我需要參數labelValue的類型爲LabelValueObject,以便驗證它並進行正確的反序列化。但是,我無法弄清楚語法!如何做到這一點?Swagger參數和複雜類型

swagger: "2.0" 

paths: 
    /competition: 
    post: 
     parameters: 
     - name: labelValue 
      in: formData 
      type: array 
      items: 
      type: string  ### this has to be a LabelValueObject ### 
     responses: 
     default: 
      description: Error 
      schema: 
      $ref: "#/definitions/AnyResponse" 

definitions: 
    AnyResponse: 
    properties: 
     any: 
     type: string 
    LabelValueObject: 
    properties: 
     label: 
     type: string 
     value: 
     type: string 
    required: 
     - label 
     - value 
+0

這個參數是否需要位於formData? –

+0

你是對的。我可以將它設置爲body並使用架構$ ref我想......我是Swagger的新手,所以我錯過了這一點。如果這是你的問題,請發表一個答案。 – Merott

回答

3

只有這樣,才能作爲參數是把它在所述主體(in: body),然後定義schema這個對象(內聯定義或引用一個預定義的對象與$ref)傳遞的對象。這裏有一個完整的例子:

swagger: "2.0" 

info: 
    title: A dummy title 
    version: 1.0.0 

paths: 
    /competition: 
    post: 
     parameters: 
     - name: labelValue 
      in: body 
      schema: 
      $ref: '#/definitions/LabelValueObject' 
     responses: 
     default: 
      description: Error 
      schema: 
      $ref: "#/definitions/AnyResponse" 

definitions: 
    AnyResponse: 
    properties: 
     any: 
     type: string 
    LabelValueObject: 
    properties: 
     label: 
     type: string 
     value: 
     type: string 
    required: 
     - label 
     - value