Skip to content

Commit 985268c

Browse files
author
Jakub Jankowski
authored
fix: eager resolving errors (#81)
* fix: handle eager resolving errors * test: handle eagerresolving errors
1 parent 7837eb3 commit 985268c

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

src/tree/__tests__/tree.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,35 @@ describe('SchemaTree', () => {
11311131
"
11321132
`);
11331133
});
1134+
1135+
test('should handle resolving errors', () => {
1136+
const schema: JSONSchema4 = {
1137+
type: 'object',
1138+
properties: {
1139+
foo: {
1140+
type: 'string',
1141+
},
1142+
bar: {
1143+
$ref: 'http://localhost:8080/some/not/existing/path',
1144+
},
1145+
},
1146+
};
1147+
1148+
const tree = new SchemaTree(schema, new SchemaTreeState(), {
1149+
expandedDepth: Infinity,
1150+
mergeAllOf: true,
1151+
resolveRef: () => {
1152+
throw new Error('resolving error');
1153+
},
1154+
shouldResolveEagerly: true,
1155+
onPopulate: void 0,
1156+
});
1157+
1158+
tree.populate();
1159+
1160+
expect(tree.count).toEqual(4);
1161+
expect(getNodeMetadata(tree.itemAt(3)!)).toHaveProperty('error', 'resolving error');
1162+
});
11341163
});
11351164

11361165
describe('onPopulate handler', () => {

src/tree/utils/populateTree.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import { JsonPath, Optional } from '@stoplight/types';
33
import { JSONSchema4 } from 'json-schema';
44
import { isObject as _isObject } from 'lodash';
55
import { IArrayNode, IObjectNode, SchemaKind, SchemaNode, SchemaTreeListNode } from '../../types';
6+
import { generateId } from '../../utils/generateId';
67
import { getCombiner } from '../../utils/getCombiner';
78
import { getPrimaryType } from '../../utils/getPrimaryType';
89
import { isCombinerNode, isRefNode } from '../../utils/guards';
910
import { getNodeMetadata, getSchemaNodeMetadata, metadataStore } from '../metadata';
1011
import { createErrorTreeNode } from './createErrorTreeNode';
1112
import { mergeAllOf } from './mergeAllOf';
12-
import { walk } from './walk';
13+
import { processNode, walk } from './walk';
1314

1415
export type WalkerRefResolver = (path: JsonPath | null, $ref: string) => JSONSchema4;
1516

@@ -245,7 +246,22 @@ function prepareSchema(
245246
try {
246247
return resolveSchema(schema, path, options);
247248
} catch (ex) {
248-
(node as TreeListParentNode).children = [];
249-
return void (node as TreeListParentNode).children.push(createErrorTreeNode(node as TreeListParentNode, ex.message));
249+
const treeNode: SchemaTreeListNode = {
250+
id: generateId(),
251+
name: '',
252+
parent: node as TreeListParentNode,
253+
children: [],
254+
};
255+
256+
(node as TreeListParentNode).children.push(treeNode);
257+
metadataStore.set(treeNode, {
258+
schemaNode: processNode(schema || {}),
259+
schema: schema || {},
260+
path,
261+
});
262+
263+
treeNode.children.push(createErrorTreeNode(treeNode, ex.message));
264+
265+
return null;
250266
}
251267
}

src/tree/utils/walk.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function assignNodeSpecificFields(base: IBaseNode, node: JSONSchema4) {
2929
}
3030
}
3131

32-
function processNode(node: JSONSchema4): SchemaNode | void {
32+
export function processNode(node: JSONSchema4): SchemaNode {
3333
const combiner = getCombiner(node);
3434
const type = node.type || inferType(node);
3535
const title = typeof node.title === 'string' ? { title: node.title } : null;

0 commit comments

Comments
 (0)