2

I am having some issues with swagger: I have an array of objects (address) described in this way in the .yaml file:

Address:
  properties:
    street:
      type: string
    city:
      type: string
    state:
      type: string
    country:
      type: string

and this is the other yaml file with the definitions of the API (address is a params):

 - name: addresses
   in: formData
   description: List of adresses of the user. The first one is the default one.
   type: array
   items:
     $ref: '#/definitions/Address'

And this is the text I put in the swagger UI:

[
  {
    "street": "Bond street",
    "city": "Torino",
    "state": "Italy",
    "country": "Italy"
  }
]

but in node.js, if I print what I receive:

{"addresses":["["," {"," \"street\": \"Bond street\","," \"city\": \"Torino\","," \"state\": \"Italy\","," \"country\": \"Italy\""," }","]"]}

And I get a parsing error... There are some extra [ and ". It seems that swagger parse it as string (?)

3
  • 1
    Is there any significance in your usage of the .yaml file (with dot) and the other yaml file (without dot)? Commented May 1, 2017 at 4:05
  • Please post your complete .yaml files. Are they in the same folder? What code do you use to print the data on your Node.js side? Commented May 1, 2017 at 18:19
  • No, the yaml files are not in the same folders, but I don't think this is the problem since the descriptions in swagger ui work. To print the data I use JSON.stringify Commented May 3, 2017 at 12:55

1 Answer 1

8

To send JSON data, you need to use use an in: body parameter (not in: formData) and specify that the operation consumes application/json. formData parameters are used for operations that consume application/x-www-form-urlencoded or multipart/form-data.

paths:
  /something:
     post:
       consumes:
         - application/json
       parameters:
         - in: body
           name: addresses
           required: true
           schema:
             type: array
             items:
               $ref: "#/definitions/Address"  # if "Address" is in the same file
               # or
               # $ref: "anotherfile.yaml#/definitions/Address"  # if "Address" is in another file
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for all the suggestions. But something is not working anyway. I changed the in:body but still
Define "is not working". Does Swagger UI render this incorrectly? Does you backend receive incorrect data? The more details the better.
yes, sorry for the short answer yesterday. Still receiving wrong data in my BE.
Hello, In the end I was able to fix it. I didn't notice the change in schema. Thank you.

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.