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.51.0",
"version": "5.52.0",
"description": "The Athenna Http server. Built on top of fastify.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
10 changes: 8 additions & 2 deletions src/kernels/HttpKernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,14 @@ export class HttpKernel {

if (swaggerPlugin) {
const openapiConfig = Json.omit(Config.get('openapi', {}), ['paths'])
const pluginConfig = Json.omit(Config.get('http.swagger.configurations', {}), ['swagger'])
const swaggerConfig = Config.get('http.swagger.configurations.swagger', {})
const pluginConfig = Json.omit(
Config.get('http.swagger.configurations', {}),
['swagger']
)
const swaggerConfig = Config.get(
'http.swagger.configurations.swagger',
{}
)

await Server.plugin(swaggerPlugin, {
...pluginConfig,
Expand Down
22 changes: 18 additions & 4 deletions src/router/RouteSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,31 @@ async function parseSchema(schema: ZodAny, data: any) {
}

function toJsonSchema(schema: ZodAny, io: 'input' | 'output') {
const jsonSchemaMethod =
(schema as any)['~standard']?.jsonSchema?.[io] ||
(schema as any).toJSONSchema
const standardJsonSchemaMethod = (schema as any)['~standard']?.jsonSchema?.[
io
]

if (standardJsonSchemaMethod) {
const jsonSchema = standardJsonSchemaMethod({
target: 'draft-07',
libraryOptions: { unrepresentable: 'any' }
})

delete jsonSchema.$schema

return jsonSchema
}

const jsonSchemaMethod = (schema as any).toJSONSchema

if (!jsonSchemaMethod) {
return {}
}

const jsonSchema = jsonSchemaMethod({
io,
target: 'draft-07',
libraryOptions: { unrepresentable: 'any' }
unrepresentable: 'any'
})

delete jsonSchema.$schema
Expand Down
2 changes: 1 addition & 1 deletion src/server/ServerImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ export class ServerImpl extends Macroable {

const normalizedSchema = normalizeRouteSchema(automaticSchema)
const currentConfig = { ...(fastifyOptions.config || {}) }

const currentSwaggerSchema =
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand Down
58 changes: 58 additions & 0 deletions tests/unit/router/RouteTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,64 @@ export default class RouteTest {
})
}

@Test()
public async shouldBeAbleToUseZodTransformSchemasWithoutBreakingSwagger({ assert }: Context) {
Route.post('transform', async ctx => {
await ctx.response.status(201).send({
name: ctx.request.input('name')
})
}).schema({
body: z.object({
name: z.string().transform(value => value.toUpperCase())
}),
response: {
201: z.object({
name: z.string().transform(value => value.toUpperCase())
})
}
})

Route.register()

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

assert.equal(response.statusCode, 201)
assert.deepEqual(response.json(), { name: 'LENON' })

const swagger = await Server.getSwagger()

assert.containSubset(swagger.paths['/transform'], {
post: {
responses: {
'201': {
schema: {
type: 'object',
properties: {
name: {}
}
}
}
},
parameters: [
{
in: 'body',
schema: {
type: 'object',
properties: {
name: { type: 'string' }
},
required: ['name']
}
}
]
}
})
}

@Test()
public async shouldBeAbleToUseExplicitZodCoercionForQuerystringAndParams({ assert }: Context) {
Route.get('users/:id', async ctx => {
Expand Down
Loading