diff --git a/src/generator/paths.ts b/src/generator/paths.ts index 3ddaea39..c0cf1b15 100644 --- a/src/generator/paths.ts +++ b/src/generator/paths.ts @@ -92,7 +92,7 @@ export const getOpenApiPathsObject = ( description, tags, security: protect ? [{ Authorization: [] }] : undefined, - requestBody: getRequestBodyObject(inputParser), + requestBody: getRequestBodyObject(inputParser, pathParameters), parameters: getParameterObjects(inputParser, pathParameters, 'path'), responses: getResponsesObject(outputParser), }, diff --git a/src/generator/schema.ts b/src/generator/schema.ts index f8c16607..2f40326f 100644 --- a/src/generator/schema.ts +++ b/src/generator/schema.ts @@ -106,7 +106,10 @@ export const getParameterObjects = ( }); }; -export const getRequestBodyObject = (schema: unknown): OpenAPIV3.RequestBodyObject | undefined => { +export const getRequestBodyObject = ( + schema: unknown, + pathParameters: string[], +): OpenAPIV3.RequestBodyObject | undefined => { if (!instanceofZod(schema)) { throw new TRPCError({ message: 'Input parser expects a Zod validator', @@ -122,11 +125,25 @@ export const getRequestBodyObject = (schema: unknown): OpenAPIV3.RequestBodyObje return undefined; } + if (!instanceofZodTypeKind(schema, z.ZodFirstPartyTypeKind.ZodObject)) { + throw new TRPCError({ + message: 'Input parser must be a ZodObject', + code: 'INTERNAL_SERVER_ERROR', + }); + } + + // remove path parameters + const mask: Record = {}; + pathParameters.forEach((pathParameter) => { + mask[pathParameter] = true; + }); + const dedupedSchema = schema.omit(mask); + return { required: !schema.isOptional(), content: { 'application/json': { - schema: zodSchemaToOpenApiSchemaObject(schema), + schema: zodSchemaToOpenApiSchemaObject(dedupedSchema), }, }, }; diff --git a/test/generator.test.ts b/test/generator.test.ts index 1b0ba5f3..411b53f3 100644 --- a/test/generator.test.ts +++ b/test/generator.test.ts @@ -893,16 +893,10 @@ describe('generator', () => { "schema": Object { "additionalProperties": false, "properties": Object { - "id": Object { - "type": "string", - }, "name": Object { "type": "string", }, }, - "required": Array [ - "id", - ], "type": "object", }, },