|
1 | 1 | const { expect } = require('chai');
|
2 |
| -const { convertSchema, convertSchemaDefinitions, dereference } = require('../lib/json-schema'); |
| 2 | +const { |
| 3 | + convertSchema, convertSchemaDefinitions, dereference, pathHasCircularReference, |
| 4 | +} = require('../lib/json-schema'); |
3 | 5 |
|
4 | 6 | describe('Swagger Schema to JSON Schema', () => {
|
5 | 7 | it('returns compatible schema when given valid JSON Schema', () => {
|
@@ -853,3 +855,50 @@ describe('Dereferencing', () => {
|
853 | 855 | });
|
854 | 856 | });
|
855 | 857 | });
|
| 858 | + |
| 859 | + |
| 860 | +describe('#pathHasCircularReference', () => { |
| 861 | + it('does not detect circular reference when reference is not circular', () => { |
| 862 | + const reference = '#/definitions/User'; |
| 863 | + const currentPath = ['#', 'definitions', 'ListUsers']; |
| 864 | + |
| 865 | + expect(pathHasCircularReference([], currentPath, reference)).to.be.false; |
| 866 | + }); |
| 867 | + |
| 868 | + it('detects circular reference when current path is reference path', () => { |
| 869 | + const reference = '#/definitions/User'; |
| 870 | + const currentPath = ['#', 'definitions', 'User']; |
| 871 | + |
| 872 | + expect(pathHasCircularReference([], currentPath, reference)).to.be.true; |
| 873 | + }); |
| 874 | + |
| 875 | + it('detects circular reference when current path contains reference path', () => { |
| 876 | + const reference = '#/definitions/User'; |
| 877 | + const currentPath = ['#', 'definitions', 'User', 'properties', 'parent']; |
| 878 | + |
| 879 | + expect(pathHasCircularReference([], currentPath, reference)).to.be.true; |
| 880 | + }); |
| 881 | + |
| 882 | + it('detects incircular reference when current path is in the prior referenced tree', () => { |
| 883 | + const reference = '#/definitions/User'; |
| 884 | + const currentPath = ['#', 'definitions', 'ListUsers']; |
| 885 | + const currentTree = ['#/definitions/User/properties/children']; |
| 886 | + |
| 887 | + expect(pathHasCircularReference(currentTree, currentPath, reference)).to.be.true; |
| 888 | + }); |
| 889 | + |
| 890 | + it('does not detect circular reference when reference last component prefixes current path', () => { |
| 891 | + const reference = '#/definitions/User'; |
| 892 | + const currentPath = ['#', 'definitions', 'UserList']; |
| 893 | + |
| 894 | + expect(pathHasCircularReference([], currentPath, reference)).to.be.false; |
| 895 | + }); |
| 896 | + |
| 897 | + it('does not detect incircular reference when reference last component prefixes path in prior reference tree', () => { |
| 898 | + const reference = '#/definitions/User'; |
| 899 | + const currentPath = ['#', 'definitions', 'UserDetail']; |
| 900 | + const currentTree = ['#/definitions/UserList/items']; |
| 901 | + |
| 902 | + expect(pathHasCircularReference(currentTree, currentPath, reference)).to.be.false; |
| 903 | + }); |
| 904 | +}); |
0 commit comments