Home > database >  How to determine if a property in PATCH request was explicitly set to null
How to determine if a property in PATCH request was explicitly set to null

Time:10-19

I have a PATCH endpoint in controller with method taking EditGroupApiCommand as parameter:

enter image description here

In Swagger you can provide a JSON object with those parameters as such: enter image description here

Regardless if the Manager property has been included in JSON and set to null or it was omitted, In the controller I am receiving an empty guid.

I want to distinguish two cases:

  • Manager guid has been omitted (then I skip any validation/actions)
  • Manager guid has been explicitly set to null (I remove the managers)

CodePudding user response:

Your Manager property is a Guid which means it can not be null. When the value is missing in the incoming data it will result in default(Guid), same happens if it is set to null.

If you can change the EditGroupApiCommand, change the public Guid Manager to public Guid? Manager. If you want to delete the Manager => set the Manager to Guid.Empty:

{
   "Manager": "00000000-0000-0000-0000-000000000000"
}

If it was ommited => it is null.

So you have to explicitly set it to Guid.Empty to distinguish thge use cases.

  • Related