asteasolutions / zod-to-openapi

A library that generates OpenAPI (Swagger) docs from Zod schemas
MIT License
916 stars 57 forks source link

Referencing already registered schemas #228

Closed max06 closed 5 months ago

max06 commented 5 months ago

Hello there - great project!

I have a large directory with a lot of already defined zod schemas. It would be really easy to run registry.register on all of these to build up a schema collection.

My issue: registry.registerPath requires me to reference those schemas again. If possible I'd love to use something like registry.definitions("someRegisteredSchema") instead to avoid adding a whole lot of references that basically already exist.

UseCase/Example: We're building a nuxt application with layers, where our schema definitions are in a different layer. Our user-facing api offers dynamic route handlers like /api/projects/[projectId]/[resourceType] auto-loading the matching schema for resourceType. It would be way easier to just define the response using that resourceType-String, like "it returns an array of type Server".

Or I'm thinking to complicated, not sure yet.

max06 commented 5 months ago

It's me, thinking too complicated 🙈

registry.registerPath({
  method: "get",
  path: "/projects",
  description: "Get all projects",
  summary: "Get all projects",
  responses: {
    200: {
      description: "List with Projects",
      content: {
        "application/json": {
          schema: {
            type: "array",
            items: {
              $ref: "#/components/schemas/Project",
            },
          },
        },
      },
    },
  },
  parameters: [{ $ref: "#/components/parameters/simulate" }],
});

Just in case someone else is looking for that.