Skip to content

Commit ba5741f

Browse files
committed
add tests for circular references
1 parent cf49248 commit ba5741f

File tree

1 file changed

+115
-7
lines changed

1 file changed

+115
-7
lines changed

test/src/Convertor.spec.js

Lines changed: 115 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ describe("Convertor", () => {
772772
result.schemas.basic.properties.payment.oneOf[0].default
773773
).to.be.equal("one");
774774

775-
const cloned = JSON.parse(JSON.stringify(basicOpenAPI));
775+
const cloned = cloneDeep(basicOpenAPI);
776776
Object.assign(cloned, { components: result });
777777
expect(cloned).to.have.property("components");
778778
expect(cloned.components).to.have.property("schemas");
@@ -807,7 +807,7 @@ describe("Convertor", () => {
807807
result.schemas.basic.properties.payment.anyOf[1].default
808808
).to.be.equal(1);
809809

810-
const cloned = JSON.parse(JSON.stringify(basicOpenAPI));
810+
const cloned = cloneDeep(basicOpenAPI);
811811
Object.assign(cloned, { components: result });
812812
expect(cloned).to.have.property("components");
813813
expect(cloned.components).to.have.property("schemas");
@@ -836,7 +836,7 @@ describe("Convertor", () => {
836836
result.schemas.basic.properties.payment.allOf[0].default
837837
).to.be.equal("one");
838838

839-
const cloned = JSON.parse(JSON.stringify(basicOpenAPI));
839+
const cloned = cloneDeep(basicOpenAPI);
840840
Object.assign(cloned, { components: result });
841841
expect(cloned).to.have.property("components");
842842
expect(cloned.components).to.have.property("schemas");
@@ -848,12 +848,120 @@ describe("Convertor", () => {
848848

849849
describe(`circular schemas`, function () {
850850
it(`should convert a circular schema`, async function () {
851-
const bundled = await $RefParser.bundle(circular);
852-
const dereferenced = await $RefParser.dereference(bundled);
851+
const parser = new $RefParser();
852+
const bundled = await parser.bundle(circular);
853+
const dereferenced = await parser.dereference(bundled);
854+
855+
const getCircularReplacer = () => {
856+
const seen = new WeakSet();
857+
return (key, value) => {
858+
if (typeof value === "object" && value !== null) {
859+
if (seen.has(value)) {
860+
return;
861+
}
862+
seen.add(value);
863+
}
864+
return value;
865+
};
866+
};
867+
868+
// console.log(JSON.stringify(dereferenced, getCircularReplacer()));
869+
870+
expect(parser.$refs.circular).to.be.true;
871+
expect(dereferenced).to.have.property("definitions");
872+
expect(
873+
dereferenced.properties.user.properties.classes.items.properties
874+
).to.haveOwnProperty("subRows");
875+
expect(
876+
dereferenced.properties.user.properties.classes.items.properties
877+
).to.haveOwnProperty("className");
878+
expect(
879+
dereferenced.properties.user.properties.classes.items.properties
880+
).to.haveOwnProperty("details");
881+
expect(
882+
dereferenced.properties.user.properties.classes.items.properties
883+
).to.haveOwnProperty("id");
884+
expect(
885+
dereferenced.properties.user.properties.classes.items.properties
886+
).to.haveOwnProperty("parentId");
887+
888+
expect(
889+
dereferenced.properties.user.properties.classes.items.properties
890+
.subRows.type
891+
).to.be.equal("array");
892+
expect(
893+
dereferenced.properties.user.properties.classes.items.properties
894+
.subRows.items.properties
895+
).to.haveOwnProperty("subRows");
896+
expect(
897+
dereferenced.properties.user.properties.classes.items.properties
898+
.subRows.items.properties
899+
).to.haveOwnProperty("className");
900+
expect(
901+
dereferenced.properties.user.properties.classes.items.properties
902+
.subRows.items.properties
903+
).to.haveOwnProperty("details");
904+
expect(
905+
dereferenced.properties.user.properties.classes.items.properties
906+
.subRows.items.properties
907+
).to.haveOwnProperty("id");
908+
expect(
909+
dereferenced.properties.user.properties.classes.items.properties
910+
.subRows.items.properties
911+
).to.haveOwnProperty("parentId");
853912

854913
const newConvertor = new Convertor(dereferenced);
855-
const result = newConvertor.convert("basic");
856-
console.log(result);
914+
try {
915+
const result = newConvertor.convert("basic");
916+
917+
expect(result.schemas.basic).to.not.have.property("definitions");
918+
expect(
919+
result.schemas.basic.properties.user.properties.classes.items
920+
.properties
921+
).to.haveOwnProperty("subRows");
922+
expect(
923+
result.schemas.basic.properties.user.properties.classes.items
924+
.properties
925+
).to.haveOwnProperty("className");
926+
expect(
927+
result.schemas.basic.properties.user.properties.classes.items
928+
.properties
929+
).to.haveOwnProperty("details");
930+
expect(
931+
result.schemas.basic.properties.user.properties.classes.items
932+
.properties
933+
).to.haveOwnProperty("id");
934+
expect(
935+
result.schemas.basic.properties.user.properties.classes.items
936+
.properties
937+
).to.haveOwnProperty("parentId");
938+
939+
expect(
940+
result.schemas.basic.properties.user.properties.classes.items
941+
.properties.subRows.type
942+
).to.be.equal("array");
943+
944+
expect(
945+
result.schemas.basic.properties.user.properties.classes.items
946+
.properties.subRows
947+
).to.have.property("items");
948+
expect(
949+
result.schemas.basic.properties.user.properties.classes.items
950+
.properties.subRows.items
951+
).to.be.deep.equal({});
952+
953+
// console.log(JSON.stringify(result));
954+
955+
const cloned = cloneDeep(basicOpenAPI);
956+
Object.assign(cloned, { components: result });
957+
expect(cloned).to.have.property("components");
958+
expect(cloned.components).to.have.property("schemas");
959+
expect(cloned.components.schemas).to.have.property("basic");
960+
let valid = await validator.validateInner(cloned, {});
961+
expect(valid).to.be.true;
962+
} catch (err) {
963+
expect(err).to.be.undefined;
964+
}
857965
});
858966
});
859967

0 commit comments

Comments
 (0)