簡體   English   中英

如何定義對象數組作為參數?

[英]How to define array-of-objects as parameter?

我對Swagger很新,所以這可能是一個基本問題。

我能夠為一個API創建.yml文件,它接受一個整數數組作為參數,如下所示:

Add samples
---
tags:
 - MY API
parameters:
 - name: my_id
   in: path
   type: integer
   required: true
   description: Some des
 - name: body
   in: body
   schema:
     id: add_samples
     required:
       - sample_ids
     properties:
       sample_ids:
         type: array
         items:
            type: integer
         description: A list of sample ids to be added
responses:
   '200':
     description: Added samples.
   '400':
     description: Error adding samples.

這是我發送到上面的API,一切正常:

{"sample_ids": [475690,475689,475688]}

現在,如果我想使用一些復雜的對象作為參數,而不是整數數組,該怎么做?

例如,如果這是我要發送的內容:

{"sample_ids": [{
    "sample_id": "7",
    "some_prop": "123"
},
{
    "sample_id": "17",
    "some_prop": "134"
}]}

.yml文件應該怎么樣? 我嘗試過這樣的東西,它似乎不起作用:

Add sample
---
tags:
 - Samples API
models:
  Sample:
    id: Sample
    properties:
      sample_id:
        type: string
        default: ""
        description: The id for this sample
      some_prop:
        type: integer
        description: Some prop this sample
parameters:
 - name: body
   in: body
   schema:
     id: add_sample
     required:
       - sample_ids
     properties:
       samples:
         type: array
         description: A list of samples to be added
         items:
           $ref: Sample
responses:
   '201':
     description: Created a new sample with the provided parameters
   '400':
     description: SOME ERROR CODE

這個似乎工作,主要是:

Add sample
---
tags:
 - Samples API
models:
  Sample:
    id: Sample
    properties:
      sample_id:
        type: string
        default: ""
        description: The id for this sample
      some_prop:
        type: integer
        description: Some prop this sample
parameters:
 - name: body
   in: body
   schema:
     id: add_sample
     required:
       - sample_ids
     properties:
       samples:
         type: array
         description: A list of samples to be added
         items:
           $ref: Sample
responses:
   '201':
     description: Created a new sample with the provided parameters
   '400':
     description: SOME ERROR CODE

現在唯一的問題是,在Swagger UI中,它沒有顯示成員變量及其默認值。 而是將其顯示為null:

{
  "samples": [
     null
  ]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM