|
1 | 1 | 'use strict'
|
2 | 2 |
|
| 3 | +const clone = require('rfdc')({ proto: true }) |
| 4 | + |
3 | 5 | const test = require('tap').test
|
4 | 6 | const build = require('..')
|
5 | 7 |
|
@@ -1822,3 +1824,70 @@ test('ref with same id in properties', (t) => {
|
1822 | 1824 | t.equal(output, '{"_id":"foo","image":{"_id":"bar","name":"hello","owner":"baz"}}')
|
1823 | 1825 | })
|
1824 | 1826 | })
|
| 1827 | + |
| 1828 | +test('Should not modify external schemas', (t) => { |
| 1829 | + t.plan(2) |
| 1830 | + |
| 1831 | + const externalSchema = { |
| 1832 | + uuid: { |
| 1833 | + format: 'uuid', |
| 1834 | + $id: 'UUID', |
| 1835 | + type: 'string' |
| 1836 | + }, |
| 1837 | + Entity: { |
| 1838 | + $id: 'Entity', |
| 1839 | + type: 'object', |
| 1840 | + properties: { |
| 1841 | + id: { $ref: 'UUID' }, |
| 1842 | + id2: { $ref: 'UUID' } |
| 1843 | + } |
| 1844 | + } |
| 1845 | + } |
| 1846 | + |
| 1847 | + const options = { schema: externalSchema } |
| 1848 | + const optionsClone = clone(options) |
| 1849 | + |
| 1850 | + const stringify = build({ $ref: 'Entity' }, options) |
| 1851 | + |
| 1852 | + const data = { id: 'a4e4c954-9f5f-443a-aa65-74d95732249a' } |
| 1853 | + const output = stringify(data) |
| 1854 | + |
| 1855 | + t.equal(output, JSON.stringify(data)) |
| 1856 | + t.same(options, optionsClone) |
| 1857 | +}) |
| 1858 | + |
| 1859 | +test('input schema is not mutated', (t) => { |
| 1860 | + t.plan(3) |
| 1861 | + |
| 1862 | + const schema = { |
| 1863 | + title: 'object with $ref', |
| 1864 | + type: 'object', |
| 1865 | + definitions: { |
| 1866 | + def: { type: 'string' } |
| 1867 | + }, |
| 1868 | + properties: { |
| 1869 | + obj: { |
| 1870 | + $ref: '#/definitions/def' |
| 1871 | + } |
| 1872 | + } |
| 1873 | + } |
| 1874 | + |
| 1875 | + const clonedSchema = JSON.parse(JSON.stringify(schema)) |
| 1876 | + |
| 1877 | + const object = { |
| 1878 | + obj: 'test' |
| 1879 | + } |
| 1880 | + |
| 1881 | + const stringify = build(schema) |
| 1882 | + const output = stringify(object) |
| 1883 | + |
| 1884 | + try { |
| 1885 | + JSON.parse(output) |
| 1886 | + t.pass() |
| 1887 | + } catch (e) { |
| 1888 | + t.fail() |
| 1889 | + } |
| 1890 | + |
| 1891 | + t.equal(output, '{"obj":"test"}') |
| 1892 | + t.same(schema, clonedSchema) |
| 1893 | +}) |
0 commit comments