Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How not to copy-paste 3 generic error responses in almost all paths?

I want almost all my paths to have the following 3 generic error responses. How do I describe that in Swagger without copypasting these lines everywhere?

    401:
      description: The requester is unauthorized.
      schema:
        $ref: '#/definitions/Error'
    500:
      description: "Something went wrong. It's server's fault."
      schema:
        $ref: '#/definitions/Error'
    503:
      description: Server is unavailable. Maybe there is maintenance?
      schema:
        $ref: '#/definitions/Error'

Example of how I use this in a request:

paths:
    /roles:
      get:
        summary: Roles
        description: |
          Returns all roles available for users.
        responses:
          200:
            description: An array with all roles.
            schema:
              type: array
              items:
                $ref: '#/definitions/Role'
          401:
            description: The requester is unauthorized.
            schema:
              $ref: '#/definitions/Error'
          500:
            description: "Something went wrong. It's server's fault."
            schema:
              $ref: '#/definitions/Error'
          503:
            description: Server is unavailable. Maybe there is maintenance?
            schema:
              $ref: '#/definitions/Error'
like image 386
CrabMan Avatar asked Mar 10 '16 15:03

CrabMan


People also ask

How to fix RDP copy paste not working?

If you’re still getting the RDP copy paste not working, you need to follow the steps below. Once you’ve made sure the local computer allows access to its clipboard and drives, it’s time to configure the remote computer. You’ll do that using the Policy Editor.

How to fix copy and paste not working on Windows 11?

However, if the problem persists, you can employ the following solutions to fix the copy and paste not working issue on Windows 11. 1. Close Unwanted Background Apps and Programs Third-party apps or programs running in the background can occasionally interfere with Windows processes.

Why do I get a copy error when copying files?

This usually occurs when they are accessing or making changes to a file. Sometimes, the error may appear while copying files from a previous version of Windows. But in professional and working life, it is very common and you don’t keep away yourself from using this application.

How to fix paste errors in MS Access?

In the MS Access, you have to click to the dropdown arrow on the Paste icon and select the Paste Special. You have to always keep in mind that, you can Copy & Paste a single row data at a time. Fix #4. How To Restore Lost & Deleted Access (.Accdb) File While Fixing Paste Errors?


1 Answers

Looks like I can add the following global response definition:

# An object to hold responses that can be used across operations.
# This property does not define global responses for all operations.
responses:
  NotAuthorized:
    description: The requester is unauthorized.
    schema:
      $ref: '#/definitions/Error'

However I will still need to reference it in paths like this:

401:
  $ref: '#/responses/NotAuthorized'


Same thing in OpenAPI 3.0, except it uses #/components/responses/... instead of #/responses/...:

openapi: 3.0.0

# An object to hold responses that can be used across operations.
# This property does not define global responses for all operations.
components:
  responses:
    NotAuthorized:
      description: The requester is unauthorized.
      schema:
        $ref: '#/components/schemas/Error'

# Then, in operation responses, use:
...
401:
  $ref: '#/components/responses/NotAuthorized'


There's also an open feature request in the OpenAPI Specification repository to add support for global/default responses for operations.

like image 81
CrabMan Avatar answered Oct 07 '22 00:10

CrabMan