Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 44f1a83

Browse files
committedMay 16, 2024
Fix the way we stop recursion
1 parent a8effba commit 44f1a83

File tree

2 files changed

+276492
-15799
lines changed

2 files changed

+276492
-15799
lines changed
 

‎utils/generate-schema.ts

+31-18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ import type {Config, SchemaGenerator} from "ts-json-schema-generator";
99

1010
const RefNameExtractorRegex = /#\/definitions\/(?<name>.*)/;
1111

12+
const isRefObject = (obj) => {
13+
return (
14+
obj != null &&
15+
typeof obj === "object" &&
16+
!Array.isArray(obj) &&
17+
"$ref" in obj
18+
);
19+
};
20+
1221
const getRefDefinitionName = (ref: string) => {
1322
const match = ref.match(RefNameExtractorRegex);
1423
if (match) {
@@ -17,24 +26,27 @@ const getRefDefinitionName = (ref: string) => {
1726
};
1827

1928
function derefSchema(schema: ReturnType<SchemaGenerator["createSchema"]>) {
20-
const derefIfRef = (level, key, obj) => {
29+
const derefIfRef = (level, obj) => {
2130
try {
22-
if (
23-
obj != null &&
24-
typeof obj === "object" &&
25-
!Array.isArray(obj) &&
26-
"$ref" in obj
27-
) {
31+
if (isRefObject(obj)) {
2832
const defName = getRefDefinitionName(obj["$ref"]);
2933

3034
if (defName != null) {
31-
return schema.definitions?.[defName];
35+
const def = derefIfRef(
36+
level + 1,
37+
schema.definitions?.[defName],
38+
);
39+
40+
// Now clone the node
41+
return JSON.parse(JSON.stringify(def));
3242
}
3343
}
3444
} catch (e) {
35-
console.log(key, obj);
45+
console.log(level, obj);
3646
throw e;
3747
}
48+
49+
return obj;
3850
};
3951

4052
const processProps = (path: string[], props, level: number = 0) => {
@@ -45,31 +57,32 @@ function derefSchema(schema: ReturnType<SchemaGenerator["createSchema"]>) {
4557
if (Array.isArray(props)) {
4658
for (let i = 0; i < props.length; i++) {
4759
const newPath = [...path, `[${i}]`];
48-
const refTarget = derefIfRef(level, i, props[i]);
49-
if (refTarget) {
50-
props[i] = refTarget;
51-
}
60+
props[i] = derefIfRef(level, props[i]);
5261

5362
processProps(newPath, props[i], level + 1);
5463
}
5564
} else if (typeof props === "object") {
5665
for (const [key, val] of Object.entries(props)) {
5766
const newPath = [...path, key];
58-
const refTarget = derefIfRef(level, key, val);
59-
if (refTarget) {
60-
props[key] = refTarget;
61-
}
67+
props[key] = derefIfRef(level, val);
6268

6369
// Stop recursion of nested Renderers
64-
if (key === "widgets" && path.indexOf("widgets") !== -1) {
70+
if (
71+
(key === "widgets" &&
72+
path.find((item) => item === "widgets")?.length) ??
73+
0 > 3
74+
) {
75+
console.log(newPath.join("."));
6576
delete props[key];
77+
// props[key] = newPath.join(".");
6678
continue;
6779
}
6880
processProps(newPath, props[key], level + 1);
6981
}
7082
}
7183
};
7284

85+
processProps(["root"], schema.definitions);
7386
processProps(["root"], schema.properties);
7487
processProps(["root"], schema.additionalProperties);
7588

0 commit comments

Comments
 (0)
Please sign in to comment.