5

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
2
  • the mapping that is the value for 'samples' does have two keys 'description'. That is not allowed in YAML. Commented Nov 16, 2016 at 19:37
  • @Anthon: It was a copy paste error while posting the question. I have updated description. Thanks. Commented Nov 16, 2016 at 19:40

1 Answer 1

2

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
  ]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.