question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Output [object Object] when trying to identify 'body' parameter in path's apiDoc

See original GitHub issue

I was trying to do a PUT /inventories/{id} endpoint that will accept request body as: { "inventory": 100 }

I defined the apiDoc in each path js file, and this is the one for PUT.

// paths/inventories/{id}.js
const inventoryByIdPath = (inventoriesService) => {
  const PUT = (req, res) => {
    const id = _.get(req, 'params.id');
    const data = _.get(req, 'params.data');
    ...
  };
  PUT.apiDoc = {
   ...
    parameters: [
      {
        in: 'path',
        name: 'id',
        description: 'productId',
        required: true,
        type: 'string'
      },
      {
        in: 'body',
        name: 'data',
        description: 'Inventory Data',
        required: true,
        schema: {
          $ref: '../../configs/schemas.json#/Inventory'
        }
      }
  ]
  ...
}

// schemas.json:

{
  "Inventory": {
    "properties": {
      "inventory": {
        "type": "number"
      }
    }
  }
}

However, when I call this endpoint, it will always return html error response instead of json:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Error</title>
    </head>
    <body>
        <pre>[object Object]</pre>
    </body>
</html>

and console would have logged: [object Object] after I send the request, which I didn’t log it anywhere in code. After setting the break point, I found out it didn’t even make it to the line: const PUT = (req, res) => {...}

Though after removing:

{
  in: 'body',
  name: 'data',
  description: 'Inventory Data',
  required: true,
  schema: {
    $ref: '../../configs/schemas.json#/Inventory'
  }
}

It would work normal, but it won’t have ‘body’ in req.

I tried to find more similar examples online, but only found those provided in express-openapi docs. Having no clue where would it go wrong. Any ideas? And is there anyway I could know what’s inside that mystery [object, Object]?

Using these packages:

{
    "express": "^4.16.2",
    "express-openapi": "^1.2.0"
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
alrikcommented, Jan 18, 2018

@BkunS @jsdevel I think i traced down the issue.

TLDR;: you might have no error handling middleware defined


I was able to reproduce this with the following 4 lines of code:

const express = require('express');
const app = express();

app.get('/', (req, res, next) => next({ status: 400 }) );
app.listen(9095, () => console.warn('Started'));

Launch http://localhost:9095 and see [object Object].

The same happens with express-openapi when a validation fails. The validator internally triggers next({ status : 400, ...someValidationErrorInfo }) in case of a failed validation. When you have no express error handler (err, req, res, next) => {}
or have not defined an express-openapi errorMiddleware express tries to convert the error in your case something like { status: 400, message: 'validation failed' } to a string and send it as error response.

2reactions
BkunScommented, Jan 18, 2018

@alrik @jsdevel Thanks! With the error handling middleware, I was able to know what’s inside the error now, and realized that this whole thing is just because I also didn’t use body-parser which I once thought was built in with express. This can be closed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Step 3: Parameters (API reference tutorial) | Documenting APIs
There are several types of parameters: header parameters, path parameters, and query string parameters. Request bodies are closely similar ...
Read more >
apiDoc - Inline Documentation for RESTful web APIs
Run. Creates an apiDoc of all files within dir src , using the default template and put all output to apidoc directory. Without...
Read more >
How to specify query type parameter? · Issue #545 · apidoc ...
How should we indicate query type parameters in the docs? I am think of URLs such as 'name' in the following example: http://....
Read more >
Swagger RESTful API Documentation Specification
This object includes the Data Type Fields in order to describe the type of this parameter. The type field MUST be used to...
Read more >
OpenAPI Specification v3.1.0 | Introduction, Definitions, & More
Allows for a referenced definition of this path item. The referenced structure MUST be in the form of a Path Item Object. In...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found