4

I can't figure out how to implement a swagger (using a swagger-node-express project) function in node that just returns the response code

For instance for this swagger path:

/api/user/create:
x-swagger-router-controller: api
get:
  operationId: userCreate
  parameters:
    - name: username
      in: query
      description: The username of the user.
      required: true
      type: string
    - name: password
      in: query
      description: The password of the user.
      required: true
      type: string
    - name: email
      in: query
      description: The email of the user.
      required: true
      type: string
  responses:
    '200':
      description: OK
    '412':
      description: User already exists

I would like to send a return.

If I try

res.json(200);

or

res.sendStatus(200);

or even

res.sendStatus();

I get the error: Response validation failed: void does not allow a value

I get a parseerror in the swagger editor if I send

res.json()

And I get a void does not allow a value

I feel like I am out of options to try so I could really use some input.

Daan Luttik
  • 2,781
  • 2
  • 24
  • 37

2 Answers2

2

Finally figured out what was the problem for error:

Response validation failed: void does not allow a value

When you have below lines in swagger.yaml

responses:
  '200':
    description: OK

swagger does not understand what kind of response it should send. We need to explicitly specify schema of response as below to say valid response type.

responses:
  200:
    description: Success
    schema:
      title: reset
      properties:
        message:
          type: string
Tunaki
  • 132,869
  • 46
  • 340
  • 423
pavan
  • 61
  • 1
  • 8
0

I found out that when you just write a head and end the connection in the standard node way it works;

res.writeHead(code, message);
res.end();
Daan Luttik
  • 2,781
  • 2
  • 24
  • 37
  • It doesn't work for me. Still the old error: `{"message":"Response validation failed: void does not allow a value","code":"INVALID_TYPE","failedValidation":true,"path":["paths","/company/{companyName}","post","responses","200"],"originalResponse":"{}"}` – Ernst Robert Jan 04 '16 at 05:25