Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
chris-doehring opened this issue Oct 29, 2021 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@chris-doehring
Copy link
Contributor

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
Copy link
Contributor Author

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:

  • getPlainAttributes(): KeyValueCollectionInterface
  • setPlainAttributes(KeyValueCollectionInterface $attributes): void

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.

@chris-doehring chris-doehring transferred this issue from dogado-group/json-api-server Jan 21, 2022
@chris-doehring chris-doehring added the enhancement New feature or request label Jan 21, 2022
@chris-doehring chris-doehring self-assigned this Mar 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant