Skip to content

Commit

Permalink
Merge pull request #112 from hagishi/feature/required-body
Browse files Browse the repository at this point in the history
feat: set required field when a request body is present
  • Loading branch information
marclave committed Apr 5, 2024
2 parents 5e78190 + eafe5d5 commit d63db39
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/utils.ts
Expand Up @@ -264,6 +264,7 @@ export const registerSchemaPath = ({
...(bodySchema
? {
requestBody: {
required: true,
content: mapTypesResponse(
contentTypes,
typeof bodySchema === 'string'
Expand Down
12 changes: 12 additions & 0 deletions test/index.test.ts
Expand Up @@ -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);
})

})

0 comments on commit d63db39

Please sign in to comment.