Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ const mapTypesResponse = (
required: string[]
}
) => {
if (typeof schema === 'object'
&& ['void', 'undefined', 'null'].includes(schema.type)) return;

const responses: Record<string, OpenAPIV3.MediaTypeObject> = {}

for (const type of types)
Expand Down
47 changes: 46 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Elysia } from 'elysia'
import { Elysia, t } from 'elysia'
import { swagger } from '../src'

import { describe, expect, it } from 'bun:test'
Expand Down Expand Up @@ -65,4 +65,49 @@ describe('Swagger', () => {
const res = await app.handle(req('/v2/swagger'))
expect(res.status).toBe(200)
})

it('should not return content response when using Void type', async () => {
const app = new Elysia().use(
swagger())
.get('/void', () => {}, {
response: { 204: t.Void({
description: 'Void response'
})}});

const res = await app.handle(req('/swagger/json'))
expect(res.status).toBe(200)
const response = await res.json();
expect(response.paths['/void'].get.responses['204'].description).toBe('Void response');
expect(response.paths['/void'].get.responses['204'].content).toBeUndefined();
})

it('should not return content response when using Undefined type', async () => {
const app = new Elysia().use(
swagger())
.get('/undefined', () => undefined, {
response: { 204: t.Undefined({
description: 'Undefined response'
})}});

const res = await app.handle(req('/swagger/json'))
expect(res.status).toBe(200)
const response = await res.json();
expect(response.paths['/undefined'].get.responses['204'].description).toBe('Undefined response');
expect(response.paths['/undefined'].get.responses['204'].content).toBeUndefined();
})

it('should not return content response when using Null type', async () => {
const app = new Elysia().use(
swagger())
.get('/null', () => null, {
response: { 204: t.Null({
description: 'Null response'
})}});

const res = await app.handle(req('/swagger/json'))
expect(res.status).toBe(200)
const response = await res.json();
expect(response.paths['/null'].get.responses['204'].description).toBe('Null response');
expect(response.paths['/null'].get.responses['204'].content).toBeUndefined();
})
})