Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/utils/__tests__/__snapshots__/renderSchema.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ Array [
"type": "string",
},
},
"type": "object",
},
Object {
"properties": Object {
"bar": Object {
"type": "string",
},
},
"type": "object",
},
],
"type": "object",
Expand Down Expand Up @@ -63,15 +61,13 @@ Array [
"type": "string",
},
},
"type": "object",
},
Object {
"properties": Object {
"bar": Object {
"type": "string",
},
},
"type": "object",
},
],
"type": "object",
Expand Down
33 changes: 25 additions & 8 deletions src/utils/__tests__/renderSchema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,36 @@ jest.mock('../generateId', () => ({

describe('renderSchema util', () => {
it.each([
['default-schema.json', ''],
['ref/original.json', 'ref/resolved.json'],
['combiner-schema.json', ''],
['array-of-objects.json', ''],
['array-of-refs.json', ''],
['array-of-allofs.json', ''],
['tickets.schema.json', ''],
])('should match %s', (schema, dereferenced) => {
'default-schema.json',
'ref/original.json',
'combiner-schema.json',
'array-of-objects.json',
'array-of-refs.json',
'array-of-allofs.json',
'tickets.schema.json',
])('should match %s', schema => {
expect(
Array.from(renderSchema(JSON.parse(fs.readFileSync(path.resolve(BASE_PATH, schema), 'utf8')))),
).toMatchSnapshot();
});

it.each([
'default-schema.json',
'ref/original.json',
'combiner-schema.json',
'array-of-objects.json',
'array-of-refs.json',
'array-of-allofs.json',
'tickets.schema.json',
])('should not mutate original object %s', schema => {
const content = fs.readFileSync(path.resolve(BASE_PATH, schema), 'utf8');
const input = JSON.parse(content);

Array.from(renderSchema(input));

expect(input).toStrictEqual(JSON.parse(content));
});

it('given schema with complex types, throws', () => {
expect(() =>
Array.from(
Expand Down
7 changes: 5 additions & 2 deletions src/utils/renderSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,13 @@ export const renderSchema: Walker = function*(schema, level = 0, meta = { path:
if (node.properties !== undefined) {
for (const [i, property] of node.properties.entries()) {
if ('type' in node) {
property.type = property.type || node.type;
node.properties[i] = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So is this needed in general? Feels like it might not be.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's needed. If you revert it, a few tests covering mutation will fail.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? For me only two tests fail, both snapshots regarding the "type": "object" thing. Not saying that means everything is good, but just that the tests don't seem to indicate any big issues?

Screen Shot 2019-10-15 at 6 58 00 PM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me only two tests fail,

Yup, these are the only ones, since they cover the combiner children case.

Not saying that means everything is good, but just that the tests don't seem to indicate any big issues?

That's right, seems like this was the only spot.

...property,
type: property.type || node.type,
};
}

yield* renderSchema(property, level + 1, {
yield* renderSchema(node.properties[i], level + 1, {
...(i !== 0 && { divider: DIVIDERS[node.combiner] }),
path: [...path, node.combiner, i],
});
Expand Down
3 changes: 2 additions & 1 deletion src/utils/walk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ function processNode(node: JSONSchema4): SchemaNode | void {
const type = node.type || inferType(node);

if (combiner) {
const properties = node[combiner];
return {
id: generateId(),
combiner,
properties: node[combiner],
properties: Array.isArray(properties) ? properties.slice() : properties,
annotations: getAnnotations(node),
...(type !== undefined && { type }),
} as ICombinerNode;
Expand Down