I have a situation where people consuming our API will need to partially update their resource. I understand that HTTP clearly indicates that this is a PATCH operation, although people on our side are used to send a PUT request for this and how the old code is generated.
For example, imagine a simple structure like this:
type Person struct { Name string Age int Address string }
In the POST request, I provided a payload with all three values (name, age, address) and checked them accordingly on my Golang server. Plain.
In the PUT / PATCH request, we know that, for example, a name never changes. But I will say that I would like to change age , then I would just send a JSON payload containing the new age :
PUT /person/1 {age:30}
Now to my real question: What is the best practice to prevent the use / update of name intentionally or unintentionally changed if a consumer of our API sends a JSON payload containing the name field?
Example:
PUT /person/1 {name:"New Name", age:35}
Possible solutions that I thought about, but they really don't like them:
In my validator method validator I will either forcefully remove the unnecessary name field OR respond with an error message that name not resolved.
Create a DTO object / structure that will be pretty much an extension of my Person structure and then unmarshall my JSON payload into it, like
type PersonPut struct { Age int Address string }
In my opinion, this will add extra code and logic to abstract the problem, however I do not see another elegant solution.
Honestly, I do not like these two approaches, and I would like to know if you encountered the same problem and how you solved it.
Thanks!