laravel5-jsonapi icon indicating copy to clipboard operation
laravel5-jsonapi copied to clipboard

POST requiring all members

Open jeremymlane opened this issue 8 years ago • 8 comments

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.

jeremymlane avatar Jun 08 '16 16:06 jeremymlane

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": "Attributecreated_atis missing.", "source": { "pointer": "/data/attributes/created_at" } very confused.

climbingblake avatar Jun 09 '16 00:06 climbingblake

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.

nilportugues avatar Jun 09 '16 16:06 nilportugues

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 avatar Jun 14 '16 16:06 jeremymlane

@jeremymlane I like the idea.

nilportugues avatar Jun 14 '16 18:06 nilportugues

As for now, added fix for $timestamps https://github.com/nilportugues/laravel5-jsonapi/releases/tag/2.4.2

nilportugues avatar Jul 06 '16 22:07 nilportugues

@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.

alexjoffroy avatar Jul 16 '16 15:07 alexjoffroy

@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 avatar Aug 10 '16 19:08 jeremymlane

@jeremymlane Only thing missing is making sure aliased properties work... and I'll close this issue

nilportugues avatar Aug 12 '16 16:08 nilportugues