Skip to content

Commit 929c08d

Browse files
authored
Merge pull request #19 from LeandroPereiraDaCruz/feat/nullishResponses
feat: handle nullish response types
2 parents 08f8c89 + 17f9674 commit 929c08d

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ const mapTypesResponse = (
4444
required: string[]
4545
}
4646
) => {
47+
if (typeof schema === 'object'
48+
&& ['void', 'undefined', 'null'].includes(schema.type)) return;
49+
4750
const responses: Record<string, OpenAPIV3.MediaTypeObject> = {}
4851

4952
for (const type of types)

test/index.test.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Elysia } from 'elysia'
1+
import { Elysia, t } from 'elysia'
22
import { swagger } from '../src'
33

44
import { describe, expect, it } from 'bun:test'
@@ -65,4 +65,49 @@ describe('Swagger', () => {
6565
const res = await app.handle(req('/v2/swagger'))
6666
expect(res.status).toBe(200)
6767
})
68+
69+
it('should not return content response when using Void type', async () => {
70+
const app = new Elysia().use(
71+
swagger())
72+
.get('/void', () => {}, {
73+
response: { 204: t.Void({
74+
description: 'Void response'
75+
})}});
76+
77+
const res = await app.handle(req('/swagger/json'))
78+
expect(res.status).toBe(200)
79+
const response = await res.json();
80+
expect(response.paths['/void'].get.responses['204'].description).toBe('Void response');
81+
expect(response.paths['/void'].get.responses['204'].content).toBeUndefined();
82+
})
83+
84+
it('should not return content response when using Undefined type', async () => {
85+
const app = new Elysia().use(
86+
swagger())
87+
.get('/undefined', () => undefined, {
88+
response: { 204: t.Undefined({
89+
description: 'Undefined response'
90+
})}});
91+
92+
const res = await app.handle(req('/swagger/json'))
93+
expect(res.status).toBe(200)
94+
const response = await res.json();
95+
expect(response.paths['/undefined'].get.responses['204'].description).toBe('Undefined response');
96+
expect(response.paths['/undefined'].get.responses['204'].content).toBeUndefined();
97+
})
98+
99+
it('should not return content response when using Null type', async () => {
100+
const app = new Elysia().use(
101+
swagger())
102+
.get('/null', () => null, {
103+
response: { 204: t.Null({
104+
description: 'Null response'
105+
})}});
106+
107+
const res = await app.handle(req('/swagger/json'))
108+
expect(res.status).toBe(200)
109+
const response = await res.json();
110+
expect(response.paths['/null'].get.responses['204'].description).toBe('Null response');
111+
expect(response.paths['/null'].get.responses['204'].content).toBeUndefined();
112+
})
68113
})

0 commit comments

Comments
 (0)