fastify / fastify-swagger

Swagger documentation generator for Fastify
MIT License
910 stars 200 forks source link

Support OpenAPI Webhooks #728

Closed MatanYadaev closed 11 months ago

MatanYadaev commented 1 year ago

Prerequisites

🚀 Feature Proposal

OpenAPI 3.1 has a concept of webhooks. It's similar to callbacks, but in webhooks, the API initiates the request. It's not an async response to the client's request.

Currently, Fastify Swagger doesn't support this feature.

Motivation

It's part of the OpenAPI 3.1 specification and it gives a lot of context for the API clients.

Example

Defining the schema:

await app.register(FastifySwagger, {
    openapi: {
        openapi: '3.1.0',
        info: {
            title: 'Fastify Project',
            description: 'Fastify Project',
            version: '0.0.0',
        },
        webhooks: {
            newPet: {
                post: {
                    requestBody: {
                        description: 'Pet to add to the store',
                        content: {
                            'application/json': {
                                schema: {
                                    $ref: '#/components/schemas/NewPet',
                                },
                            },
                        },
                    },
                    responses: {
                        '200': {
                            description: 'OK',
                        },
                    },
                },
            },
        },
    },
});

Should generate this OpenAPI specification:

openapi: 3.1.0
info:
  title: Fastify Project
  description: Fastify Project
  version: 0.0.0
components:
  schemas: {}
webhooks:
  newPet:
    post:
      description: Pet to add to the store
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Pet'
      responses:
        '200':
          description: OK
mcollina commented 1 year ago

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.