-
-
Notifications
You must be signed in to change notification settings - Fork 367
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
ActiveRecord ignore nullable types with null value (on update) #485
Comments
Can you give a reproducible test case? |
Cannot reproduce. However, we created a specific test for this issue, just in case ( |
I was about to open an issue about this topic but I found this one, so I'm re-opening it. Tested in latest release 3.2.1 as well as current main branch. Bug: Given an entity with a field of type "Nullable" (I've tried with NullableInt32 and NullableString), if a PUT request is done with a JSON body, the value in the entity is kept as it was in the database, ignoring the new null value. I'm using the entity mapping it straight away to ActiveRecordMappingRegistry. Example: entity customer Initial value
now I try to update address and countryId to null values through a PUT request.
In this case, the values are kept as the previous ones and the new null values are ignored. I've spent a few hours trying to track down the problem myself, but I'm not 100% confident about where the problem is, although I agree with @sf-spb because I ended up in the same method: File: Method: Line:
When the serializer is parsing the JsonObject, the AName variable has the right value of 'countryId' or 'address', but the objectValue always returns "nil", which makes avoiding the if clause all together. If this info is not enough to replicate, I can try to provide a project with a practical example of the bug. |
Please, provide a small and self contained example. So that we can put it in the test cases too. |
Demo project: activerecord_restful_crud_null_bug.zip I've tweaked a bit the sample activerecord_restful_crud included in the library to adapt it to this scenario. To reproduce the bug these steps need to be followed: 1º Open the project and connect to your database of choice. I've used the firebird activerecord.fdb bd provided with the samples in the folder "samples\data". Verify in the FDConnectionConfigU.pas unit that the connection params are correct. 2º Compile and execute the project. 3º Using postman or rest debugger send the GET request:
It should return:
4º Send a PUT request to the same endpoint with the JSON body:
5º Send a GET request again and you'll see that none of the fields have been updated. I added as well the entity object to the log in the event AfterUpdate, which you'll see hasn't updated any of the values. You can see in the EntitiesU.pas unit that all the fields but the ID are defined as NullableString or NullableInt16. The fields in the table CUSTOMERS in the database aren't marked as NOT NULL apart from the ID, so this scenario should have worked. I hope this helps to reproduce the problem. |
Please, let me know if this commit fixes your problem |
Fixed, all tests pass |
Tested this fix and it works as expected |
Solution (MVCFramework.Serializer.JsonDataObjects.pas, method TMVCJsonDataObjectsSerializer.JsonDataValueToAttribute).
`jdtObject:
`
The text was updated successfully, but these errors were encountered: