hapi-swagger / hapi-swagger

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

Providing examples to request params generates invalid swagger.json #295

Closed xargi closed 8 years ago

xargi commented 8 years ago

I have a route definition as follows:

{
  method: 'GET',
  path: '/hello/{name}',
  config: {
    tags: ['api'],
    description: 'Write "Hello, {name}"',
    handler: Handlers.writeHelloName,
    validate: {
      params: {
        name: Joi.string().required().example('Bob')
          .description('Name to hello to'),
      },
    },
  },
}

Hapi-swagger produces the following spec for the route:

"/hello/{name}": {
  "get": {
    "responses": {
      "default": {
        "schema": {
          "type": "string"
        },
        "description": "Successful"
      }
    },
    "parameters": [
      {
        "description": "Name to hello to",
        "in": "path",
        "example": "Bob",
        "required": true,
        "name": "name",
        "type": "string"
      }
    ],
    "tags": [
      "hello"
    ],
    "operationId": "getHelloName",
    "summary": "Write \"Hello, {name}\""
  }
}

Attempting to validate this causes a validation error: "Additional properties not allowed: example" for the parameters block. Removing the examples from the Joi-schema for the parameters causes the generated swagger.json to pass validation.

glennjones commented 8 years ago

Thanks for pointing this out fix in v6.2.0.

Its now formatted in JSON as x-example which will validate correctly.