@@ -4,10 +4,10 @@ import { JSONSchema, JSONSchemaObject, PatternProperties } from "@json-schema-to
4
4
* Signature of the mutation method passed to traverse.
5
5
*
6
6
* @param schema The schema or subschema node being traversed
7
- * @param isRootOfCycle false if the schema passed is not the root of a detected cycle. Useful for special handling of cycled schemas.
7
+ * @param isCycle false if the schema passed is not the root of a detected cycle. Useful for special handling of cycled schemas.
8
8
* @param path json path string separated by periods
9
9
*/
10
- export type MutationFunction = ( schema : JSONSchema , isRootOfCycle : boolean , path : string , ) => JSONSchema ;
10
+ export type MutationFunction = ( schema : JSONSchema , isCycle : boolean , path : string , ) => JSONSchema ;
11
11
12
12
/**
13
13
* The options you can use when traversing.
@@ -74,8 +74,8 @@ export default function traverse(
74
74
recursiveStack : JSONSchema [ ] = [ ] ,
75
75
pathStack : string [ ] = [ ] ,
76
76
prePostMap : Array < [ JSONSchema , JSONSchema ] > = [ ] ,
77
+ cycleSet : JSONSchema [ ] = [ ] ,
77
78
) : JSONSchema {
78
- let isRootOfCycle = false ;
79
79
const opts = { ...defaultOptions , ...traverseOptions } ; // would be nice to make an 'entry' func when we get around to optimizations
80
80
81
81
// booleans are a bit messed. Since all other schemas are objects (non-primitive type
@@ -112,7 +112,7 @@ export default function traverse(
112
112
const rec = ( s : JSONSchema , path : string [ ] ) : JSONSchema => {
113
113
const foundCycle = isCycle ( s , recursiveStack ) ;
114
114
if ( foundCycle ) {
115
- if ( foundCycle === schema ) { isRootOfCycle = true ; }
115
+ cycleSet . push ( foundCycle ) ;
116
116
117
117
// if the cycle is a ref to the root schema && skipFirstMutation is try we need to call mutate.
118
118
// If we don't, it will never happen.
@@ -127,6 +127,7 @@ export default function traverse(
127
127
return cycledMutableSchema ;
128
128
}
129
129
130
+ // else
130
131
return traverse (
131
132
s ,
132
133
mutation ,
@@ -135,21 +136,22 @@ export default function traverse(
135
136
recursiveStack ,
136
137
path ,
137
138
prePostMap ,
139
+ cycleSet ,
138
140
) ;
139
141
} ;
140
142
141
143
if ( schema . anyOf ) {
142
- mutableSchema . anyOf = schema . anyOf . map ( ( x , i ) => {
144
+ mutableSchema . anyOf = schema . anyOf . map ( ( x , i ) => {
143
145
const result = rec ( x , [ ...pathStack , "anyOf" , i . toString ( ) ] ) ;
144
146
return result ;
145
147
} ) ;
146
148
} else if ( schema . allOf ) {
147
- mutableSchema . allOf = schema . allOf . map ( ( x , i ) => {
149
+ mutableSchema . allOf = schema . allOf . map ( ( x , i ) => {
148
150
const result = rec ( x , [ ...pathStack , "allOf" , i . toString ( ) ] ) ;
149
151
return result ;
150
152
} ) ;
151
153
} else if ( schema . oneOf ) {
152
- mutableSchema . oneOf = schema . oneOf . map ( ( x , i ) => {
154
+ mutableSchema . oneOf = schema . oneOf . map ( ( x , i ) => {
153
155
const result = rec ( x , [ ...pathStack , "oneOf" , i . toString ( ) ] ) ;
154
156
return result ;
155
157
} ) ;
@@ -158,14 +160,14 @@ export default function traverse(
158
160
159
161
if ( schema . items ) {
160
162
if ( schema . items instanceof Array ) {
161
- mutableSchema . items = schema . items . map ( ( x , i ) => {
163
+ mutableSchema . items = schema . items . map ( ( x , i ) => {
162
164
const result = rec ( x , [ ...pathStack , "items" , i . toString ( ) ] ) ;
163
165
return result ;
164
166
} ) ;
165
167
} else {
166
168
const foundCycle = isCycle ( schema . items , recursiveStack ) ;
167
169
if ( foundCycle ) {
168
- if ( foundCycle === schema ) { isRootOfCycle = true ; }
170
+ cycleSet . push ( foundCycle ) ;
169
171
170
172
if ( opts . skipFirstMutation === true && foundCycle === recursiveStack [ 0 ] ) {
171
173
mutableSchema . items = mutation ( schema . items , true , jsonPathStringify ( pathStack ) ) ;
@@ -186,6 +188,7 @@ export default function traverse(
186
188
recursiveStack ,
187
189
pathStack ,
188
190
prePostMap ,
191
+ cycleSet ,
189
192
) ;
190
193
}
191
194
}
@@ -229,6 +232,7 @@ export default function traverse(
229
232
if ( opts . bfs === true ) {
230
233
return mutableSchema ;
231
234
} else {
232
- return mutation ( mutableSchema , isRootOfCycle , jsonPathStringify ( pathStack ) ) ;
235
+ const isCycle = cycleSet . indexOf ( schema ) !== - 1
236
+ return mutation ( mutableSchema , isCycle , jsonPathStringify ( pathStack ) ) ;
233
237
}
234
238
}
0 commit comments