Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/generator/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
Expand Down
21 changes: 19 additions & 2 deletions src/generator/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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<string, true> = {};
pathParameters.forEach((pathParameter) => {
mask[pathParameter] = true;
});
const dedupedSchema = schema.omit(mask);

return {
required: !schema.isOptional(),
content: {
'application/json': {
schema: zodSchemaToOpenApiSchemaObject(schema),
schema: zodSchemaToOpenApiSchemaObject(dedupedSchema),
},
},
};
Expand Down
6 changes: 0 additions & 6 deletions test/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
Expand Down