feat: handle nullish response types

This commit is contained in:
Leandro Pereira da Cruz
2023-09-14 18:59:55 -03:00
parent 47a5b26abc
commit 17f9674d63
2 changed files with 49 additions and 1 deletions

View File

@@ -1,4 +1,4 @@
import { Elysia } from 'elysia'
import { Elysia, t } from 'elysia'
import { swagger } from '../src'
import { describe, expect, it } from 'bun:test'
@@ -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();
})
})