@@ -9,6 +9,15 @@ import type {Config, SchemaGenerator} from "ts-json-schema-generator";
9
9
10
10
const RefNameExtractorRegex = / # \/ d e f i n i t i o n s \/ (?< name > .* ) / ;
11
11
12
+ const isRefObject = ( obj ) => {
13
+ return (
14
+ obj != null &&
15
+ typeof obj === "object" &&
16
+ ! Array . isArray ( obj ) &&
17
+ "$ref" in obj
18
+ ) ;
19
+ } ;
20
+
12
21
const getRefDefinitionName = ( ref : string ) => {
13
22
const match = ref . match ( RefNameExtractorRegex ) ;
14
23
if ( match ) {
@@ -17,24 +26,27 @@ const getRefDefinitionName = (ref: string) => {
17
26
} ;
18
27
19
28
function derefSchema ( schema : ReturnType < SchemaGenerator [ "createSchema" ] > ) {
20
- const derefIfRef = ( level , key , obj ) => {
29
+ const derefIfRef = ( level , obj ) => {
21
30
try {
22
- if (
23
- obj != null &&
24
- typeof obj === "object" &&
25
- ! Array . isArray ( obj ) &&
26
- "$ref" in obj
27
- ) {
31
+ if ( isRefObject ( obj ) ) {
28
32
const defName = getRefDefinitionName ( obj [ "$ref" ] ) ;
29
33
30
34
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 ) ) ;
32
42
}
33
43
}
34
44
} catch ( e ) {
35
- console . log ( key , obj ) ;
45
+ console . log ( level , obj ) ;
36
46
throw e ;
37
47
}
48
+
49
+ return obj ;
38
50
} ;
39
51
40
52
const processProps = ( path : string [ ] , props , level : number = 0 ) => {
@@ -45,31 +57,32 @@ function derefSchema(schema: ReturnType<SchemaGenerator["createSchema"]>) {
45
57
if ( Array . isArray ( props ) ) {
46
58
for ( let i = 0 ; i < props . length ; i ++ ) {
47
59
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 ] ) ;
52
61
53
62
processProps ( newPath , props [ i ] , level + 1 ) ;
54
63
}
55
64
} else if ( typeof props === "object" ) {
56
65
for ( const [ key , val ] of Object . entries ( props ) ) {
57
66
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 ) ;
62
68
63
69
// 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 ( "." ) ) ;
65
76
delete props [ key ] ;
77
+ // props[key] = newPath.join(".");
66
78
continue ;
67
79
}
68
80
processProps ( newPath , props [ key ] , level + 1 ) ;
69
81
}
70
82
}
71
83
} ;
72
84
85
+ processProps ( [ "root" ] , schema . definitions ) ;
73
86
processProps ( [ "root" ] , schema . properties ) ;
74
87
processProps ( [ "root" ] , schema . additionalProperties ) ;
75
88
0 commit comments