baucis icon indicating copy to clipboard operation
baucis copied to clipboard

POST frequent error when passing _id

Open pjmolina opened this issue 8 years ago • 1 comments

Hi. I saw this subtle error very frequent in people using baucis. Let's say we have a baucis resource exposed on /users and let's asume it has two properties: account and city.

Doing POST to this resource is as easy as follows:

POST /users 
{"acount":"sputnik", city:"misk"}

Typical reponse will be something like:

201 Created

{"_id":"5638ceface8a340aa068d814", "acount":"sputnik", city:"misk", "__v":0}

However, I found very frequent cases where people using a DTO on the client side send something like:

POST /users 
{"_id":null, "acount":"sputnik", city:"misk"}

Nothe the null Id. The object is created. And a new _id assigned. However, the response to this today is:

200 OK

[]

Probably because on query time no _id mach null in fact.

My question here is:

  1. Should here baucis return 201 and the already created object (for a better API UX)?
  2. Should here baucis return 422 validation error: _id is not expected on resource creation (for a better API UX)?
  3. Do nothing and keep it as it is?

My vote goes for (2): manage it as a validation error. What do you think?

pjmolina avatar Feb 03 '16 17:02 pjmolina

I agree. A validation error makes sense because null is not a valid ID. Thanks for reporting this!

Sent from my iPhone

On Feb 3, 2016, at 11:39 AM, Pedro J. Molina [email protected] wrote:

Hi I saw this subtle error very frequent in people using baucis Let's say we have a baucis resource exposed on /users and let's asume it has two properties: account and city

Doing POST to this resource is as easy as follows:

POST /users {"acount":"sputnik", city:"misk"} Typical reponse will be something like:

201 Created

{"_id":"5638ceface8a340aa068d814", "acount":"sputnik", city:"misk", "__v":0} However, I found very frequent cases where people using a DTO on the client side send something like:

POST /users {"_id":null, "acount":"sputnik", city:"misk"} Nothe the null Id The object is created And a new _id assigned However, the response to this today is:

200 OK

[] Probably because on query time no _id mach null in fact

My question here is:

1 Should here baucis return 201 and the already created object (for a better API UX)? 2 Should here baucis return 422 validation error: _id is not expected on resource creation (for a better API UX)? 3 Do nothing and keep it as it is?

My vote goes for (2): manage it as a validation error What do you think?

— Reply to this email directly or view it on GitHub.

wprl avatar Feb 04 '16 05:02 wprl