Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Support for recursive types #459

Open
valerii15298 opened this issue Oct 12, 2024 · 0 comments
Open

Support for recursive types #459

valerii15298 opened this issue Oct 12, 2024 · 0 comments

Comments

@valerii15298
Copy link

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 });
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant