hapi-swagger / hapi-swagger

A Swagger interface for hapi
https://hapi.dev/
MIT License
913 stars 420 forks source link

Default response #365

Open no-more opened 8 years ago

no-more commented 8 years ago

Context

What are you trying to achieve or the steps to reproduce ?

I would like to define a default response for not 200 status code. This seems to be possible in swagger (http://swagger.io/specification/#responsesObject) but I wasn't able to define it in hapi.

Describe your issue here, include example route or JOI schemas.

response: {
        status: {
            200: ValidResponse, // Joi object -> works
            400: DefaultErrorResponse, // Joi object -> works
            default: DefaultErrorResponse, // Joi object -> doesn't work
        },
}

What result did you get?

I got an error, Invalid routeConfig options ... "default" is not allowed

What did you expect ?


        "responses": {
          "200": {
            "schema": {
              "$ref": "#/definitions/EmptyResponse",
              "type": "object"
            },
            "description": "Successful"
          },
          "400": {
            "schema": {
              "$ref": "#/definitions/BaseErrorResponse",
              "type": "object"
            },
            "description": "Bad Request"
          },
          "default": {
            "schema": {
              "$ref": "#/definitions/BaseErrorResponse",
              "type": "object"
            },
            "description": "Unauthorized"
          },

I know this might not be directly related to this plugin but I would like to know if you have some clues on how to deal with that or if I have to open a ticket on hapi ?

Thanks.

indreek commented 8 years ago

HI

Try like that:

config: { description: 'Testing.', notes: ['Testing.'], tags: ['api', 'test'], handler: handler, validate: validate, response: { schema: respSchema }, plugins: { 'hapi-swagger': { responses: { 400: { 'description': 'Bad Request' }, 401: { 'description': 'Unauthorized' }, default: { 'description': 'Default response' } } } } }

Indrek

no-more commented 8 years ago

Thanks, I'll try that, do you think it's possible to :

  1. Define it globally in hapi swagger plugin options ?
  2. Define a schema that can be used ?

Thanks

no-more commented 8 years ago

When I'm trying to registrer it at root level with glue :


            {
                plugin: {
                    register: 'hapi-swagger',
                    options: {
                        info: {
                            'title': Config.get('/server').title,
                            'version': Config.get('/server').version,
                        },
                        documentationPage: false,
                        swaggerUI: false,
                        tags: Config.get('/api').tags,
                        basePath: '/api/frontend',
                        pathPrefixSize: 3,
                        responses: {
                            default: {
                                schema: Joi.object(
                                    {
                                        test: Joi.string()
                                    }
                                ).label("testResponse")
                            }
                        }
                    }
                }
            },

I have the following swagger definition :


  "responses": {
    "default": {
      "schema": {
        "isJoi": true,
        "_type": "object",
        "_settings": null,
        "_valids": {
          "_set": []
        },
        "_invalids": {
          "_set": []
        },
        "_tests": [],
        "_refs": [],
        "_flags": {
          "label": "testResponse"
        },
        "_description": null,
        "_unit": null,
        "_notes": [],
        "_tags": [],
        "_examples": [],
        "_meta": [],
        "_inner": {
          "children": [
            {
              "key": "test",
              "schema": {
                "isJoi": true,
                "_type": "string",
                "_settings": null,
                "_valids": {
                  "_set": []
                },
                "_invalids": {
                  "_set": [
                    ""
                  ]
                },
                "_tests": [],
                "_refs": [],
                "_flags": {},
                "_description": null,
                "_unit": null,
                "_notes": [],
                "_tags": [],
                "_examples": [],
                "_meta": [],
                "_inner": {}
              }
            }
          ],
          "renames": [],
          "dependencies": [],
          "patterns": []
        }
      }
    }
  },
no-more commented 7 years ago

Hi,

Did you had time to have a look onto this please ?

Thanks a lot.

Removed-5an commented 6 years ago

I'm also looking for something like this, any update?

robmcguinness commented 3 years ago

https://swagger.io/docs/specification/2-0/describing-responses/#default