Skip to content

Commit

Permalink
fix: body was not rendering (#12)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vacas5 authored Nov 21, 2024
1 parent 7b8d7fe commit e8fb44d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
27 changes: 26 additions & 1 deletion src/openAPI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -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;
});
});
10 changes: 9 additions & 1 deletion src/openAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down

0 comments on commit e8fb44d

Please sign in to comment.