|
| 1 | + |
| 2 | +const { test } = require('tap') |
| 3 | +const URI = require('uri-js') |
| 4 | +const RefResolver = require('../ref-resolver') |
| 5 | + |
| 6 | +test('resolving schema within the $id', t => { |
| 7 | + t.plan(2) |
| 8 | + |
| 9 | + const schema = { |
| 10 | + description: 'Succesful response', |
| 11 | + type: 'object', |
| 12 | + properties: { |
| 13 | + greetings: { $ref: 'greetings#' } |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + const opts = { |
| 18 | + applicationUri: 'one-single-uri.to', |
| 19 | + externalSchemas: [ |
| 20 | + { |
| 21 | + $id: 'greetings', |
| 22 | + type: 'object', |
| 23 | + properties: { |
| 24 | + hello: { type: 'string' } |
| 25 | + } |
| 26 | + } |
| 27 | + ], |
| 28 | + buildLocalReference (json, baseUri, fragment, i) { |
| 29 | + return baseUri.path |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + const resolver = RefResolver(opts) |
| 34 | + const out = resolver.resolve(schema) |
| 35 | + const externalDef = resolver.definitions() |
| 36 | + t.equal(out.properties.greetings.$ref, '#/definitions/greetings') |
| 37 | + t.same(externalDef.definitions.greetings, opts.externalSchemas[0]) |
| 38 | +}) |
| 39 | + |
| 40 | +test('resolving schema within the $id case #2', t => { |
| 41 | + t.plan(3) |
| 42 | + |
| 43 | + const schema = { |
| 44 | + $id: 'urn:schema:ref', |
| 45 | + type: 'object', |
| 46 | + properties: { |
| 47 | + hello: { $ref: 'urn:schema:base#/definitions/hello' } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + const opts = { |
| 52 | + applicationUri: schema.$id, |
| 53 | + externalSchemas: [ |
| 54 | + { |
| 55 | + $id: 'urn:schema:base', |
| 56 | + definitions: { hello: { type: 'string' } }, |
| 57 | + type: 'object', |
| 58 | + properties: { hello: { $ref: '#/definitions/hello' } } |
| 59 | + }, |
| 60 | + schema |
| 61 | + ], |
| 62 | + buildLocalReference (json, baseUri, fragment, i) { |
| 63 | + return escape(json.$id) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + const resolver = RefResolver(opts) |
| 68 | + const out = resolver.resolve(schema) |
| 69 | + t.equal(out.properties.hello.$ref, '#/definitions/urn%3Aschema%3Abase/definitions/hello') |
| 70 | + |
| 71 | + const externalDef = resolver.definitions() |
| 72 | + t.ok(externalDef.definitions['urn%3Aschema%3Abase'], 'buildLocalReference result') |
| 73 | + t.ok(externalDef.definitions['urn%3Aschema%3Aref'], 'buildLocalReference result') |
| 74 | +}) |
| 75 | + |
| 76 | +test('resolving schema within the $id relative', t => { |
| 77 | + t.plan(4) |
| 78 | + |
| 79 | + const schema = { |
| 80 | + type: 'object', |
| 81 | + properties: { |
| 82 | + user: { $ref: 'http://hello.absolute/user.json#/definitions/name' }, |
| 83 | + address: { $ref: 'adr#/definitions/street' } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + const opts = { |
| 88 | + applicationUri: 'http://hello.relative', |
| 89 | + externalSchemas: [ |
| 90 | + { |
| 91 | + $id: 'http://hello.absolute/user.json', |
| 92 | + type: 'object', |
| 93 | + definitions: { name: { type: 'string' } } |
| 94 | + }, |
| 95 | + { |
| 96 | + $id: 'adr', |
| 97 | + type: 'object', |
| 98 | + definitions: { street: { type: 'string' } } |
| 99 | + } |
| 100 | + ], |
| 101 | + buildLocalReference (json, baseUri, fragment, i) { |
| 102 | + t.equal(URI.serialize(baseUri), baseUriCheck.shift()) |
| 103 | + return escape(json.$id) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + const baseUriCheck = [ |
| 108 | + opts.externalSchemas[0].$id, |
| 109 | + `${opts.applicationUri}/${opts.externalSchemas[1].$id}` |
| 110 | + ] |
| 111 | + |
| 112 | + const resolver = RefResolver(opts) |
| 113 | + const out = resolver.resolve(schema) |
| 114 | + t.equal(out.properties.user.$ref, '#/definitions/http%3A//hello.absolute/user.json/definitions/name') |
| 115 | + t.equal(out.properties.address.$ref, '#/definitions/adr/definitions/street') |
| 116 | +}) |
0 commit comments