swaggest / rest

Web services with OpenAPI and JSON Schema done quick in Go
https://pkg.go.dev/github.com/swaggest/rest
MIT License
362 stars 17 forks source link

SetExpectedErrors allows only one error per status code #71

Closed marianogappa closed 2 years ago

marianogappa commented 2 years ago

An endpoint may fail for different reasons with the same status code, e.g.:

Screenshot 2022-04-14 at 18 27 30 Screenshot 2022-04-14 at 18 27 52

It would be useful to be able to document multiple reasons an endpoint request can fail with the same status code. In my use case, the client can create a "prediction" via a JSON blob, and there are a million things that can be wrong with it, but most of them will be a BadRequest.

OpenAPI v3 supports this feature via oneOf: https://stackoverflow.com/questions/36576447/swagger-specify-two-responses-with-same-code-based-on-optional-parameter

As a separate easier question, the interface for SetExpectedErrors takes a list of errors, but there are implicit requirements for those errors to actually appear on the docs, e.g. a .Status() int function. Would you be able to clarify what exactly do I need to implement to make this work? Cheers.

vearutop commented 2 years ago

Hi, I just experimented a bit with oneOf (and less restrictive anyOf) and found out it is not so straightforward and easy to use, I'll need to improve implementation to make is simpler.

As for status indicators and other error flavors, they are defined in https://github.com/swaggest/rest/blob/v0.2.26/error.go#L10-L32 and used in https://github.com/swaggest/rest/blob/v0.2.26/error.go#L45-L72. I need to document them to make more discoverable, but I haven't found a good place for such docs so far. SetExpectedErrors might be a good host for these docs, but it resides in a different module, independent of any HTTP concerns.

vearutop commented 2 years ago

Please check if #73 (merged and tagged with v0.2.27) fits your needs.