krakenjs / swaggerize-express

Design-driven apis with swagger 2.0 and express.
Other
355 stars 81 forks source link

Attemping to parse body when using formData #82

Open ninemind opened 8 years ago

ninemind commented 8 years ago

When creating my swagger.yaml I have decided to use formData for creating users. So /users POST would contain the following parameters.

  parameters:
    -
      name: first_name
      in: formData
      description: First name
      required: true
      type: string
      x-example: Jane
    -
      name: last_name
      in: formData
      description: Last name
      required: true
      type: string
      x-example: Doe
    -
      name: username
      in: formData
      description: Username
      required: true
      type: string
      x-example: janedoe
    -
      name: password
      in: formData
      description: Password
      required: true
      type: string
      x-example: abc123

According to the swagger spec and all the examples I've seen this is how you're supposed to do this, however I get an "invalid json" error because it's trying to parse the body, which doesn't exist. I have gotten the in: body type to work by including body-parser in my project. Without it even the in: body type fails saying that "body is required".

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use(swaggerize({
    api: './app/docs/swagger.yml',
    docspath: '/',
    handlers: path.resolve('./app/handlers')
}));

Is body-parser causing the conflict? If so, how can I remove body parser and have both in: body and in: formData work?

tlivings commented 8 years ago

You must include any parsing middleware before swaggerize - which is only handling routing & validation, not parsing.

Does form data not work with the body parser for urlencoded? It isnt clear from the description.