Skip to content

Commit

Permalink
Fix schema controller error when adding response schemas (#3269)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eomm committed Aug 19, 2021
1 parent 0dbc89b commit 13f2c36
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
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

0 comments on commit 13f2c36

Please sign in to comment.