0

I used swagger-codegen to build a service api in javascript. When the call works it returns my custom defined type and status 200, but when there is a validation error it returns an error but also a status 200.

For example:

    {
  "message": "Request validation failed: Parameter (model) is required",
  "code": "REQUIRED",
  "failedValidation": true,
  "path": [
    "paths",
    "/make/ford",
    "get",
    "parameters",
    "0"
  ],
  "paramName": "model"
}

I would expect it to return status code 400 as my swagger.yml only specifies my user defined type as a response. How can I make the generated code return status 400 for validation errors?

The endpoint is described like this (partial file):

 /make/ford:
    x-swagger-router-controller: controller
    get:
      description: Description
      operationId: get_car
      parameters:
        - name: model
          in: query
          description: Model
          required: true
          type: array
          items:
            type: string
      responses:
        "200":
          description: Success
          schema:
            $ref: "#/definitions/ModelResponse"
        # responses may fall through to errors
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"

definitions:
  ModelResponse:
    properties:
      options:
        type: array
        items:
          type: string
  ErrorResponse:
    required:
      - message
    properties:
      message:
        type: string   

Note: AWS API Gateway doesn't not support default responses, so I may take that out.

BeWarned
  • 2,280
  • 1
  • 15
  • 22
  • How do you define the responses in your swagger.yaml? Could you include that part? – moondaisy Apr 20 '17 at 20:16
  • I updated my question – BeWarned Apr 20 '17 at 20:45
  • The validation result (`false`) is a valid result itself. It doesn't matter what is wrong with the input - the server did produce a correct result just fine without any serverside errors. – Bergi Apr 21 '17 at 00:31
  • But swagger only allows me to define one type for a 200 status code. Swagger doesn't support one-of like JSON Schema's – BeWarned Apr 21 '17 at 00:47

0 Answers0