Inversifyjs: @RequestBody("xxx") returns body when property is null or undefined

Created on 31 May 2017  路  8Comments  路  Source: inversify/InversifyJS

Expected Behavior

@RequestBody("title") title : string
should be returns null / undefined values when property title is null or undefined in body

Current Behavior

When I post as body a json data like that

{
  "customerId" : 234,
  "title" : null,
   "firstname" : "Alex"
}

and I'm using a property attribute like that
@RequestBody("title") title : string
title variable contains all body data instead of null.

Possible Solution

I think in server.js the function getParam in that line
return param[name] || this.checkQueryParam(paramType, param);
should be

if (paramType === "query") {
  return param[name] || undefined;
} else if (paramType === "body") {
  return param[name];
}
return param[name] || param;

or something better.

Steps to Reproduce (for bugs)

  1. Post as body a json data like that
{
  "customerId" : 234,
  "title" : null,
   "firstname" : "Alex"
}
  1. inside the controller use a property attribute like that
    @RequestBody("title") title : string

  2. title variable contains all body data instead of null.

Context

I'm simply posting data to a controller :)

Your Environment

  • Version used: inversify 4.1.1, inversify-express-utils 3.5.2
  • Environment name and version (e.g. Chrome 39, node.js 5.4): node 6.9.2, Chrome 58.0.3029.110 (64-bit)
  • Operating System and version (desktop or mobile): Windows 10 (64-bit)
  • Link to your project:

Stack trace

bug

Most helpful comment

@requestBody() does not take any arguments. It was intended to supply the body of the request as an object so that you can apply a type to it. So in your case:

interface Customer {
  customerId: number;
  title: string;
  firstName: string;
}

and the method

@post('/')
public addCustomer (@responseBody() customer: Customer): any {
  let title = customer.title;
}

All 8 comments

@requestBody() does not take any arguments. It was intended to supply the body of the request as an object so that you can apply a type to it. So in your case:

interface Customer {
  customerId: number;
  title: string;
  firstName: string;
}

and the method

@post('/')
public addCustomer (@responseBody() customer: Customer): any {
  let title = customer.title;
}

@AltekkeE can we throw or something is someone tries to use it with args @RequestBody("xxx")? Should that not be a compilation error.

I don't see why not

@massivedynamicsrls are you using TypeScript? how did you pass arguments to RequestBody without getting a compilation error?

I can confirm that typescript compilation goes without any errors.
Also, when you have @RequestBody("title") title : string it'll extract "title" parameter from the body, it "title" parameter is part of the body, but if parameter "title" doesn't exist, it'll pick up entire body.
I would say that behaviour isn't consistent. If it's designed to extract entire body, that shouldn't extract any parameter in any case.

#161 thanks!

5.2.2

The docs still state that RequestBody annotation does get an argument. Probably should be fixed.

Was this page helpful?
0 / 5 - 0 ratings