|
| 1 | +import type { ValidAPIDefinition } from '../../utils.js'; |
| 2 | + |
| 3 | +import { describe, it, expect, assert } from 'vitest'; |
| 4 | + |
| 5 | +import { dereference, validate } from '../../../src/index.js'; |
| 6 | +import { relativePath } from '../../utils.js'; |
| 7 | +import { toValidate } from '../../vitest.matchers.js'; |
| 8 | + |
| 9 | +expect.extend({ toValidate }); |
| 10 | + |
| 11 | +describe('API with extensive circular $refs that cause slowdowns', () => { |
| 12 | + it('should validate successfully', async () => { |
| 13 | + await expect(relativePath('specs/circular-slowdowns/schema.json')).toValidate(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should dereference successfully', async () => { |
| 17 | + const circularRefs = new Set<string>(); |
| 18 | + |
| 19 | + const schema = await dereference<ValidAPIDefinition>(relativePath('specs/circular-slowdowns/schema.json'), { |
| 20 | + dereference: { |
| 21 | + onCircular: (ref: string) => circularRefs.add(ref), |
| 22 | + }, |
| 23 | + }); |
| 24 | + |
| 25 | + // Ensure that a non-circular $ref was dereferenced. |
| 26 | + expect(schema.components?.schemas?.ArrayOfMappedData).toStrictEqual({ |
| 27 | + type: 'array', |
| 28 | + items: { |
| 29 | + type: 'object', |
| 30 | + properties: { |
| 31 | + mappingTypeName: { type: 'string' }, |
| 32 | + sourceSystemValue: { type: 'string' }, |
| 33 | + mappedValueID: { type: 'string' }, |
| 34 | + mappedValue: { type: 'string' }, |
| 35 | + }, |
| 36 | + additionalProperties: false, |
| 37 | + }, |
| 38 | + }); |
| 39 | + |
| 40 | + // Ensure that a circular $ref **was** dereferenced. |
| 41 | + expect(circularRefs).toHaveLength(23); |
| 42 | + expect(schema.components?.schemas?.Customer?.properties?.customerNode).toStrictEqual({ |
| 43 | + type: 'array', |
| 44 | + items: { |
| 45 | + type: 'object', |
| 46 | + properties: { |
| 47 | + customerNodeGuid: expect.any(Object), |
| 48 | + customerGuid: expect.any(Object), |
| 49 | + nodeId: expect.any(Object), |
| 50 | + customerGu: expect.any(Object), |
| 51 | + }, |
| 52 | + additionalProperties: false, |
| 53 | + }, |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should not dereference circular $refs if "options.dereference.circular" is "ignore"', async () => { |
| 58 | + const circularRefs = new Set<string>(); |
| 59 | + |
| 60 | + const schema = await dereference<ValidAPIDefinition>(relativePath('specs/circular-slowdowns/schema.json'), { |
| 61 | + dereference: { |
| 62 | + circular: 'ignore', |
| 63 | + onCircular: (ref: string) => circularRefs.add(ref), |
| 64 | + }, |
| 65 | + }); |
| 66 | + |
| 67 | + // Ensure that a non-circular $ref was dereferenced. |
| 68 | + expect(schema.components?.schemas?.ArrayOfMappedData).toStrictEqual({ |
| 69 | + type: 'array', |
| 70 | + items: { |
| 71 | + type: 'object', |
| 72 | + properties: { |
| 73 | + mappingTypeName: { type: 'string' }, |
| 74 | + sourceSystemValue: { type: 'string' }, |
| 75 | + mappedValueID: { type: 'string' }, |
| 76 | + mappedValue: { type: 'string' }, |
| 77 | + }, |
| 78 | + additionalProperties: false, |
| 79 | + }, |
| 80 | + }); |
| 81 | + |
| 82 | + // Ensure that a circular $ref was **not** dereferenced. |
| 83 | + expect(circularRefs).toHaveLength(23); |
| 84 | + expect(schema.components?.schemas?.Customer?.properties?.customerNode).toStrictEqual({ |
| 85 | + type: 'array', |
| 86 | + items: { |
| 87 | + $ref: '#/components/schemas/CustomerNode', |
| 88 | + }, |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should fail validation if "options.dereference.circular" is false', async () => { |
| 93 | + try { |
| 94 | + await validate(relativePath('specs/circular-slowdowns/schema.json'), { dereference: { circular: false } }); |
| 95 | + assert.fail(); |
| 96 | + } catch (err) { |
| 97 | + expect(err).toBeInstanceOf(ReferenceError); |
| 98 | + expect(err.message).toBe( |
| 99 | + 'The API contains circular references but the validator is configured to not permit them.', |
| 100 | + ); |
| 101 | + } |
| 102 | + }); |
| 103 | +}); |
0 commit comments