|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { test } = require('tap') |
| 4 | +const clone = require('rfdc/default') |
| 5 | +const build = require('..') |
| 6 | + |
| 7 | +test('oneOf with $ref should not change the input schema', t => { |
| 8 | + t.plan(2) |
| 9 | + |
| 10 | + const referenceSchema = { |
| 11 | + $id: 'externalId', |
| 12 | + type: 'object', |
| 13 | + properties: { |
| 14 | + name: { type: 'string' } |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + const schema = { |
| 19 | + $id: 'mainSchema', |
| 20 | + type: 'object', |
| 21 | + properties: { |
| 22 | + people: { |
| 23 | + oneOf: [{ $ref: 'externalId' }] |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + const clonedSchema = clone(schema) |
| 28 | + const stringify = build(schema, { |
| 29 | + schema: { |
| 30 | + [referenceSchema.$id]: referenceSchema |
| 31 | + } |
| 32 | + }) |
| 33 | + |
| 34 | + const value = stringify({ people: { name: 'hello', foo: 'bar' } }) |
| 35 | + t.is(value, '{"people":{"name":"hello"}}') |
| 36 | + t.deepEqual(schema, clonedSchema) |
| 37 | +}) |
| 38 | + |
| 39 | +test('oneOf and anyOf with $ref should not change the input schema', t => { |
| 40 | + t.plan(3) |
| 41 | + |
| 42 | + const referenceSchema = { |
| 43 | + $id: 'externalSchema', |
| 44 | + type: 'object', |
| 45 | + properties: { |
| 46 | + name: { type: 'string' } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + const schema = { |
| 51 | + $id: 'rootSchema', |
| 52 | + type: 'object', |
| 53 | + properties: { |
| 54 | + people: { |
| 55 | + oneOf: [{ $ref: 'externalSchema' }] |
| 56 | + }, |
| 57 | + love: { |
| 58 | + anyOf: [ |
| 59 | + { $ref: '#/definitions/foo' }, |
| 60 | + { type: 'boolean' } |
| 61 | + ] |
| 62 | + } |
| 63 | + }, |
| 64 | + definitions: { |
| 65 | + foo: { type: 'string' } |
| 66 | + } |
| 67 | + } |
| 68 | + const clonedSchema = clone(schema) |
| 69 | + const stringify = build(schema, { |
| 70 | + schema: { |
| 71 | + [referenceSchema.$id]: referenceSchema |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + const valueAny1 = stringify({ people: { name: 'hello', foo: 'bar' }, love: 'music' }) |
| 76 | + const valueAny2 = stringify({ people: { name: 'hello', foo: 'bar' }, love: true }) |
| 77 | + |
| 78 | + t.equal(valueAny1, '{"people":{"name":"hello"},"love":"music"}') |
| 79 | + t.equal(valueAny2, '{"people":{"name":"hello"},"love":true}') |
| 80 | + t.deepEqual(schema, clonedSchema) |
| 81 | +}) |
| 82 | + |
| 83 | +test('multiple $ref tree', t => { |
| 84 | + t.plan(2) |
| 85 | + |
| 86 | + const referenceDeepSchema = { |
| 87 | + $id: 'deepId', |
| 88 | + type: 'number' |
| 89 | + } |
| 90 | + |
| 91 | + const referenceSchema = { |
| 92 | + $id: 'externalId', |
| 93 | + type: 'object', |
| 94 | + properties: { |
| 95 | + name: { $ref: '#/definitions/foo' }, |
| 96 | + age: { $ref: 'deepId' } |
| 97 | + }, |
| 98 | + definitions: { |
| 99 | + foo: { type: 'string' } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + const schema = { |
| 104 | + $id: 'mainSchema', |
| 105 | + type: 'object', |
| 106 | + properties: { |
| 107 | + people: { |
| 108 | + oneOf: [{ $ref: 'externalId' }] |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + const clonedSchema = clone(schema) |
| 113 | + const stringify = build(schema, { |
| 114 | + schema: { |
| 115 | + [referenceDeepSchema.$id]: referenceDeepSchema, |
| 116 | + [referenceSchema.$id]: referenceSchema |
| 117 | + } |
| 118 | + }) |
| 119 | + |
| 120 | + const value = stringify({ people: { name: 'hello', foo: 'bar', age: 42 } }) |
| 121 | + t.is(value, '{"people":{"name":"hello","age":42}}') |
| 122 | + t.deepEqual(schema, clonedSchema) |
| 123 | +}) |
0 commit comments