laravel5-jsonapi
laravel5-jsonapi copied to clipboard
POST requiring all members
Is there a reason that POST requires all members? It seems like that's not a part of the spec. It would be nice if we could define which members are required and also some default values for members that are not provided.
A good example of this being useful is for the created_at and updated_at fields that Laravel uses by default.
I just ran into this very thing. I know I POSTed to the controller and created properly, but now it is failing with "status": 422, "code": "unprocessable_entity", "title": "Missing Attribute", "detail": "Attribute
created_atis missing.", "source": { "pointer": "/data/attributes/created_at" }
very confused.
This is a bug.
This can be fixed at https://github.com/nilportugues/serializer-eloquent-driver for sure by checking if public $timestamps = false;
field is true, and force them there.
Problem comes from the Serializer's Eloquent adapter doing all the magic for you.
Not sure if I was totally clear. I think it would be helpful to be able to specify which members are required. Maybe a getRequiredAttributes() function on the transformer would do the trick. I'd propose the following functionality:
Default: Require all attributes. If getRequiredAttributes() returns an array: Only require the specified attributes.
The database schema would be responsible for the default values of whatever is not required. Is there any reason this wouldn't be a good idea? It doesn't change existing default functionality, it just provides a way to configure the environment for something that's not in the spec.
@jeremymlane I like the idea.
As for now, added fix for $timestamps https://github.com/nilportugues/laravel5-jsonapi/releases/tag/2.4.2
@jeremymlane @nilportugues Are you working on something about this ? Indeed, I ran into a similar problem, and it could be great to have optional fields others than timestamps.
@nilportugues Submitted several pull requests for this one. I left it out of the interface so it can be backwards compatible with the transformers everyone has already implemented.
@jeremymlane Only thing missing is making sure aliased properties work... and I'll close this issue