Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Model Schemas to OpenApi Docs #417

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

calasanmarko
Copy link

From issue #157 :

I needed a way to integrate my OpenAPI-extended Zod schemas (using @asteasolutions/zod-to-openapi) with the rest of the document generated by trpc-openapi, so I threw this together in an afternoon.

This implementation integrates a registry of Zod schemas into the final document, and also links return, parameter, and request body types of tRPC procedures with these schemas.

A minimal example of how this looks with @asteasolutions/zod-to-openapi (although it should work with any solution):

import { OpenAPIRegistry, OpenApiGeneratorV3 } from "@asteasolutions/zod-to-openapi";
import { setZodComponentDefinitions, setZodComponentSchemaGenerator } from "trpc-openapi";
import { z } from "zod";

const registry = new OpenAPIRegistry();
registry.register("ComponentName", z.object({
    // ...
}))

const generator = new OpenApiGeneratorV3(registry.definitions);

// ~~~ FUNCTIONS FROM THE PR BELOW ~~~

// The definitions are used to map the types referenced in tRPC procedures
setZodComponentDefinitions(registry.definitions.reduce((acc, d) => {
    if (d.type === 'schema') {
        acc[d.schema._def.openapi._internal.refId] = d.schema;
    }
    return acc;
}, {} as { [key: string]: z.ZodType }));

// The schema generator generates the actual components in the OpenAPI document
setZodComponentSchemaGenerator(() => generator.generateDocument(config).components?.schemas ?? {});

@tonyxiao
Copy link

Can this work with other generators notably https://github.com/samchungy/zod-openapi?

@calasanmarko
Copy link
Author

I don't see why not -- it should work as long as the generator embeds a reference ID somewhere in the object definition, which a cursory glance at zod-openapi tells me it does just that -- see https://github.com/samchungy/zod-openapi#auto-registering-schema

I might take it upon myself to test it in the next few days, but it would almost certainly work.

@tonyxiao
Copy link

That'd be awesome. samchungy's version is an evolution of the @asteasolutions/zod-to-openapi one

@tonyxiao
Copy link

Just found this fork and it seems to work like a charm!

@tonyxiao
Copy link

https://github.com/lilyrose2798/trpc-openapi

Wonder if the two PRs overlap / can be used together or are there conflicting ideas?

@tonyxiao
Copy link

@calasanmarko can you look take a look at the fork from @LilyRose2798 too? Seems to contain the same functionalities but more robust and supports optional schema registration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants