8000 add tests for #350 and #417 · fastify/fast-json-stringify@ac7fd4d · GitHub
[go: up one dir, main page]

Skip to content

Commit ac7fd4d

Browse files
add tests for #350 and #417
1 parent c85cc87 commit ac7fd4d

File tree

1 file changed

+327
-0
lines changed

1 file changed

+327
-0
lines changed

test/ref.test.js

Lines changed: 327 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,3 +1428,330 @@ test('Regression 2.5.2', t => {
14281428

14291429
t.equal(output, '[{"field":"parent","sub":{"field":"joined"}}]')
14301430
})
1431+
1432+
test('Reference through multiple definitions', (t) => {
1433+
t.plan(2)
1434+
1435+
const schema = {
1436+
$ref: '#/definitions/A',
1437+
definitions: {
1438+
A: {
1439+
type: 'object',
1440+
additionalProperties: false,
1441+
properties: { a: { anyOf: [{ $ref: '#/definitions/B' }] } },
1442+
required: ['a']
1443+
},
1444+
B: {
1445+
type: 'object',
1446+
properties: { b: { anyOf: [{ $ref: '#/definitions/C' }] } },
1447+
required: ['b'],
1448+
additionalProperties: false
1449+
},
1450+
C: {
1451+
type: 'object',
1452+
properties: { c: { type: 'string', const: 'd' } },
1453+
required: ['c'],
1454+
additionalProperties: false
1455+
}
1456+
}
1457+
}
1458+
1459+
const object = { a: { b: { c: 'd' } } }
1460+
1461+
const stringify = build(schema)
1462+
const output = stringify(object)
1463+
1464+
JSON.parse(output)
1465+
t.pass()
1466+
1467+
t.equal(output, JSON.stringify(object))
1468+
})
1469+
1470+
test('issue #350', (t) => {
1471+
t.plan(2)
1472+
1473+
const schema = {
1474+
title: 'Example Schema',
1475+
type: 'object',
1476+
properties: {
1477+
firstName: { $ref: '#foo' },
1478+
lastName: { $ref: '#foo' },
1479+
nested: {
1480+
type: 'object',
1481+
properties: {
1482+
firstName: { $ref: '#foo' },
1483+
lastName: { $ref: '#foo' }
1484+
}
1485+
}
1486+
},
1487+
definitions: {
1488+
foo: {
1489+
$id: '#foo',
1490+
type: 'string'
1491+
}
1492+
}
1493+
}
1494+
1495+
const object = {
1496+
firstName: 'Matteo',
1497+
lastName: 'Collina',
1498+
nested: {
1499+
firstName: 'Matteo',
1500+
lastName: 'Collina'
1501+
}
1502+
}
1503+
1504+
const stringify = build(schema)
1505+
const output = stringify(object)
1506+
1507+
JSON.parse(output)
1508+
t.pass()
1509+
1510+
t.equal(output, JSON.stringify(object))
1511+
})
1512+
1513+
test('deep union type', (t) => {
1514+
const stringify = build({
1515+
schema: {
1516+
type: 'array',
1517+
items: {
1518+
oneOf: [
1519+
{
1520+
$ref: 'components#/schemas/IDirectory'
1521+
},
1522+
{
1523+
$ref: 'components#/schemas/IImageFile'
1524+
},
1525+
{
1526+
$ref: 'components#/schemas/ITextFile'
1527+
},
1528+
{
1529+
$ref: 'components#/schemas/IZipFile'
1530+
}
1531+
]
1532+
},
1533+
nullable: false
1534+
},
1535+
components: {
1536+
schemas: {
1537+
IDirectory: {
1538+
$id: 'IDirectory',
1539+
$recursiveAnchor: true,
1540+
type: 'object',
1541+
properties: {
1542+
children: {
1543+
type: 'array',
1544+
items: {
1545+
oneOf: [
1546+
{
1547+
$recursiveRef: '#'
1548+
},
1549+
{
1550+
$ref: 'components#/schemas/IImageFile'
1551+
},
1552+
{
1553+
$ref: 'components#/schemas/ITextFile'
1554+
},
1555+
{
1556+
$ref: 'components#/schemas/IZipFile'
1557+
}
1558+
]
1559+
},
1560+
nullable: false
1561+
},
1562+
type: {
1563+
type: 'string',
1564+
nullable: false
1565+
},
1566+
id: {
1567+
type: 'string',
1568+
nullable: false
1569+
},
1570+
name: {
1571+
type: 'string',
1572+
nullable: false
1573+
}
1574+
},
1575+
nullable: false,
1576+
required: [
1577+
'children',
1578+
'type',
1579+
'id',
1580+
'name'
1581+
]
1582+
} 10000 ,
1583+
IImageFile: {
1584+
$id: 'IImageFile',
1585+
type: 'object',
1586+
properties: {
1587+
width: {
1588+
type: 'number',
1589+
nullable: false
1590+
},
1591+
height: {
1592+
type: 'number',
1593+
nullable: false
1594+
},
1595+
url: {
1596+
type: 'string',
1597+
nullable: false
1598+
},
1599+
extension: {
1600+
type: 'string',
1601+
nullable: false
1602+
},
1603+
size: {
1604+
type: 'number',
1605+
nullable: false
1606+
},
1607+
type: {
1608+
type: 'string',
1609+
nullable: false
1610+
},
1611+
id: {
1612+
type: 'string',
1613+
nullable: false
1614+
},
1615+
name: {
1616+
type: 'string',
1617+
nullable: false
1618+
}
1619+
},
1620+
nullable: false,
1621+
required: [
1622+
'width',
1623+
'height',
1624+
'url',
1625+
'extension',
1626+
'size',
1627+
'type',
1628+
'id',
1629+
'name'
1630+
]
1631+
},
1632+
ITextFile: {
1633+
$id: 'ITextFile',
1634+
type: 'object',
1635+
properties: {
1636+
content: {
1637+
type: 'string',
1638+
nullable: false
1639+
},
1640+
extension: {
1641+
type: 'string',
1642+
nullable: false
1643+
},
1644+
size: {
1645+
type: 'number',
1646+
nullable: false
1647+
},
1648+
type: {
1649+
type: 'string',
1650+
nullable: false
1651+
},
1652+
id: {
1653+
type: 'string',
1654+
nullable: false
1655+
},
1656+
name: {
1657+
type: 'string',
1658+
nullable: false
1659+
}
1660+
},
1661+
nullable: false,
1662+
required: [
1663+
'content',
1664+
'extension',
1665+
'size',
1666+
'type',
1667+
'id',
1668+
'name'
1669+
]
1670+
},
1671+
IZipFile: {
1672+
$id: 'IZipFile',
1673+
type: 'object',
1674+
properties: {
1675+
files: {
1676+
type: 'number',
1677+
nullable: false
1678+
},
1679+
extension: {
1680+
type: 'string',
1681+
nullable: false
1682+
},
1683+
size: {
1684+
type: 'number',
1685+
nullable: false
1686+
},
1687+
type: {
1688+
type: 'string',
1689+
nullable: false
1690+
},
1691+
id: {
1692+
type: 'string',
1693+
nullable: false
1694+
},
1695+
name: {
1696+
type: 'string',
1697+
nullable: false
1698+
}
1699+
},
1700+
nullable: false,
1701+
required: [
1702+
'files',
1703+
'extension',
1704+
'size',
1705+
'type',
1706+
'id',
1707+
'name'
1708+
]
1709+
}
1710+
}
1711+
}
1712+
})
1713+
1714+
const obj = [
1715+
{
1716+
type: 'directory',
1717+
id: '7b1068a4-dd6e-474a-8d85-09a2d77639cb',
1718+
name: 'ixcWGOKI',
1719+
children: [
1720+
{
1721+
type: 'directory',
1722+
id: '5883e17c-b207-46d4-ad2d-be72249711ce',
1723+
name: 'vecQwFGS',
1724+
children: []
1725+
},
1726+
{
1727+
type: 'file',
1728+
id: '670b6556-a610-4a48-8a16-9c2da97a0d18',
1729+
name: 'eStFddzX',
1730+
extension: 'jpg',
1731+
size: 7,
1732+
width: 300,
1733+
height: 1200,
1734+
url: 'https://github.com/samchon/typescript-json'
1735+
},
1736+
{
1737+
type: 'file',
1738+
id: '85dc796d-9593-4833-b1a1-addc8ebf74ea',
1739+
name: 'kTdUfwRJ',
1740+
extension: 'ts',
1741+
size: 86,
1742+
content: 'console.log("Hello world");'
1743+
},
1744+
{
1745+
type: 'file',
1746+
id: '8933c86a-7a1e-4d4a-b0a6-17d6896fdf89',
1747+
name: 'NBPkefUG',
1748+
extension: 'zip',
1749+
size: 22,
1750+
files: 20
1751+
}
1752+
]
1753+
}
1754+
]
1755+
t.equal(JSON.stringify(obj), stringify(obj))
1756+
t.autoend()
1757+
})

0 commit comments

Comments
 (0)
0