@@ -19,15 +19,41 @@ class RefResolver {
19
19
this . #cloneSchemaWithoutRefs = opts . cloneSchemaWithoutRefs ?? false
20
20
}
21
21
22
- addSchema ( schema , schemaId ) {
23
- if ( schema . $id !== undefined && schema . $id . charAt ( 0 ) !== '#' ) {
24
- // Schema has an $id that is not an anchor
25
- schemaId = schema . $id
26
- } else {
27
- // Schema has no $id or $id is an anchor
28
- this . #insertSchemaBySchemaId( schema , schemaId )
22
+ addSchema ( schema , rootSchemaId , isRootSchema = true ) {
23
+ if ( isRootSchema ) {
24
+ if ( schema . $id !== undefined && schema . $id . charAt ( 0 ) !== '#' ) {
25
+ // Schema has an $id that is not an anchor
26
+ rootSchemaId = schema . $id
27
+ } else {
28
+ // Schema has no $id or $id is an anchor
29
+ this . #insertSchemaBySchemaId( schema , rootSchemaId )
30
+ }
31
+ }
32
+
33
+ const schemaId = schema . $id
34
+ if ( schemaId !== undefined && typeof schemaId === 'string' ) {
35
+ if ( schemaId . charAt ( 0 ) === '#' ) {
36
+ this . #insertSchemaByAnchor( schema , rootSchemaId , schemaId )
37
+ } else {
38
+ this . #insertSchemaBySchemaId( schema , schemaId )
39
+ rootSchemaId = schemaId
40
+ }
41
+ }
42
+
43
+ const ref = schema . $ref
44
+ if ( ref !== undefined && typeof ref === 'string' ) {
45
+ const { refSchemaId, refJsonPointer } = this . #parseSchemaRef( ref , rootSchemaId )
46
+ this . #schemas[ rootSchemaId ] . refs . push ( {
47
+ schemaId : refSchemaId ,
48
+ jsonPointer : refJsonPointer
49
+ } )
50
+ }
51
+
52
+ for ( const key in schema ) {
53
+ if ( typeof schema [ key ] === 'object' && schema [ key ] !== null ) {
54
+ this . addSchema ( schema [ key ] , rootSchemaId , false )
55
+ }
29
56
}
30
- this . #addSchema( schema , schemaId )
31
57
}
32
58
33
59
getSchema ( schemaId , jsonPointer = '#' ) {
@@ -60,7 +86,11 @@ class RefResolver {
60
86
61
87
for ( const ref of schema . refs ) {
62
88
const dependencySchemaId = ref . schemaId
63
- if ( dependencies [ dependencySchemaId ] !== undefined ) continue
89
+ if (
90
+ dependencySchemaId === schemaId ||
91
+ dependencies [ dependencySchemaId ] !== undefined
92
+ ) continue
93
+
64
94
dependencies [ dependencySchemaId ] = this . getSchema ( dependencySchemaId )
65
95
this . getSchemaDependencies ( dependencySchemaId , dependencies )
66
96
}
@@ -84,12 +114,12 @@ class RefResolver {
84
114
}
85
115
86
116
const refs = [ ]
87
- this . #addDerefSchema( schema . schema , schemaId , refs )
117
+ this . #addDerefSchema( schema . schema , schemaId , true , refs )
88
118
89
119
const dependencies = this . getSchemaDependencies ( schemaId )
90
120
for ( const schemaId in dependencies ) {
91
121
const schema = dependencies [ schemaId ]
92
- this . #addDerefSchema( schema , schemaId , refs )
122
+ this . #addDerefSchema( schema , schemaId , true , refs )
93
123
}
94
124
95
125
for ( const ref of refs ) {
@@ -140,35 +170,18 @@ class RefResolver {
140
170
}
141
171
}
142
172
143
- #addSchema ( schema , rootSchemaId ) {
144
- const schemaId = schema . $id
145
- if ( schemaId !== undefined && typeof schemaId === 'string' ) {
146
- if ( schemaId . charAt ( 0 ) === '#' ) {
147
- this . #insertSchemaByAnchor( schema , rootSchemaId , schemaId )
148
- } else {
149
- this . #insertSchemaBySchemaId( schema , schemaId )
150
- rootSchemaId = schemaId
151
- }
152
- }
153
-
154
- const ref = schema . $ref
155
- if ( ref !== undefined && typeof ref === 'string' ) {
156
- const { refSchemaId, refJsonPointer } = this . #parseSchemaRef( ref , rootSchemaId )
157
- this . #schemas[ rootSchemaId ] . refs . push ( {
158
- schemaId : refSchemaId ,
159
- jsonPointer : refJsonPointer
160
- } )
161
- }
173
+ #addDerefSchema ( schema , rootSchemaId , isRootSchema , refs = [ ] ) {
174
+ const derefSchema = Array . isArray ( schema ) ? [ ...schema ] : { ...schema }
162
175
163
- for ( const key in schema ) {
164
- if ( typeof schema [ key ] === 'object' && schema [ key ] !== null ) {
165
- this . #addSchema( schema [ key ] , rootSchemaId )
176
+ if ( isRootSchema ) {
177
+ if ( schema . $id !== undefined && schema . $id . charAt ( 0 ) !== '#' ) {
178
+ // Schema has an $id that is not an anchor
179
+ rootSchemaId = schema . $id
180
+ } else {
181
+ // Schema has no $id or $id is an anchor
182
+ this . #insertDerefSchemaBySchemaId( derefSchema , rootSchemaId )
166
183
}
167
184
}
168
- }
169
-
170
- #addDerefSchema ( schema , rootSchemaId , refs = [ ] ) {
171
- const derefSchema = Array . isArray ( schema ) ? [ ...schema ] : { ...schema }
172
185
173
186
const schemaId = derefSchema . $id
174
187
if ( schemaId !== undefined && typeof schemaId === 'string' ) {
@@ -191,7 +204,7 @@ class RefResolver {
191
204
for ( const key in derefSchema ) {
192
205
const value = derefSchema [ key ]
193
206
if ( typeof value === 'object' && value !== null ) {
194
- derefSchema [ key ] = this . #addDerefSchema( value , rootSchemaId , refs )
207
+ derefSchema [ key ] = this . #addDerefSchema( value , rootSchemaId , false , refs )
195
208
}
196
209
}
197
210
0 commit comments