asteasolutions / zod-to-openapi

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

Can't reference component.response in operation.response #274

Open artsiommiksiuk opened 4 days ago

artsiommiksiuk commented 4 days ago

Defining operation object doesn't allow refs in responses:

export const BadRequestResponseZ = registry.registerComponent("responses", "BadRequest", {
    description: "BadRequest",
    content: {
        "application/json": {
            schema: ErrorSchema,
        }
    }
});

const getUser = (): RouteConfig => ({
        summary: "Get user of an organization",
        tags,
        method: "get",
        operationId: "getOrganizationUser",
        path: "/organization/{organizationId}/users",
        request: {
            params: z.object({
                organizationId: z.string().uuid(),
            })
        },
        responses: {
            "400": { '$ref': '#/components/responses/BadRequest' },
        }
    }),

I see BadRequest registered in components.responses, but neither hardcoded, or referenced properly components allowed on RouteConfig. Not on typing level (TS error), neither in runtime (400 response simply discarded from the output).

artsiommiksiuk commented 4 days ago

My fault. I overlooked the place where I've been testing this.

artsiommiksiuk commented 4 days ago

Typing is not allowing to write something as BadRequestResponseZ.ref though.