How to define array-of-objects as parameter?

懵懂的女人 提交于 2019-12-10 15:22:16

问题


I am quite new to Swagger, so this might be a basic question.

I am able to create .yml file for an API which takes an array of integers as parameter, as follows:

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.

This is what I send to the above API and everything works fine:

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

Now, instead of an array of integers, if I want to use some complex object as parameter, how to do it?

E.g. If this is what I want to send:

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

How should the .yml file look? I have tried something like this and it doesn't seem to work:

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

回答1:


This one seems to work, mostly:

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

Now only problem is, in the Swagger UI, it is not showing member variables and their default values. Rather is showing it as null:

{
  "samples": [
     null
  ]
}


来源:https://stackoverflow.com/questions/40640243/how-to-define-array-of-objects-as-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!