This repository was archived by the owner on Nov 19, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 196
This repository was archived by the owner on Nov 19, 2024. It is now read-only.
Support for recursive types #459
Copy link
Copy link
Open
Description
For now trpc-openapi does not support recursive types because of this: https://github.com/jlalmes/trpc-openapi/blob/master/src/generator/schema.ts#L19
But now zod-to-json-schema fixed that so we can delete refStrategy: "none".
Repro:
import { initTRPC } from "@trpc/server";
import type { OpenApiMeta } from "trpc-openapi";
import { generateOpenApiDocument } from "trpc-openapi";
import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";
const baseCategorySchema = z.object({
name: z.string(),
});
type Category = z.infer<typeof baseCategorySchema> & {
subcategories: Category[];
};
const categorySchema: z.ZodType<Category> = baseCategorySchema.extend({
subcategories: z.lazy(() => categorySchema.array()),
});
// here zod-to-json-schema handles circular reference
console.dir(zodToJsonSchema(categorySchema), {
depth: null,
});
const t = initTRPC.meta<OpenApiMeta>().create();
const appRouter = t.router({
sayHello: t.procedure
.meta({ openapi: { method: "POST", path: "/say-hello" } })
.input(categorySchema)
.output(z.object({ greeting: z.string() }))
.mutation(({ input }) => ({ greeting: `Hello ${input.name}!` })),
});
export const openApiDocument = generateOpenApiDocument(appRouter, {
title: "tRPC OpenAPI",
version: "1.0.0",
baseUrl: "http://localhost:3000",
});
// circular reference is not handled correctly, Defaults to any
console.dir(openApiDocument, { depth: null });Metadata
Metadata
Assignees
Labels
No labels