Skip to main content
added 75 characters in body; edited tags
Source Link
Laiv
  • 15k
  • 2
  • 34
  • 72

Context:

Suppose the current way to update a person is to use HTTP PATCH, where you can set the firstfirstname and lastnamelastname:

{ "firstname": "john", "lastname": "doe" }

{
  "firstname": "john", 
  "lastname": "doe"
}

And you want to add an etxraextra optional field birthdatebirthdate to the person. There is no problem for the GET or PUT (as itsit's optional). But can it be added to the PATCH?

{ "firstname": "john", "lastname": "doe", "birthdate": "01-01-2000" }

{
  "firstname": "john", 
  "lastname": "doe",
  "birthdate": "01-01-2000"
}

Problem:

New clients can set the field, but old clients wanting to PATCH the name, will delete the birthdate value.

Solutions that I dontdon't want

Not updating fields when the response field is missing. But what if the API needs to allow deleting specific fields (like deleting a birthdatebirthdate)?

You could argue to only delete the field when itsit's explicitly mentioned in the request. But my backend is in Java and in any case, it will map to a LocalDate null value.

{ "firstname": "john", "lastname": "doe", "birthdate": null // delete the value as its explicitly mentioned }

{
  "firstname": "john", 
  "lastname": "doe",
  "birthdate": null // delete the value as its explicitly mentioned
}

I dont want to version my API for every field that I add.

What are the solutions?

Suppose the current way to update a person is to use HTTP PATCH, where you can set the first and lastname:

{ "firstname": "john", "lastname": "doe" }

And you want to add an etxra optional field birthdate to the person. There is no problem for the GET or PUT (as its optional). But can it be added to the PATCH?

{ "firstname": "john", "lastname": "doe", "birthdate": "01-01-2000" }

Problem:

New clients can set the field, but old clients wanting to PATCH the name, will delete the birthdate value.

Solutions that I dont want

Not updating fields when the response field is missing. But what if the API needs to allow deleting specific fields (like deleting a birthdate)?

You could argue to only delete the field when its explicitly mentioned in the request. But my backend is in Java and in any case, it will map to a LocalDate null value.

{ "firstname": "john", "lastname": "doe", "birthdate": null // delete the value as its explicitly mentioned }

I dont want to version my API for every field that I add.

What are the solutions?

Context:

Suppose the current way to update a person is to use HTTP PATCH, where you can set the firstname and lastname:

{
  "firstname": "john", 
  "lastname": "doe"
}

And you want to add an extra optional field birthdate to the person. There is no problem for the GET or PUT (as it's optional). But can it be added to the PATCH?

{
  "firstname": "john", 
  "lastname": "doe",
  "birthdate": "01-01-2000"
}

Problem:

New clients can set the field, but old clients wanting to PATCH the name, will delete the birthdate value.

Solutions that I don't want

Not updating fields when the response field is missing. But what if the API needs to allow deleting specific fields (like deleting a birthdate)?

You could argue to only delete the field when it's explicitly mentioned in the request. But my backend is in Java and in any case, it will map to a LocalDate null value.

{
  "firstname": "john", 
  "lastname": "doe",
  "birthdate": null // delete the value as its explicitly mentioned
}

I dont want to version my API for every field that I add.

What are the solutions?

Source Link

backwards compatibility of REST API for update/patch when adding new fields

Suppose the current way to update a person is to use HTTP PATCH, where you can set the first and lastname:

{ "firstname": "john", "lastname": "doe" }

And you want to add an etxra optional field birthdate to the person. There is no problem for the GET or PUT (as its optional). But can it be added to the PATCH?

{ "firstname": "john", "lastname": "doe", "birthdate": "01-01-2000" }

Problem:

New clients can set the field, but old clients wanting to PATCH the name, will delete the birthdate value.

Solutions that I dont want

Not updating fields when the response field is missing. But what if the API needs to allow deleting specific fields (like deleting a birthdate)?

You could argue to only delete the field when its explicitly mentioned in the request. But my backend is in Java and in any case, it will map to a LocalDate null value.

{ "firstname": "john", "lastname": "doe", "birthdate": null // delete the value as its explicitly mentioned }

I dont want to version my API for every field that I add.

What are the solutions?