OpenAPI-Specification icon indicating copy to clipboard operation
OpenAPI-Specification copied to clipboard

Proposal: Default responses for all endpoints

Open sebastiandemel opened this issue 8 years ago • 59 comments

I suggest that we add "default responses" that can be defined as default response for All endpoints. This would avoid copy-paste for some common responses like:

429 Too Many Requests 405 Method Not Allowed

The format could be

produces:
  - application/json
responses:
  '429':
    description: to many requests in the given timeframe
    schema:
      $ref: '#/definitions/error'
paths:
...

Or if flexibility is required, we could alter the default response keyword to take a list of responsedefinitions. This could benefit with the response code range proposal (https://github.com/swagger-api/swagger-spec/issues/516).

responseDefinitions:
 "success":
    description: request successful
    code: 200
  "request limit":
    description: to many requests in the given timeframe
    code: 429, 419
    schema:
      $ref: '#/definitions/error'
  "server error":
    description: server error
    code: 500 - 599
    schema:
      $ref: '#/definitions/error'

paths
  test:
    get:
      responses:
        default:
         - "success"
         - "request limit"
         - "server error"

this would require some changes to the default response keyword.

sebastiandemel avatar Dec 05 '15 13:12 sebastiandemel

:+1: to the suggestion. As you mentioned, this would save A LOT of copy/paste.

mrname avatar Dec 31 '15 17:12 mrname

Link to meta #566

jharmn avatar Feb 16 '16 20:02 jharmn

Just a reminder, please use github reactions, rather than "+1" comments (two recent ones deleted).

MikeRalphson avatar Aug 03 '18 09:08 MikeRalphson

Is there any progress with this? The ticket is open for more than 3 years now... :(

marmax avatar Jun 12 '19 18:06 marmax

I cannot imagine why this is not fixed yet as this issue just makes impossible to specify an error for an invalid path (404) or method (405).

NoiseByNorthwest avatar Jul 18 '19 11:07 NoiseByNorthwest

you can reference responses, which is more or less what is being requested here:

paths:
  /my_endpoint:
    get:
      responses:
        404:
          $ref: "#/components/responses/notFound"
        405:
          $ref: "#/components/responses/methodNotAllowed"
  /other:
    get:
      responses:
        404:
          $ref: "#/components/responses/notFound"
        405:
          $ref: "#/components/responses/methodNotAllowed"
components:
  responses:
    notFound:
      description: "Not found response"
      content":
        application/json":
          schema:
            $ref: "#/components/schemas/notFoundResponse"
    methodNotAllowed:
      description: "Method Not Allowed response"
      content":
        application/json":
          schema:
            $ref: "#/components/schemas/methodNotAllowedResponse"

I know this is much more verbose, but at least is a workaround....

nogates avatar Jul 19 '19 01:07 nogates

+1 dream about this feature

saturov avatar Jan 16 '20 09:01 saturov

+1, will be useful

avsmirnov567 avatar Jan 16 '20 09:01 avsmirnov567

+1, amazing feature

artem-zaitsev avatar Jan 16 '20 09:01 artem-zaitsev

+1, I will be gratefull

ozh-dev avatar Jan 16 '20 10:01 ozh-dev

+1 this will save a lot of repeating docs code

alexcambose avatar Apr 15 '20 12:04 alexcambose

+1 would save a lot of time

Metroxe avatar Apr 26 '20 09:04 Metroxe

+1

matthiassb avatar Apr 29 '20 01:04 matthiassb

this will be usefull

malotho-zz avatar May 18 '20 08:05 malotho-zz

This would be incredibly useful and performance booster!

erdimeola avatar Jun 23 '20 19:06 erdimeola

its so helpfull if this feature is exist

baguse avatar Jul 29 '20 13:07 baguse

We have about 800 endpoints. Repeating the same error response references everywhere increases the JSON a lot...

Havunen avatar Oct 21 '20 06:10 Havunen

I'm also wondering how this isn't a thing yet. There's so many responses that are just part of every single endpoint by default. Things like "content-type not supported", "invalid credentials", "request entity too large", "malformed content", and more... Are we not supposed to add these to the API at all? Or are we really supposed to add these manually to every endpoint?

Also, what about responses that aren't even tied to an endpoint at all? For example, an "endpoint does not exist" response? I obviously can't add that response to any of the endpoint definitions.

Maybe some of those are not supposed to be part of the API definition at all, but I'm using RFC 7807 for my error responses and I want to give a unique response for each error so that clients can switch on the type for error handling. Currently this is a big pain to do.

Blacklands avatar Oct 21 '20 10:10 Blacklands

I'm currently looking at the possible need to add a default / override for the JSON Schema, $schema $vocabulary etc keywords in OAS 3.1

A design question comes up between simplicity and extensibility of this "default" feature.

Would something like:

openapi: 3.1.0
info:
  title: API
  version: 1.0.0
defaults:
  responses:
    '405':
     description: Method not allowed
     schema:
       $ref: '#/components/responses/error'
    '429':
      description: Too many requests
      schema:
        $ref: '#/components/responses/error'
      headers:
       ....

address the requirements for people in this issue? Bearing in mind @darrelmiller's very good advice in this tweet.

MikeRalphson avatar Oct 21 '20 11:10 MikeRalphson

+1 for this feature!

devdilson avatar Mar 12 '21 09:03 devdilson

I'm interested in that. Similar to what's been discussed so far in https://github.com/OAI/OpenAPI-Specification/issues/563

mxmlnglt avatar Jul 08 '21 20:07 mxmlnglt

+1

omgkotofey avatar Jul 13 '21 21:07 omgkotofey

@MikeRalphson With regards to the tweet: Does the spec already contain some language whether the defined responses should be exhaustive? When manually implementing an API client this information might be gathered from description texts or sources outside the openapi definition, but for code generation it is unclear, I think.

It would be really interesting to know whether people actually need to define common responses for all endpoints, or just some sensible fall-back handling for all kinds of undefined responses. If you like respond with :tada: for default handling would be enough and with :rocket: if having explicit definition for common responses would give you more.

bodograumann avatar Aug 18 '21 07:08 bodograumann

I have an in-progress update to the Overlay specification to suggest enabling this method to apply defaults to an OpenAPI description using an Overlay.

#### Default Overlays Examples

Using wildcards and the `default` action, overlays can be used to provide default behavior.

```yaml
overlay: 1.0.0
info:
  title: Provide defaults where objects don't exist
  version: 1.0.0
updates:
- target: paths.*.get.responses.400
  default:
    description: Bad request
    content:
      application/http-problem:
        schema:
          $ref: "/components/schemas/badRequestProblem"

- target: paths.*.get.responses.404
  default:
    description: Not found
    content:
      application/http-problem:
        schema:
          $ref: "/components/schemas/notFoundProblem"
- target: paths.*.get.responses.5XX
  default:
    description: Server Error
    content:
      application/http-problem:
        schema:
          $ref: "/components/schemas/serverErrorProblem"

darrelmiller avatar Aug 18 '21 13:08 darrelmiller

Would there be any way of indicating some default responses as a combination of status code + headers + body? For example 301 + Location, 400 + Content-Type: application/problem+json, 503 + Retry-After.

karenetheridge avatar Sep 09 '21 19:09 karenetheridge

Common responses with http status codes, schemas, and example, or at least common errors, would be really great to have and would allow to focus endpoint description only on specific success responses and specific, meaningful error responses.

piotr-yuxuan avatar Mar 10 '22 18:03 piotr-yuxuan

This is a must-have feature, and it's disappointing that the issue was opened back in 2015, and no lead still. Upvote!

ginosian avatar Mar 17 '22 08:03 ginosian

you can reference responses, which is more or less what is being requested here:

paths:
  /my_endpoint:
    get:
      responses:
        404:
          $ref: "#/components/responses/notFound"
        405:
          $ref: "#/components/responses/methodNotAllowed"
  /other:
    get:
      responses:
        404:
          $ref: "#/components/responses/notFound"
        405:
          $ref: "#/components/responses/methodNotAllowed"
components:
  responses:
    notFound:
      description: "Not found response"
      content":
        application/json":
          schema:
            $ref: "#/components/schemas/notFoundResponse"
    methodNotAllowed:
      description: "Method Not Allowed response"
      content":
        application/json":
          schema:
            $ref: "#/components/schemas/methodNotAllowedResponse"

I know this is much more verbose, but at least is a workaround....

This is an absolute nonsense, how a declared "GET /my_endpoint" could result in a 404 or 405, these errors only apply to the inverse set of declared endpoints, which is the infinite set of undeclared things.

NoiseByNorthwest avatar Mar 17 '22 14:03 NoiseByNorthwest

Adding a global response might make the contract definition much more "implicit" and I get that. On the other hand this feature can be handy for many specific use cases.

For example: I an developing a platform that generates server API from openAPI contract for developer which in turn will implement the contract. The web server I use performs some validations on incoming requests (vertx-web-openapi is the web server). It would be nice if I can auto add that response to all new endpoints, something some developers using this platform will almost certainly won't, because this error itself is "implicit" in the code.

I am not sure if it is a good/bad idea to have this feature but there might be a solution in between.

iekdosha avatar Mar 18 '22 17:03 iekdosha

+1 Surprised this is not implemented yet, this would save me lots of work

ronaldwanink avatar May 04 '22 08:05 ronaldwanink