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

feat: Add Model Schemas to OpenApi Docs #417

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Zod-to-openapi
calasanmarko committed Oct 26, 2023
commit 456456f6510ecd88f32bfbd24c0a83b1181b7946
32 changes: 28 additions & 4 deletions src/generator/schema.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import { OpenAPIV3 } from 'openapi-types';
import { z } from 'zod';
import zodToJsonSchema from 'zod-to-json-schema';

import { OpenApiContentType } from '../types';
import { OpenApiContentType, ZodToOpenApiRegistry } from '../types';
import {
instanceofZodType,
instanceofZodTypeCoercible,
@@ -15,9 +15,33 @@ import {
zodSupportsCoerce,
} from '../utils/zod';

let zodComponentDefinitions: Record<string, z.ZodType> = {};

const zodSchemaToOpenApiSchemaObject = (zodSchema: z.ZodType): OpenAPIV3.SchemaObject => {
// FIXME: https://github.com/StefanTerdell/zod-to-json-schema/issues/35
return zodToJsonSchema(zodSchema, { target: 'openApi3', $refStrategy: 'none' }) as any;
const result = zodToJsonSchema(zodSchema, {
target: 'openApi3',
definitions: zodComponentDefinitions,
definitionPath: 'components/schemas',
}) as any;
delete result['components/schemas'];
return result;
};

export const setZodComponentDefinitions = (definitions: Record<string, z.ZodType>) => {
zodComponentDefinitions = definitions;
};

export const setZodComponentRegistry = (registry: ZodToOpenApiRegistry) => {
const mapped = registry.definitions.reduce((acc, d) => {
const refId = d.schema?._def.openapi?._internal?.refId;
if (d.type === 'schema' && refId && d.schema) {
acc[refId] = d.schema;
}
return acc;
}, {} as { [key: string]: z.ZodType });

setZodComponentDefinitions(mapped);
};

export const getParameterObjects = (
@@ -156,7 +180,7 @@ export const getRequestBodyObject = (
return undefined;
}

const openApiSchemaObject = zodSchemaToOpenApiSchemaObject(dedupedSchema);
const openApiSchemaObject = zodSchemaToOpenApiSchemaObject(unwrappedSchema);
const content: OpenAPIV3.RequestBodyObject['content'] = {};
for (const contentType of contentTypes) {
content[contentType] = {
@@ -189,7 +213,7 @@ export const errorResponseObject: OpenAPIV3.ResponseObject = {
export const getResponsesObject = (
schema: unknown,
example: Record<string, any> | undefined,
headers: Record<string, OpenAPIV3.HeaderObject | OpenAPIV3.ReferenceObject> | undefined
headers: Record<string, OpenAPIV3.HeaderObject | OpenAPIV3.ReferenceObject> | undefined,
): OpenAPIV3.ResponsesObject => {
if (!instanceofZodType(schema)) {
throw new TRPCError({
21 changes: 20 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import type { RootConfig } from '@trpc/server/dist/core/internals/config';
import { TRPC_ERROR_CODE_KEY } from '@trpc/server/rpc';
import type { RouterDef } from '@trpc/server/src/core/router';
import { OpenAPIV3 } from 'openapi-types';
import { ZodIssue } from 'zod';
import { ZodIssue, z } from 'zod';

export type OpenApiMethod = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE';

@@ -77,3 +77,22 @@ export type OpenApiErrorResponse = {
};

export type OpenApiResponse<D = any> = OpenApiSuccessResponse<D> | OpenApiErrorResponse;

export type ZodToOpenApiRegistry = {
definitions: ZodToOpenApiRegistryDefinition[];
};

export type ZodToOpenApiRegistryDefinition = {
type: string;
schema?: z.ZodType<
any,
z.ZodTypeDef & {
openapi?: {
_internal?: {
refId?: string;
};
};
},
any
>;
};