diff --git a/standalone.js b/standalone.js index 7f0d40a..e593fb1 100644 --- a/standalone.js +++ b/standalone.js @@ -11,7 +11,7 @@ function SerializerSelector () { function responseSchemaCompiler (fjsOpts, { schema /* method, url, httpStatus */ }) { if (fjsOpts.schema && schema.$id && fjsOpts.schema[schema.$id]) { - fjsOpts.schema = { ...fjsOpts.schema } + fjsOpts = { ...fjsOpts, schema: { ...fjsOpts.schema } } delete fjsOpts.schema[schema.$id] } return fastJsonStringify(schema, fjsOpts) diff --git a/test/duplicate-schema.test.js b/test/duplicate-schema.test.js index 2acaa24..e1e4377 100644 --- a/test/duplicate-schema.test.js +++ b/test/duplicate-schema.test.js @@ -24,3 +24,22 @@ test('Use input schema duplicate in the externalSchemas', async t => { t.assert.ok(true) }) + +test('Keep the external schemas for later compilations', async t => { + t.plan(1) + const externalSchemas = { + schema1: { + $id: 'schema1', + type: 'object', + properties: { name: { type: 'string' } } + } + } + + const factory = FjsCompiler() + const compiler = factory(externalSchemas) + + compiler({ schema: externalSchemas.schema1 }) + const serialize = compiler({ schema: { $ref: 'schema1#' } }) + + t.assert.equal(serialize({ name: 'a', secret: 1 }), '{"name":"a"}') +})