swagger-express-middleware icon indicating copy to clipboard operation
swagger-express-middleware copied to clipboard

If middleware has no data to deliver to a GET request on an endpoint, fails with a 404

Open alex-dow opened this issue 8 years ago • 5 comments

If you have an endpoint that's expected to deliver a response of some kind, and no response is given, the middleware will fail the request with a 404, implying that the endpoint itself does not exist.

I find this misleading and confusing. Perhaps a 500 (or maybe a 204??) is more appropriate with a log message explaining why.

alex-dow avatar May 06 '16 14:05 alex-dow

same issue here

I use the Swagger Pet Store API

And find out the same issue when i GET /pets/{petName}.

It shows 404 for this.

Eventually i find this open issue and realized it has no response defined in the yaml file...

This is quite confusing.Hope can give some prompt in the console log or like alex-dow said give it a 500 or some other code

whq731 avatar May 30 '16 08:05 whq731

This is by design. REST is about resources, not endpoints. If a resource doesn't exist, then the proper response code is 404, not 500 or 204. In the case of the GET /pets/{petName} endpoint mentioned above, each different {petName} represents a different resource. So if someone requests GET /pets/fido, and there isn't a pet named "Fido", then a 404 is returned because the resource /pets/fido does not exist, even though the endpoint /pets/{petName} exists.

Of course, you can choose to override this default behavior if you want, by specifying a default or example in your Swagger schema, or by writing your own middleware function to perform your own logic and return a response.

JamesMessinger avatar May 30 '16 11:05 JamesMessinger

yes, default or example works well.

whq731 avatar May 30 '16 11:05 whq731

I realize that there is some confusion with my problem.

GET /pets/{petName} should return a 404 if {petName} does not exist.

but....

GET /pets should not return a 404. It will always exist, even if the list of pets is empty.

But, if the middleware expects you to setup a data store, and you don't set one up for /pets, returning 404 is even more confusing, because this suggests that your swagger.json file is wrong.

swagger.json defines /pets, so even if you don't define a data store for it, /pets should return something other than 404. Either an empty list by default, or a 500 error if the data store is required. Either way, a lack of a data store shouldn't mean the endpoint doesn't exist ;)

alex-dow avatar Jun 01 '16 22:06 alex-dow

@alex-dow - Agreed. /pets is a collection wheras /pets/{petName} is a resource, so Swagger-Express-Middleware treats them differently. Collections never return a 404. If the collection is empty (i.e. there's no data), then an empty array is returned. This is explained in the walkthrough and in the docs

JamesMessinger avatar Jun 01 '16 22:06 JamesMessinger