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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/http",
"version": "5.56.0",
"version": "5.57.0",
"description": "The Athenna Http server. Built on top of fastify.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
2 changes: 1 addition & 1 deletion src/router/Route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export class Route extends Macroable {
// @ts-ignore
this.route.fastify.config.zod = zod

if (Object.keys(zod.response).length) {
if (Object.keys(swaggerSchema).length) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this.route.fastify.config.swaggerSchema = swaggerSchema
Expand Down
3 changes: 2 additions & 1 deletion src/router/RouteSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ export function normalizeRouteSchema(options: RouteSchemaOptions): {
}

request[key] = options[key]
schema[key] = toJsonSchema(options[key], 'input')
swaggerSchema[key] = toJsonSchema(options[key], 'input')

delete schema[key]
})

if (options.response && Is.Object(options.response)) {
Expand Down
49 changes: 48 additions & 1 deletion tests/unit/router/RouteTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Config } from '@athenna/config'
import { MyMiddleware } from '#tests/fixtures/middlewares/MyMiddleware'
import { Test, AfterEach, BeforeEach, type Context, Cleanup } from '@athenna/test'
import { HelloController } from '#tests/fixtures/controllers/HelloController'
import { Route, Server, HttpRouteProvider, HttpServerProvider } from '#src'
import { Route, Server, HttpKernel, HttpRouteProvider, HttpServerProvider } from '#src'
import { z } from 'zod'

export default class RouteTest {
Expand Down Expand Up @@ -369,6 +369,53 @@ export default class RouteTest {
})
}

@Test()
@Cleanup(() => Config.set('openapi.paths', {}))
@Cleanup(() => Config.set('http.logger.ignoreStatuses', []))
public async shouldUseZodValidationErrorsForInvalidOpenApiRequestSchemas({ assert }: Context) {
Config.set('http.logger.ignoreStatuses', [422])

const kernel = new HttpKernel()
await kernel.registerExceptionHandler()

Config.set('openapi.paths', {
'/avatars': {
post: {
body: z.object({
name: z.string(),
siteId: z.string(),
slug: z.string()
})
}
}
})

Route.post('avatars', async ctx => {
await ctx.response.status(201).send({ ok: true })
})

Route.register()

const response = await Server.request({
path: '/avatars',
method: 'post',
payload: {}
})

const body = response.json()

assert.equal(response.statusCode, 422)
assert.equal(body.code, 'E_VALIDATION_ERROR')
assert.deepEqual(
body.details.map(({ expected, code, path }) => ({ expected, code, path })),
[
{ expected: 'string', code: 'invalid_type', path: ['name'] },
{ expected: 'string', code: 'invalid_type', path: ['siteId'] },
{ expected: 'string', code: 'invalid_type', path: ['slug'] }
]
)
}

@Test()
public async shouldBeAbleToHideARouteFromTheSwaggerDocumentation({ assert }: Context) {
Route.get('test', new HelloController().index)
Expand Down
Loading