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 schema controller error when adding response schemas #3269

Merged
merged 3 commits into from Aug 19, 2021
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
19 changes: 12 additions & 7 deletions lib/schema-controller.js
Expand Up @@ -15,13 +15,18 @@ function buildSchemaController (parentSchemaCtrl, opts) {
return new SchemaController(parentSchemaCtrl, opts)
}

const option = Object.assign({
bucket: buildSchemas,
compilersFactory: {
buildValidator: ValidatorSelector(),
buildSerializer: serializerCompiler
}
}, opts)
let compilersFactory = {
buildValidator: ValidatorSelector(),
buildSerializer: serializerCompiler
}
if (opts && opts.compilersFactory) {
compilersFactory = Object.assign(compilersFactory, opts.compilersFactory)
}

const option = {
bucket: (opts && opts.bucket) || buildSchemas,
compilersFactory: compilersFactory
}

return new SchemaController(undefined, option)
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -123,9 +123,9 @@
"@types/pino": "^6.0.1",
"@typescript-eslint/eslint-plugin": "^4.5.0",
"@typescript-eslint/parser": "^4.5.0",
"JSONStream": "^1.3.5",
"ajv": "^6.0.0",
"ajv-errors": "^1.0.1",
"ajv-formats": "^2.1.1",
"ajv-i18n": "^3.5.0",
"ajv-merge-patch": "^4.1.0",
"ajv-pack": "^0.3.1",
Expand All @@ -151,6 +151,7 @@
"hsts": "^2.2.0",
"http-errors": "^1.7.1",
"ienoopen": "^1.1.0",
"JSONStream": "^1.3.5",
"license-checker": "^25.0.1",
"pem": "^1.14.4",
"proxyquire": "^2.1.3",
Expand Down
45 changes: 43 additions & 2 deletions test/schema-special-usage.test.js
Expand Up @@ -11,7 +11,7 @@ const ajvErrors = require('ajv-errors')
const buildValidatorAJV8 = require('@fastify/ajv-compiler-8')

test('Ajv8 usage instead of the bundle one', t => {
t.plan(1)
t.plan(2)

t.test('use new ajv8 option', t => {
t.plan(2)
Expand Down Expand Up @@ -46,9 +46,50 @@ test('Ajv8 usage instead of the bundle one', t => {
t.match(err.message, 'strictRequired', 'the new ajv8 option trigger a startup error')
})
})

t.test('use new ajv8 option within a response schema', t => {
t.plan(2)
const fastify = Fastify({
schemaController: {
compilersFactory: {
buildValidator: buildValidatorAJV8()
}
}
})

fastify.post('/', {
schema: {
body: {
type: 'object',
required: ['missing'],
properties: {
foo: {
type: 'string'
}
}
},
response: {
'2xx': {
type: 'object',
properties: {
ok: {
type: 'integer'
}
}
}
}
},
handler (req, reply) { reply.send({ ok: 1 }) }
})

fastify.ready(err => {
t.error(err)
t.pass('startup successfull')
})
})
})

test('Ajv8 usage with plugins', { skip: 'until npm 7.2 will be bundled with node.js 16 https://github.com/npm/cli/issues/3147' }, t => {
test('Ajv8 usage with plugins', t => {
t.plan(2)

t.test('use new ajv8 option', t => {
Expand Down