diff --git a/README.md b/README.md index 9958b64..6047f98 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ This package is responsible for compiling the application's `response` JSON sche | v1.x | v3.x | ^3.x | | v2.x | v3.x | ^4.x | | v3.x | v4.x | ^4.x | +| v3.x | v5.x | ^4.x | ### fast-json-stringify Configuration diff --git a/index.js b/index.js index 2f14dd0..836b4c7 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,10 @@ function SerializerSelector () { } function responseSchemaCompiler (fjsOpts, { schema /* method, url, httpStatus */ }) { + if (fjsOpts.schema && schema.$id && fjsOpts.schema[schema.$id]) { + fjsOpts.schema = { ...fjsOpts.schema } + delete fjsOpts.schema[schema.$id] + } return fastJsonStringify(schema, fjsOpts) } diff --git a/package.json b/package.json index 0fd96d6..d52874a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@fastify/fast-json-stringify-compiler", "description": "Build and manage the fast-json-stringify instances for the fastify framework", - "version": "3.0.1", + "version": "4.0.0", "main": "index.js", "types": "index.d.ts", "scripts": { @@ -25,10 +25,10 @@ "fastify": "^4.0.0", "standard": "^17.0.0", "tap": "^16.0.0", - "tsd": "^0.20.0" + "tsd": "^0.21.0" }, "dependencies": { - "fast-json-stringify": "^4.2.0" + "fast-json-stringify": "^5.0.0" }, "tsd": { "directory": "test/types" diff --git a/test/duplicate-schema.test.js b/test/duplicate-schema.test.js new file mode 100644 index 0000000..d6904ed --- /dev/null +++ b/test/duplicate-schema.test.js @@ -0,0 +1,26 @@ +'use strict' + +const t = require('tap') +const FjsCompiler = require('../index') + +t.test('Use input schema duplicate in the externalSchemas', async t => { + t.plan(1) + const externalSchemas = { + schema1: { + $id: 'schema1', + type: 'number' + }, + schema2: { + $id: 'schema2', + type: 'string' + } + } + + const factory = FjsCompiler() + const compiler = factory(externalSchemas) + + compiler({ schema: externalSchemas.schema1 }) + compiler({ schema: externalSchemas.schema2 }) + + t.pass() +})