postlight / lux

Build scalable, Node.js-powered REST JSON APIs with almost no code.
https://lux.postlight.com
MIT License
571 stars 60 forks source link

Controller params default to empty array, not to serializer attributes #745

Open CodingItWrong opened 5 years ago

CodingItWrong commented 5 years ago

The guides for Controller#params seem to indicate that it is optional:

If you do not override this property all of the attributes specified in the Serializer that represents a Controller's resource. If the Serializer cannot be resolved, this property will default to an empty array.

However, this defaulting doesn't seem to be working for me.

(Demo repo here: https://github.com/CodingItWrong/lux-test)

When I have the following Serializer and Controller with explicit params, I can successfully create a record with values passed in:

class RestaurantsController extends Controller {
  params = ['name', 'address'];
}
class RestaurantsSerializer extends Serializer {
  attributes = ['name', 'address'];
}

But if I remove Controller#params:

class RestaurantsController extends Controller {
}

Then upon POSTing a record I get empty attributes back:

POST:

{
  "data": {
    "type": "restaurants",
    "attributes": {
      "name": "Sushi Place",
      "address": "123 Main Street"
    }
  }
}

Response:

{
    "data": {
        "id": "3",
        "type": "restaurants",
        "attributes": {},
        "relationships": {
            "dishes": {
                "data": []
            }
        }
    },
    "links": {
        "self": "http://localhost:4000/restaurants"
    },
    "jsonapi": {
        "version": "1.0"
    }
}

And if I GET /restaurants/ I get null values for the attributes:

{
  "data": [
    {
      "id": "1",
      "type": "restaurants",
      "attributes": {
        "name": null,
        "address": null
      },
...
    }
  ],
...

}