Skip to content

Commit

Permalink
feat: pass maxRefDepth to tree to limit recursion (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel A. White authored Nov 15, 2023
1 parent 38f4f92 commit c467ab1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
},
"dependencies": {
"@stoplight/json": "^3.20.1",
"@stoplight/json-schema-tree": "^2.2.5",
"@stoplight/json-schema-tree": "^2.3.0",
"@stoplight/react-error-boundary": "^2.0.0",
"@types/json-schema": "^7.0.7",
"classnames": "^2.2.6",
Expand Down
7 changes: 6 additions & 1 deletion src/components/JsonSchemaViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export type JsonSchemaProps = Partial<JSVOptions> & {
emptyText?: string;
className?: string;
resolveRef?: SchemaTreeRefDereferenceFn;
/** Controls the level of recursion of refs. Prevents overly complex trees and running out of stack depth. */
maxRefDepth?: number;
onTreePopulated?: (props: { rootNode: RootNode; nodeCount: number }) => void;
maxHeight?: number;
parentCrumbs?: string[];
Expand Down Expand Up @@ -80,6 +82,7 @@ const JsonSchemaViewerInner = ({
viewMode,
className,
resolveRef,
maxRefDepth,
emptyText = 'No schema defined',
onTreePopulated,
maxHeight,
Expand All @@ -91,6 +94,7 @@ const JsonSchemaViewerInner = ({
| 'viewMode'
| 'className'
| 'resolveRef'
| 'maxRefDepth'
| 'emptyText'
| 'onTreePopulated'
| 'maxHeight'
Expand All @@ -106,6 +110,7 @@ const JsonSchemaViewerInner = ({
const jsonSchemaTree = new JsonSchemaTree(schema, {
mergeAllOf: true,
refResolver: resolveRef,
maxRefDepth,
});

let nodeCount = 0;
Expand Down Expand Up @@ -134,7 +139,7 @@ const JsonSchemaViewerInner = ({
jsonSchemaTreeRoot: jsonSchemaTree.root,
nodeCount,
};
}, [schema, resolveRef, viewMode]);
}, [schema, resolveRef, maxRefDepth, viewMode]);

React.useEffect(() => {
onTreePopulated?.({
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2392,10 +2392,10 @@
json-schema-compare "^0.2.2"
lodash "^4.17.4"

"@stoplight/json-schema-tree@^2.2.5":
version "2.2.5"
resolved "https://registry.yarnpkg.com/@stoplight/json-schema-tree/-/json-schema-tree-2.2.5.tgz#a3f78f3d475b74338f6d2da7ec6733a96cb9819f"
integrity sha512-H2d5EHbWJwbu4h7Eh3R4He4SGlfA5ScucpMqylVTgPwQImvikIyrI+v6oqzpl9fFZ6SkidfTcUs3z1+IKwxWdA==
"@stoplight/json-schema-tree@^2.3.0":
version "2.3.0"
resolved "https://registry.yarnpkg.com/@stoplight/json-schema-tree/-/json-schema-tree-2.3.0.tgz#cdad7f7571700016d8e0e5bad59d287e9bb782d0"
integrity sha512-IH4SYuvV0C4maYQEPftduDBG1qVV4hy5ZXzS9rgq3V7zCT34I7xVwX9Vmpl0mOQTh2IZaTcyPWXkngt21ShJaQ==
dependencies:
"@stoplight/json" "^3.12.0"
"@stoplight/json-schema-merge-allof" "^0.7.8"
Expand Down

0 comments on commit c467ab1

Please sign in to comment.