maxmello
04/11/2020, 3:18 PMvar
. But when a model is stored in a database and edited in a frontend, we (de-)serialize the model, which removes the usefulness of val
, because objects are newly created when deserialized (e.g. from a JSON request body). In my PUT
endpoints, I want to make sure only the fields are updated which are actually mutable, so right now I manually get the old version of the model from the database and update mutable fields one by one manually from the new instance. For me, this has 2 problems: If I only update the var fields, the client might wonder why the other fields did not update on the next request. Secondly, when the model changes, it is easy to forget updating the manual updating code.
Anybody got a better approach to this? Do you prever PUT endpoints per mutable property instead?Shawn
04/11/2020, 3:44 PMBut when a model is stored in a database and edited in a frontend, we (de-)serialize the model, which removes the usefulness ofthis is a common misconception. there are in fact libraries that will let you deserialize to classes with val properties, see, because objects are newly created when deserialized (e.g. from a JSON request body)val
jackson-datatype-kotlin
for an examplemaxmello
04/11/2020, 6:16 PMShawn
04/11/2020, 6:30 PMaraqnid
04/13/2020, 12:41 PMJohannes Zick
04/14/2020, 8:00 AMDavid Eriksson
04/15/2020, 9:00 AMEamonn
04/16/2020, 1:28 PMmaxmello
04/16/2020, 3:17 PMopen fun updateModel(newModel)
which I can call on the existing model from the database, pass the one from the API call and in there decide what is allowed to be updated. It’s still not as clean as DTOs, so maybe I will change it in the future. Also true that it is not very Kotlin specific, I should have posted it in #random or thought about it more myself before posting at all.