fastify / fastify-swagger

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

Transform hook for modifying full documentation #736

Closed samchungy closed 1 year ago

samchungy commented 1 year ago

Prerequisites

🚀 Feature Proposal

Add another transform (name TBC maybe transformDocument?) hook which exposes the generated documentation before it is published to allow for modification

Motivation

I'm trying to create a plugin which brings together my library https://github.com/samchungy/zod-openapi for use with this.

However, my library allows for dynamic registration of OpenAPI components via the schemas.

eg. you declare z.string().openapi({ref: 'jobId')} and it will automatically insert {jobId: { type: 'string' }} into the components and leave a $ref behind.

It also allows users to add Zod Objects directly to the components section and we will replace them with converted objects.

This presents a little bit of an issue because it doesn't seem like I can modify the components during the transform hook you currently expose, nor would I be able to allow for the same behaviour of inserting ZodObjects into the components.

So, if you would accept a PR for this, could I expose the full documentation object before it's served so third parties could modify documentation accordingly?

Example

const transformDocument = (document) => {
  // do whatever you want with the document
  document.components = {};
  return document;
}
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.