Essencium Backend is a software library built on top of Spring Boot that allows developers to quickly get started on new software projects. Essencium provides, for example, a fully implemented role-rights concept as well as various field-tested solutions for access management and authentication.
GNU Lesser General Public License v3.0
15
stars
3
forks
source link
Patch preprocessing implementation not useful #105
The current implementation of PATCH iterates over all map entries and sets their value on the target object (database object, NOT DTO), e.g. User. For primitive types this is ok. But for complex objects it is not.
Example:
I want to set an image of type UploadedFile for my AppUser (subclass of User), which I reference by its id (Long). If I now send PATCH /v1/users/25 with { "image": 324 }, it would try to place a Long in the place of an UploadedFile via reflection.
Either this functionality should be fundamentally rebuilt or consider applying the patches from the Map Entries to the associated DTO rather than the Model.
A "workaround" is to override the patchPreProcessing() method in the corresponding concrete server and replace the primitive Map Values with complex objects.
Use-Case
Implementation proposals
If we apply the patches to the Input DTO we could use convertInputDtoToEntity. But then it would make sense to add a parameter id to this method, so that you can load the existing entity from the DB there and then change it.
Or for PUT requests we also use the DTOs as input and then add the id. For POST requests the id is null and the convertInputDtoToEntity method decides if the entity is taken from the DB or created.
Suggestion for improvement
The current implementation of PATCH iterates over all map entries and sets their value on the target object (database object, NOT DTO), e.g. User. For primitive types this is ok. But for complex objects it is not.
Example: I want to set an image of type UploadedFile for my AppUser (subclass of User), which I reference by its id (Long). If I now send PATCH /v1/users/25 with { "image": 324 }, it would try to place a Long in the place of an UploadedFile via reflection.
Either this functionality should be fundamentally rebuilt or consider applying the patches from the Map Entries to the associated DTO rather than the Model. A "workaround" is to override the patchPreProcessing() method in the corresponding concrete server and replace the primitive Map Values with complex objects.
Use-Case
Implementation proposals
Additional Information