L Ja
L Ja

Reputation: 1506

Expected type object but found type string

I am having trouble with setting up my API. The error I am getting is as follows:

[
  {
    "message": "Wrong data in the response. ",
    "error": [
      {
        "code": "INVALID_TYPE",
        "params": [
          "object",
          "string"
        ],
        "message": "Expected type object but found type string",
        "path": "#/"
      }
    ],
    "content": "{\"success\":true,\"payload\":{\"userId\":47}}"
  }
]

With response headers:

connection: keep-alive  
content-length: 233  
content-type: application/json; charset=utf-8  
date: Fri, 17 Apr 2020 08:09:38 GMT  
etag: W/"e9-2OFjPp0RZp8asoi4T2vo8yXiZxE"  
x-powered-by: Express 

Now I expected this to work, as this very same code worked when using Swagger 2.0 instead of the oas-tools 3.0.1. This is my swagger file:

openapi: 3.0.1
info:
  title: Swagger test
  version: 0.0.1
servers:
- url: http://localhost:8081/
paths:
  /add-user:
    post:
      x-router-controller: 'addUser'
      description: Adds a user to the database
      operationId: add
      requestBody:
        $ref: '#/components/requestBodies/AddUser'
      responses:
        201:
          description: Successfully added the user to the database
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                  payload:
                    type: object
                    properties:
                      userId:
                        type: integer

And this is my node script in which I send the response back:

db
.query(insert_user_query, [username, password])
.then(function(result) {
    var response = {
        "success": true,
        "payload": {
            "userId": result.rows[0].user_id
        }
    }

    res.status(201).json(response)
})

Now I am completely lost, as I am building the JSON in the response variable, and I am setting response code 201 and sending it as json (which adds the application/json content-type), and yet Swagger interprets is as a string instead of an object.

Anyone here who could please point me in the right direction?

Upvotes: 2

Views: 19843

Answers (2)

arcademan
arcademan

Reputation: 21

I had the same problem and was using Express 4.x.

I could be mistaken, but I think there might be an issue with "res.json" (i.e. res.status(xxx).json({ a:b });)

I worked around the issue by using send:

res.status(201).send(response); // in your case

Upvotes: 2

jfriend00
jfriend00

Reputation: 707466

You have to take the string and parse it back into an object.

res.json() takes your object and converts it to a string using the json format. application/json is a string format and that's how you send javascript formatted objects over the network. They get converted to a canonical string format, sent over the network and then to use them as objects, they have to be parsed and converted back into real live objects.

You don't show your receiving code, but something on the receiving side needs to call JSON.parse() on the string response. That will return to you an actual object that you can use as it converts the json string back into an object on the receiving side.

Upvotes: 2

Related Questions