-
-
Notifications
You must be signed in to change notification settings - Fork 208
support for recursion in arrays #363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
'use strict' | ||
|
||
const test = require('tap').test | ||
const build = require('..') | ||
|
||
test('can stringify recursive directory tree (issue #181)', (t) => { | ||
t.plan(1) | ||
|
||
const schema = { | ||
definitions: { | ||
directory: { | ||
type: 'object', | ||
properties: { | ||
name: { type: 'string' }, | ||
subDirectories: { | ||
type: 'array', | ||
items: { $ref: '#/definitions/directory' }, | ||
default: [] | ||
} | ||
} | ||
} | ||
}, | ||
type: 'array', | ||
items: { $ref: '#/definitions/directory' } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a test using external schemas? I think a map track only one ref when there are conflicts There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! Fixed issue with conflicts and added corresponding tests |
||
} | ||
const stringify = build(schema) | ||
|
||
t.equal(stringify([ | ||
{ name: 'directory 1', subDirectories: [] }, | ||
{ | ||
name: 'directory 2', | ||
subDirectories: [ | ||
{ name: 'directory 2.1', subDirectories: [] }, | ||
{ name: 'directory 2.2', subDirectories: [] } | ||
] | ||
} | ||
]), '[{"name":"directory 1","subDirectories":[]},{"name":"directory 2","subDirectories":[{"name":"directory 2.1","subDirectories":[]},{"name":"directory 2.2","subDirectories":[]}]}]') | ||
}) | ||
|
||
test('can stringify when recursion in external schema', t => { | ||
t.plan(1) | ||
|
||
const referenceSchema = { | ||
$id: 'person', | ||
type: 'object', | ||
properties: { | ||
name: { type: 'string' }, | ||
children: { | ||
type: 'array', | ||
items: { $ref: '#' } | ||
} | ||
} | ||
} | ||
|
||
const schema = { | ||
$id: 'mainSchema', | ||
type: 'object', | ||
properties: { | ||
people: { | ||
$ref: 'person' | ||
} | ||
} | ||
} | ||
const stringify = build(schema, { | ||
schema: { | ||
[referenceSchema.$id]: referenceSchema | ||
} | ||
}) | ||
|
||
const value = stringify({ people: { name: 'Elizabeth', children: [{ name: 'Charles' }] } }) | ||
t.equal(value, '{"people":{"name":"Elizabeth","children":[{"name":"Charles"}]}}') | ||
}) | ||
|
||
test('use proper serialize function', t => { | ||
t.plan(1) | ||
|
||
const personSchema = { | ||
$id: 'person', | ||
type: 'object', | ||
properties: { | ||
name: { type: 'string' }, | ||
children: { | ||
type: 'array', | ||
items: { $ref: '#' } | ||
} | ||
} | ||
} | ||
|
||
const directorySchema = { | ||
$id: 'directory', | ||
type: 'object', | ||
properties: { | ||
name: { type: 'string' }, | ||
subDirectories: { | ||
type: 'array', | ||
items: { $ref: '#' }, | ||
default: [] | ||
} | ||
} | ||
} | ||
|
||
const schema = { | ||
$id: 'mainSchema', | ||
type: 'object', | ||
properties: { | ||
people: { $ref: 'person' }, | ||
directory: { $ref: 'directory' } | ||
} | ||
} | ||
const stringify = build(schema, { | ||
schema: { | ||
[personSchema.$id]: personSchema, | ||
[directorySchema.$id]: directorySchema | ||
} | ||
}) | ||
|
||
const value = stringify({ | ||
people: { | ||
name: 'Elizabeth', | ||
children: [{ | ||
name: 'Charles', | ||
children: [{ name: 'William', children: [{ name: 'George' }, { name: 'Charlotte' }] }, { name: 'Harry' }] | ||
}] | ||
}, | ||
directory: { | ||
name: 'directory 1', | ||
subDirectories: [ | ||
{ name: 'directory 1.1', subDirectories: [] }, | ||
{ | ||
name: 'directory 1.2', | ||
subDirectories: [{ name: 'directory 1.2.1' }, { name: 'directory 1.2.2' }] | ||
} | ||
] | ||
} | ||
}) | ||
t.equal(value, '{"people":{"name":"Elizabeth","children":[{"name":"Charles","children":[{"name":"William","children":[{"name":"George"},{"name":"Charlotte"}]},{"name":"Harry"}]}]},"directory":{"name":"directory 1","subDirectories":[{"name":"directory 1.1","subDirectories":[]},{"name":"directory 1.2","subDirectories":[{"name":"directory 1.2.1","subDirectories":[]},{"name":"directory 1.2.2","subDirectories":[]}]}]}}') | ||
}) |
Uh oh!
There was an error while loading. Please reload this page.