From d6f5d6de67c485af828696680ca72901f1c69ef7 Mon Sep 17 00:00:00 2001 From: IOsonoTAN Date: Tue, 6 Oct 2020 06:45:44 +0700 Subject: [PATCH 1/2] feature: Add optional to show schema id instead of def-# --- ref-resolver.js | 7 +-- test/ref-resolver.test.js | 62 +++++++++++++++++++++++++ test/schemas/relativeId-showSchemaId.js | 8 ++++ 3 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 test/schemas/relativeId-showSchemaId.js diff --git a/ref-resolver.js b/ref-resolver.js index 3354a5e..b18e58e 100644 --- a/ref-resolver.js +++ b/ref-resolver.js @@ -17,7 +17,8 @@ const kConsumed = Symbol('json-schema-resolver.consumed') // when an external js const defaultOpts = { target: 'draft-07', - clone: false + clone: false, + showSchemaId: false } const targetSupported = ['draft-07'] // TODO , 'draft-08' @@ -33,7 +34,7 @@ const targetCfg = { // logic: https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.appendix.B.1 function jsonSchemaResolver (options) { const ee = new EventEmitter() - const { clone, target, applicationUri, externalSchemas: rootExternalSchemas } = Object.assign({}, defaultOpts, options) + const { clone, target, applicationUri, externalSchemas: rootExternalSchemas, showSchemaId } = Object.assign({}, defaultOpts, options) const allIds = new Map() let rolling = 0 @@ -137,7 +138,7 @@ function jsonSchemaResolver (options) { const id = URI.serialize(baseUri) + rel if (!allIds.has(id)) { debug('Collected $id %s', id) - json[kRefToDef] = `def-${rolling++}` + json[kRefToDef] = showSchemaId ? id : `def-${rolling++}` allIds.set(id, json) } else { debug('WARN duplicated id %s .. IGNORED - ', id) diff --git a/test/ref-resolver.test.js b/test/ref-resolver.test.js index fbe7304..040b0f5 100644 --- a/test/ref-resolver.test.js +++ b/test/ref-resolver.test.js @@ -244,3 +244,65 @@ test('absolute $ref #2', t => { t.equal(out.properties.address.$ref, '#/definitions/def-0') t.equal(out.properties.houses.items.$ref, '#/definitions/def-0') }) + +test('set showSchemaId to be true with relativeId', t => { + t.plan(1) + + const schema = factory('relativeId-showSchemaId') + const absSchemaId = { + $id: 'relativeIdSchemaId', + type: 'object', + properties: { + foo: { + type: 'string' + } + } + } + const externalSchemas = [absSchemaId] + + const resolver = RefResolver({ + clone: true, + showSchemaId: true + }) + resolver.resolve(schema, { + externalSchemas + }) + const definitions = resolver.definitions() + + t.deepEquals(definitions, { + definitions: { + [absSchemaId.$id]: absSchemaId + } + }) +}) + +test('set showSchemaId to be true with absoluteId', t => { + t.plan(1) + + const schema = factory('absoluteId-localRef') + const absSchemaId = { + $id: 'relativeIdSchemaId', + type: 'object', + properties: { + foo: { + type: 'string' + } + } + } + const externalSchemas = [absSchemaId] + + const resolver = RefResolver({ + clone: true, + showSchemaId: true + }) + resolver.resolve(schema, { + externalSchemas + }) + const definitions = resolver.definitions() + + t.deepEquals(definitions, { + definitions: { + 'http://example.com/relativeIdSchemaId': absSchemaId + } + }) +}) diff --git a/test/schemas/relativeId-showSchemaId.js b/test/schemas/relativeId-showSchemaId.js new file mode 100644 index 0000000..879267a --- /dev/null +++ b/test/schemas/relativeId-showSchemaId.js @@ -0,0 +1,8 @@ +module.exports = { + $id: 'relativeAddressShowSchemaId', + type: 'object', + properties: { + foo: { type: 'string' }, + bar: { type: 'string' } + } +} From 36fb37bf3e39873a2b74b2e592fa3f0a399ccf1c Mon Sep 17 00:00:00 2001 From: IOsonoTAN Date: Tue, 6 Oct 2020 16:13:51 +0700 Subject: [PATCH 2/2] update test case about $ref --- test/ref-resolver.test.js | 42 +++++++++++-------------- test/schemas/relativeId-showSchemaId.js | 16 ++++++++-- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/test/ref-resolver.test.js b/test/ref-resolver.test.js index 040b0f5..35732ab 100644 --- a/test/ref-resolver.test.js +++ b/test/ref-resolver.test.js @@ -245,51 +245,45 @@ test('absolute $ref #2', t => { t.equal(out.properties.houses.items.$ref, '#/definitions/def-0') }) +const phoneNumberSchemaId = { + $id: 'phoneNumber', + type: 'object', + properties: { + countryCode: { type: 'string' }, + number: { type: 'string' } + } +} + test('set showSchemaId to be true with relativeId', t => { - t.plan(1) + t.plan(4) const schema = factory('relativeId-showSchemaId') - const absSchemaId = { - $id: 'relativeIdSchemaId', - type: 'object', - properties: { - foo: { - type: 'string' - } - } - } - const externalSchemas = [absSchemaId] + const externalSchemas = [phoneNumberSchemaId] const resolver = RefResolver({ clone: true, showSchemaId: true }) - resolver.resolve(schema, { + const out = resolver.resolve(schema, { externalSchemas }) const definitions = resolver.definitions() t.deepEquals(definitions, { definitions: { - [absSchemaId.$id]: absSchemaId + [phoneNumberSchemaId.$id]: phoneNumberSchemaId } }) + t.equal(out.properties.personal.homeNumber.$ref, '#/definitions/phoneNumber') + t.equal(out.properties.personal.mobilePhoneNumber.$ref, '#/definitions/phoneNumber') + t.equal(out.properties.work.$ref, '#/definitions/phoneNumber') }) test('set showSchemaId to be true with absoluteId', t => { t.plan(1) const schema = factory('absoluteId-localRef') - const absSchemaId = { - $id: 'relativeIdSchemaId', - type: 'object', - properties: { - foo: { - type: 'string' - } - } - } - const externalSchemas = [absSchemaId] + const externalSchemas = [phoneNumberSchemaId] const resolver = RefResolver({ clone: true, @@ -302,7 +296,7 @@ test('set showSchemaId to be true with absoluteId', t => { t.deepEquals(definitions, { definitions: { - 'http://example.com/relativeIdSchemaId': absSchemaId + 'http://example.com/phoneNumber': phoneNumberSchemaId } }) }) diff --git a/test/schemas/relativeId-showSchemaId.js b/test/schemas/relativeId-showSchemaId.js index 879267a..c2644cc 100644 --- a/test/schemas/relativeId-showSchemaId.js +++ b/test/schemas/relativeId-showSchemaId.js @@ -1,8 +1,18 @@ module.exports = { - $id: 'relativeAddressShowSchemaId', + $id: 'personPhoneNumbers', type: 'object', properties: { - foo: { type: 'string' }, - bar: { type: 'string' } + personal: { + type: 'object', + homeNumber: { + $ref: '#/definitions/phoneNumber' + }, + mobilePhoneNumber: { + $ref: '#/definitions/phoneNumber' + } + }, + work: { + $ref: '#/definitions/phoneNumber' + } } }