0

In a PUT request scenario. Request with all the fields:

{
    "firstName":"first",
    "middleName":"middle",
    "lastName":"last",
    "location":"USA"
}

Request with missing field middleName when I don't want to update middleName:

{
    "firstName":"first",
    "lastName":"last",
    "location":"USA"
}

Request with field middleName set to null when I want to update middleName to null:

{
    "firstName":"first",
    "middleName":null,
    "lastName":"last",
    "location":"USA"
}

How do I identify which field has been set to null and which has not been passed in the request at all.

Timmy
  • 4,098
  • 2
  • 14
  • 34
CurssedHammer
  • 89
  • 1
  • 10
  • you just can't. – Morph21 Jul 27 '22 at 06:16
  • Found a way to check fields by using @RequestBody parameter as Map instead of the Object Class type. I was looking for a better solution where I don't have to check each field individually as the request contains 35 fields – CurssedHammer Jul 27 '22 at 06:16
  • you should ask yourself why do you want to identify if it was set or not. – Morph21 Jul 27 '22 at 06:18
  • @Morph21 Its because of the way we plan to implement PUT as explained in the question – CurssedHammer Jul 27 '22 at 09:05
  • 1
    PUT request should updat every field, so if you don't send anything it should be replaced with null. PATCH request on the other hand should update only values that were sent to you, I imagine that sending empty string there should remove value from some fields. Also sending null field vs not sending it at all is on the client side. – Morph21 Jul 27 '22 at 17:03
  • You should check this post for better explanation: https://stackoverflow.com/questions/49647682/patch-in-rest-and-null – Morph21 Jul 27 '22 at 17:08

0 Answers0