From eafe5d5ee60b76a8f340c8bf32ffc899a0624e21 Mon Sep 17 00:00:00 2001 From: hagishi Date: Fri, 5 Apr 2024 10:02:13 +0900 Subject: [PATCH] feat: set required field when a request body is present --- src/utils.ts | 1 + test/index.test.ts | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/utils.ts b/src/utils.ts index 1efa070..ee0f8bf 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -264,6 +264,7 @@ export const registerSchemaPath = ({ ...(bodySchema ? { requestBody: { + required: true, content: mapTypesResponse( contentTypes, typeof bodySchema === 'string' diff --git a/test/index.test.ts b/test/index.test.ts index ec3fe82..d5e32ba 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -156,4 +156,16 @@ describe('Swagger', () => { response.paths['/null'].get.responses['204'].content ).toBeUndefined() }) + + it("should set the required field to true when a request body is present", async () => { + const app = new Elysia().use(swagger()).post("/post", () => {}, { + body: t.Object({ name: t.String() }), + }); + + const res = await app.handle(req("/swagger/json")); + expect(res.status).toBe(200); + const response = await res.json(); + expect(response.paths['/post'].post.requestBody.required).toBe(true); + }) + })