From e8fb44d71306941c37b31ce91c117745ae70f62a Mon Sep 17 00:00:00 2001 From: Russell Anderson Date: Thu, 21 Nov 2024 15:57:56 -0600 Subject: [PATCH] fix: body was not rendering (#12) Missed a test case and part of the migration doc where the request body had to be updated to support the changes made to the upstream dep. --- src/openAPI.test.ts | 27 ++++++++++++++++++++++++++- src/openAPI.ts | 10 +++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/openAPI.test.ts b/src/openAPI.test.ts index ae56a30..c10e1e1 100644 --- a/src/openAPI.test.ts +++ b/src/openAPI.test.ts @@ -70,7 +70,7 @@ describe("buildOpenAPIDocument", () => { ), ); const routers: Router[] = [router]; - const schemaPaths: string[] = []; + const schemaPaths: string[] = ["../mocks/schemas"]; const errors = { 401: "Unauthorized", 403: "Forbidden" }; const document = buildOpenAPIDocument({ config, routers, schemaPaths, errors }); @@ -146,4 +146,29 @@ describe("buildOpenAPIDocument", () => { expect(responseSchema.$ref.includes("ResponseSchema")).to.be.true; }); + + it("should properly describe routes with request body", () => { + const config = { openapi: "3.0.0", info: { title: "Test API", version: "1.0.0" } }; + const router = Router(); + router.get( + "/test", + openAPIRoute( + { + tag: "Test", + summary: "Test route", + body: schemas.BodySchema, + response: schemas.ResponseSchema, + }, + (req, res) => res.json({ success: true }), + ), + ); + const routers: Router[] = [router]; + const schemaPaths: string[] = ["../mocks/schemas"]; + const errors = { 401: "Unauthorized", 403: "Forbidden" }; + + const document = buildOpenAPIDocument({ config, routers, schemaPaths, errors }); + const requestBodySchema = document.paths["/test"].get.requestBody.content["application/json"].schema; + + expect(requestBodySchema.$ref.includes("BodySchema")).to.be.true; + }); }); diff --git a/src/openAPI.ts b/src/openAPI.ts index 60c1edf..3652832 100644 --- a/src/openAPI.ts +++ b/src/openAPI.ts @@ -167,7 +167,15 @@ export function buildOpenAPIDocument(args: { request: { params: asZodObject(referencingNamedSchemas(params)), query: asZodObject(referencingNamedSchemas(query)), - body: referencingNamedSchemas(body), + body: body + ? { + content: { + "application/json": { + schema: referencingNamedSchemas(body), + }, + }, + } + : undefined, }, responses: responses, };