Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: should now call default schema compilers #4340

Merged
merged 1 commit into from Oct 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/schema-controller.js
Expand Up @@ -15,12 +15,16 @@ function buildSchemaController (parentSchemaCtrl, opts) {
return new SchemaController(parentSchemaCtrl, opts)
}

let compilersFactory = {
buildValidator: ValidatorSelector(),
buildSerializer: SerializerSelector()
const compilersFactory = Object.assign({
buildValidator: null,
buildSerializer: null
}, opts?.compilersFactory)

if (!compilersFactory.buildValidator) {
compilersFactory.buildValidator = ValidatorSelector()
}
if (opts && opts.compilersFactory) {
compilersFactory = Object.assign(compilersFactory, opts.compilersFactory)
if (!compilersFactory.buildSerializer) {
compilersFactory.buildSerializer = SerializerSelector()
}

const option = {
Expand Down
42 changes: 42 additions & 0 deletions test/schema-special-usage.test.js
Expand Up @@ -702,3 +702,45 @@ test('Custom schema object should not trigger FST_ERR_SCH_DUPLICATE', async t =>
await fastify.ready()
t.pass('fastify is ready')
})

test('The default schema compilers should not be called when overwritte by the user', async t => {
const Fastify = t.mock('../', {
'@fastify/ajv-compiler': () => {
t.fail('The default validator compiler should not be called')
},
'@fastify/fast-json-stringify-compiler': () => {
t.fail('The default serializer compiler should not be called')
}
})

const fastify = Fastify({
schemaController: {
compilersFactory: {
buildValidator: function factory () {
t.pass('The custom validator compiler should be called')
return function validatorCompiler () {
return () => { return true }
}
},
buildSerializer: function factory () {
t.pass('The custom serializer compiler should be called')
return function serializerCompiler () {
return () => { return true }
}
}
}
}
})

fastify.get('/',
{
schema: {
query: { foo: { type: 'string' } },
response: {
200: { type: 'object' }
}
}
}, () => {})

await fastify.ready()
})