From 3d27d153fcb829ed0dc1e1494e14dcaebcdc6314 Mon Sep 17 00:00:00 2001 From: Diogo Silva Date: Sun, 3 Nov 2024 03:26:07 +0100 Subject: [PATCH 1/5] fix: assign schemas to host ajv instance --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index 34eb845..7d6f4c5 100644 --- a/index.js +++ b/index.js @@ -41,6 +41,7 @@ function fastifyResponseValidation (fastify, opts, next) { } function onRoute (routeOpts) { + ajv.addSchema(Object.values(fastify.getSchemas())); if (routeOpts.responseValidation === false) return if (routeOpts.schema && routeOpts.schema.response) { const responseStatusCodeValidation = routeOpts.responseStatusCodeValidation || opts.responseStatusCodeValidation || false From 012ce33cb6931cb486a1ea68ba49593533cec493 Mon Sep 17 00:00:00 2001 From: Diogo Silva Date: Sun, 3 Nov 2024 03:38:09 +0100 Subject: [PATCH 2/5] fix: move schema add when strictly necessary --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 7d6f4c5..5fc63d1 100644 --- a/index.js +++ b/index.js @@ -41,9 +41,9 @@ function fastifyResponseValidation (fastify, opts, next) { } function onRoute (routeOpts) { - ajv.addSchema(Object.values(fastify.getSchemas())); if (routeOpts.responseValidation === false) return if (routeOpts.schema && routeOpts.schema.response) { + ajv.addSchema(Object.values(fastify.getSchemas())); const responseStatusCodeValidation = routeOpts.responseStatusCodeValidation || opts.responseStatusCodeValidation || false routeOpts.preSerialization = routeOpts.preSerialization || [] routeOpts.preSerialization.push(buildHook(routeOpts.schema.response, responseStatusCodeValidation)) From 6ac52ba0dfd12c6724d61bd6c4e3e1bc0282bd23 Mon Sep 17 00:00:00 2001 From: Diogo Silva Date: Sun, 3 Nov 2024 15:12:36 +0100 Subject: [PATCH 3/5] fix: avoid adding schemas to avj when already existing --- index.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 5fc63d1..7053421 100644 --- a/index.js +++ b/index.js @@ -41,9 +41,16 @@ function fastifyResponseValidation (fastify, opts, next) { } function onRoute (routeOpts) { + const schemas = Object.values(fastify.getSchemas()) + + for (const schema of schemas) { + if (!ajv.getSchema(schema.$id)) { + ajv.addSchema(schema) + } + } + if (routeOpts.responseValidation === false) return if (routeOpts.schema && routeOpts.schema.response) { - ajv.addSchema(Object.values(fastify.getSchemas())); const responseStatusCodeValidation = routeOpts.responseStatusCodeValidation || opts.responseStatusCodeValidation || false routeOpts.preSerialization = routeOpts.preSerialization || [] routeOpts.preSerialization.push(buildHook(routeOpts.schema.response, responseStatusCodeValidation)) From 08bff6bc8681e2d24a7d17a8127a20c53a838e33 Mon Sep 17 00:00:00 2001 From: Diogo Silva Date: Sun, 3 Nov 2024 15:13:07 +0100 Subject: [PATCH 4/5] chore: add ref support test --- test/index.test.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/index.test.js b/test/index.test.js index a572994..e533ee8 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -194,6 +194,42 @@ test('Should support media types', async t => { t.assert.deepStrictEqual(JSON.parse(response.payload), { answer: 42 }) }) +test('Should support ref schema', async (t) => { + const fastify = Fastify() + await fastify.register(plugin) + + fastify.addSchema({ + $id: 'answerSchema', + type: 'object', + properties: { + answer: { type: 'number' } + } + }) + + fastify.route({ + method: 'GET', + url: '/', + schema: { + response: { + '2xx': { + $ref: 'answerSchema' + } + } + }, + handler: async (req, reply) => { + return { answer: 42 } + } + }) + + const response = await fastify.inject({ + method: 'GET', + url: '/' + }) + + t.assert.strictEqual(response.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(response.payload), { answer: 42 }) +}) + test('Should check anyOf Schema', async t => { const fastify = Fastify() await fastify.register(plugin) From 52248c72fdf45f89fd735eecd1aff1a15dad67f3 Mon Sep 17 00:00:00 2001 From: Diogo Silva Date: Sun, 3 Nov 2024 15:18:29 +0100 Subject: [PATCH 5/5] fix: fix: move schema add when strictly necessary --- index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 7053421..5d16af4 100644 --- a/index.js +++ b/index.js @@ -41,16 +41,16 @@ function fastifyResponseValidation (fastify, opts, next) { } function onRoute (routeOpts) { - const schemas = Object.values(fastify.getSchemas()) + if (routeOpts.responseValidation === false) return + if (routeOpts.schema && routeOpts.schema.response) { + const schemas = Object.values(fastify.getSchemas()) - for (const schema of schemas) { - if (!ajv.getSchema(schema.$id)) { - ajv.addSchema(schema) + for (const schema of schemas) { + if (!ajv.getSchema(schema.$id)) { + ajv.addSchema(schema) + } } - } - if (routeOpts.responseValidation === false) return - if (routeOpts.schema && routeOpts.schema.response) { const responseStatusCodeValidation = routeOpts.responseStatusCodeValidation || opts.responseStatusCodeValidation || false routeOpts.preSerialization = routeOpts.preSerialization || [] routeOpts.preSerialization.push(buildHook(routeOpts.schema.response, responseStatusCodeValidation))