dogado-group / json-api-common

Common php interfaces and classes which represent the JSON:API protocol in order to build a client or server application.
https://www.dogado.de
MIT License
1 stars 0 forks source link

Nullable model properties during PATCH request - missing request body context #21

Open chris-doehring opened 3 years ago

chris-doehring commented 3 years ago

When the JSON-API server receives a PATCH request, the model converter will automatically fill in the existing payload attributes into the provided model instance. As a result, we have a blank model instance where only those properties are filled, which have been provided with values in the request body.

A PATCH request is designed to modify an existing document partially, but here comes the problem: When trying to work with the converted payload model instance further, we don't know if a property with a null value should be persisted to be null or if it just wasn't set in the request body. We loose the necessarry request payload context. Therefor we require an optional model method, which will provide the information, whether a certain property is intended to be null, because it needs to be "removed", or if it's just blank because it has not been provided. So something equivalent to the php isset function.

chris-doehring commented 2 years ago

After a very productive discussion with @stixxx2k we found a possible solution here. We could create a new Dogado\JsonApi\Support\Model\PlainAttributesInterface that requires the following structure:

The resource converter must be extended to check if the resulting model is supporting that new interface. If it does, it will put in all attributes as plain multidimensional array using the setPlainAttributes method. The getPlainAttributes method must be implemented in a way to return the set key value collection, where one can then use the has method to check the request body attribute context.

For developer comfort, there should also be a Dogado\JsonApi\Support\Model\PlainAttributesTrait that already implements that logic.