|
| 1 | +import { describe, it } from "vitest"; |
| 2 | +import $RefParser from "../../../lib/index.js"; |
| 3 | +import helper from "../../utils/helper.js"; |
| 4 | +import path from "../../utils/path.js"; |
| 5 | + |
| 6 | +import { expect } from "vitest"; |
| 7 | + |
| 8 | +describe("Schema with an extensive amount of circular $refs", () => { |
| 9 | + it("should dereference successfully", async () => { |
| 10 | + const circularRefs = new Set<string>(); |
| 11 | + |
| 12 | + const parser = new $RefParser<Record<string, any>>(); |
| 13 | + const schema = await parser.dereference(path.rel("test/specs/circular-extensive/schema.json"), { |
| 14 | + dereference: { |
| 15 | + onCircular: (ref: string) => circularRefs.add(ref), |
| 16 | + }, |
| 17 | + }); |
| 18 | + |
| 19 | + // Ensure that a non-circular $ref was dereferenced. |
| 20 | + expect(schema.components?.schemas?.ArrayOfMappedData).toStrictEqual({ |
| 21 | + type: "array", |
| 22 | + items: { |
| 23 | + type: "object", |
| 24 | + properties: { |
| 25 | + mappingTypeName: { type: "string" }, |
| 26 | + sourceSystemValue: { type: "string" }, |
| 27 | + mappedValueID: { type: "string" }, |
| 28 | + mappedValue: { type: "string" }, |
| 29 | + }, |
| 30 | + additionalProperties: false, |
| 31 | + }, |
| 32 | + }); |
| 33 | + |
| 34 | + // Ensure that a circular $ref **was** dereferenced. |
| 35 | + expect(circularRefs).toHaveLength(23); |
| 36 | + expect(schema.components?.schemas?.Customer?.properties?.customerNode).toStrictEqual({ |
| 37 | + type: "array", |
| 38 | + items: { |
| 39 | + type: "object", |
| 40 | + properties: { |
| 41 | + customerNodeGuid: expect.any(Object), |
| 42 | + customerGuid: expect.any(Object), |
| 43 | + nodeId: expect.any(Object), |
| 44 | + customerGu: expect.any(Object), |
| 45 | + }, |
| 46 | + additionalProperties: false, |
| 47 | + }, |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should dereference successfully with `dereference.circular` is `ignore`", async () => { |
| 52 | + const circularRefs = new Set<string>(); |
| 53 | + |
| 54 | + const parser = new $RefParser<Record<string, any>>(); |
| 55 | + const schema = await parser.dereference(path.rel("test/specs/circular-extensive/schema.json"), { |
| 56 | + dereference: { |
| 57 | + onCircular: (ref: string) => circularRefs.add(ref), |
| 58 | + circular: "ignore", |
| 59 | + }, |
| 60 | + }); |
| 61 | + |
| 62 | + // Ensure that a non-circular $ref was dereferenced. |
| 63 | + expect(schema.components?.schemas?.ArrayOfMappedData).toStrictEqual({ |
| 64 | + type: "array", |
| 65 | + items: { |
| 66 | + type: "object", |
| 67 | + properties: { |
| 68 | + mappingTypeName: { type: "string" }, |
| 69 | + sourceSystemValue: { type: "string" }, |
| 70 | + mappedValueID: { type: "string" }, |
| 71 | + mappedValue: { type: "string" }, |
| 72 | + }, |
| 73 | + additionalProperties: false, |
| 74 | + }, |
| 75 | + }); |
| 76 | + |
| 77 | + // Ensure that a circular $ref was **not** dereferenced. |
| 78 | + expect(circularRefs).toHaveLength(23); |
| 79 | + expect(schema.components?.schemas?.Customer?.properties?.customerNode).toStrictEqual({ |
| 80 | + type: "array", |
| 81 | + items: { |
| 82 | + $ref: "#/components/schemas/CustomerNode", |
| 83 | + }, |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should throw an error if "options.dereference.circular" is false', async () => { |
| 88 | + const parser = new $RefParser(); |
| 89 | + |
| 90 | + try { |
| 91 | + await parser.dereference(path.rel("test/specs/circular-extensive/schema.json"), { |
| 92 | + dereference: { circular: false }, |
| 93 | + }); |
| 94 | + |
| 95 | + helper.shouldNotGetCalled(); |
| 96 | + } catch (err) { |
| 97 | + expect(err).to.be.an.instanceOf(ReferenceError); |
| 98 | + expect(err.message).to.contain("Circular $ref pointer found at "); |
| 99 | + expect(err.message).to.contain( |
| 100 | + "specs/circular-extensive/schema.json#/components/schemas/AssignmentExternalReference/properties/assignment/oneOf/0", |
| 101 | + ); |
| 102 | + |
| 103 | + // $Refs.circular should be true |
| 104 | + expect(parser.$refs.circular).to.equal(true); |
| 105 | + } |
| 106 | + }); |
| 107 | +}); |
0 commit comments