Skip to content

Commit 6477322

Browse files
authored
Merge pull request #314 from json-schema-tools/feat/iscycle-working-and-deps
Feat/iscycle working and deps
2 parents 1a5bbee + 3fdca68 commit 6477322

File tree

4 files changed

+148
-52
lines changed

4 files changed

+148
-52
lines changed

package-lock.json

Lines changed: 108 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929
"devDependencies": {
3030
"@json-schema-tools/meta-schema": "^1.6.10",
3131
"@types/jest": "^26.0.23",
32-
"@types/node": "^15.12.2",
33-
"@typescript-eslint/eslint-plugin": "^4.26.1",
34-
"@typescript-eslint/parser": "^4.26.1",
32+
"@types/node": "^15.12.3",
33+
"@typescript-eslint/eslint-plugin": "^4.27.0",
34+
"@typescript-eslint/parser": "^4.27.0",
3535
"eslint": "^7.28.0",
3636
"jest": "^26.6.3",
3737
"ts-jest": "^26.5.6",
38-
"typedoc": "^0.20.36",
38+
"typedoc": "^0.20.37",
3939
"typescript": "4.2.4"
4040
},
4141
"dependencies": {}

src/index.test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ describe("traverse", () => {
415415
title: "6",
416416
type: "object",
417417
allOf: [
418-
{ title: "7", type: "object", properties: { baz: { title: "8" } } },
418+
{ title: "7", type: "object", properties: { baz: { title: "5" } } },
419419
],
420420
},
421421
},
@@ -573,6 +573,27 @@ describe("traverse", () => {
573573
expect(mockMutation).toHaveBeenCalledWith(testSchema, true, "");
574574
expect(mockMutation).not.toHaveBeenCalledWith(testSchema, false, "");
575575
});
576+
577+
it("true when the cycle is inside oneOf", () => {
578+
const testSchema = {
579+
title: "a",
580+
oneOf: [{
581+
title: "b",
582+
type: "object",
583+
properties: {
584+
a: {}
585+
}
586+
}]
587+
};
588+
testSchema.oneOf[0].properties.a = testSchema;
589+
590+
const mockMutation = jest.fn((mockS) => mockS);
591+
592+
traverse(testSchema, mockMutation, { mutable: false });
593+
594+
expect(mockMutation).toHaveBeenCalledWith(testSchema, true, "");
595+
expect(mockMutation).not.toHaveBeenCalledWith(testSchema, false, "");
596+
});
576597
});
577598

578599
describe("bfs", () => {

src/index.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { JSONSchema, JSONSchemaObject, PatternProperties } from "@json-schema-to
44
* Signature of the mutation method passed to traverse.
55
*
66
* @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.
88
* @param path json path string separated by periods
99
*/
10-
export type MutationFunction = (schema: JSONSchema, isRootOfCycle: boolean, path: string, ) => JSONSchema;
10+
export type MutationFunction = (schema: JSONSchema, isCycle: boolean, path: string,) => JSONSchema;
1111

1212
/**
1313
* The options you can use when traversing.
@@ -74,8 +74,8 @@ export default function traverse(
7474
recursiveStack: JSONSchema[] = [],
7575
pathStack: string[] = [],
7676
prePostMap: Array<[JSONSchema, JSONSchema]> = [],
77+
cycleSet: JSONSchema[] = [],
7778
): JSONSchema {
78-
let isRootOfCycle = false;
7979
const opts = { ...defaultOptions, ...traverseOptions }; // would be nice to make an 'entry' func when we get around to optimizations
8080

8181
// booleans are a bit messed. Since all other schemas are objects (non-primitive type
@@ -112,7 +112,7 @@ export default function traverse(
112112
const rec = (s: JSONSchema, path: string[]): JSONSchema => {
113113
const foundCycle = isCycle(s, recursiveStack);
114114
if (foundCycle) {
115-
if (foundCycle === schema) { isRootOfCycle = true; }
115+
cycleSet.push(foundCycle);
116116

117117
// if the cycle is a ref to the root schema && skipFirstMutation is try we need to call mutate.
118118
// If we don't, it will never happen.
@@ -127,6 +127,7 @@ export default function traverse(
127127
return cycledMutableSchema;
128128
}
129129

130+
// else
130131
return traverse(
131132
s,
132133
mutation,
@@ -135,21 +136,22 @@ export default function traverse(
135136
recursiveStack,
136137
path,
137138
prePostMap,
139+
cycleSet,
138140
);
139141
};
140142

141143
if (schema.anyOf) {
142-
mutableSchema.anyOf = schema.anyOf.map((x,i) => {
144+
mutableSchema.anyOf = schema.anyOf.map((x, i) => {
143145
const result = rec(x, [...pathStack, "anyOf", i.toString()]);
144146
return result;
145147
});
146148
} else if (schema.allOf) {
147-
mutableSchema.allOf = schema.allOf.map((x,i) => {
149+
mutableSchema.allOf = schema.allOf.map((x, i) => {
148150
const result = rec(x, [...pathStack, "allOf", i.toString()]);
149151
return result;
150152
});
151153
} else if (schema.oneOf) {
152-
mutableSchema.oneOf = schema.oneOf.map((x,i) => {
154+
mutableSchema.oneOf = schema.oneOf.map((x, i) => {
153155
const result = rec(x, [...pathStack, "oneOf", i.toString()]);
154156
return result;
155157
});
@@ -158,14 +160,14 @@ export default function traverse(
158160

159161
if (schema.items) {
160162
if (schema.items instanceof Array) {
161-
mutableSchema.items = schema.items.map((x,i) => {
163+
mutableSchema.items = schema.items.map((x, i) => {
162164
const result = rec(x, [...pathStack, "items", i.toString()]);
163165
return result;
164166
});
165167
} else {
166168
const foundCycle = isCycle(schema.items, recursiveStack);
167169
if (foundCycle) {
168-
if (foundCycle === schema) { isRootOfCycle = true; }
170+
cycleSet.push(foundCycle);
169171

170172
if (opts.skipFirstMutation === true && foundCycle === recursiveStack[0]) {
171173
mutableSchema.items = mutation(schema.items, true, jsonPathStringify(pathStack));
@@ -186,6 +188,7 @@ export default function traverse(
186188
recursiveStack,
187189
pathStack,
188190
prePostMap,
191+
cycleSet,
189192
);
190193
}
191194
}
@@ -229,6 +232,7 @@ export default function traverse(
229232
if (opts.bfs === true) {
230233
return mutableSchema;
231234
} else {
232-
return mutation(mutableSchema, isRootOfCycle, jsonPathStringify(pathStack));
235+
const isCycle = cycleSet.indexOf(schema) !== -1
236+
return mutation(mutableSchema, isCycle, jsonPathStringify(pathStack));
233237
}
234238
}

0 commit comments

Comments
 (0)