redoc/lib/services/schema-normalizer.service.spec.ts

258 lines
8.8 KiB
TypeScript
Raw Normal View History

2016-06-22 19:13:57 +03:00
'use strict';
2016-06-22 21:17:48 +03:00
import { SchemaNormalizer } from './schema-normalizer.service';
2016-10-23 20:18:42 +03:00
import { SpecManager } from '../utils/spec-manager';;
2017-04-19 12:41:15 +03:00
import { OptionsService } from '../services/options.service';
2016-06-22 19:13:57 +03:00
describe('Spec Helper', () => {
2017-04-19 12:41:15 +03:00
let specMgr:SpecManager = new SpecManager(new OptionsService());
let normalizer = new SchemaNormalizer(specMgr);
2016-06-22 19:13:57 +03:00
describe('Dereference', () => {
beforeAll(done => {
specMgr.load('/tests/schemas/base-component-dereference.json').then(
2016-06-22 19:13:57 +03:00
() => done()
);
});
describe('simple dereference', () => {
let resolved;
let pointer;
beforeAll(() => {
pointer = '/paths/test1/get/parameters/0';
resolved = normalizer.normalize(specMgr.byPointer(pointer), pointer);
2016-06-22 19:13:57 +03:00
});
it('should not contain $ref property', () => {
expect(resolved.$ref).toBeUndefined();
});
it('should inject Title if not exist based on reference', () => {
resolved.title.should.be.equal('Simple');
});
it('should inject pointer', () => {
resolved._pointer.should.be.equal('#/definitions/Simple');
});
it('should insert correct definition instead of reference', () => {
delete resolved.title;
delete resolved._pointer;
resolved.should.be.deepEqual(specMgr.schema.definitions.Simple);
2016-06-22 19:13:57 +03:00
});
});
describe('nested dereference', () => {
let resolved;
beforeAll(() => {
let pointer = '/paths/test2/get/parameters/0';
resolved = normalizer.normalize(specMgr.byPointer(pointer), pointer);
2016-06-22 19:13:57 +03:00
});
it('should not touch title if exist', () => {
resolved.title.should.be.equal('NesteTitle');
});
it('should resolve nested schema', () => {
expect(resolved.properties.subschema.$ref).toBeUndefined();
resolved._pointer.should.be.equal('#/definitions/Nested');
resolved.properties.subschema._pointer.should.be.equal('#/definitions/Simple');
resolved.properties.subschema.type.should.be.equal('object');
});
});
describe('array schema dereference', () => {
let resolved;
beforeAll(() => {
let pointer = '/paths/test3/get/parameters/0';
resolved = normalizer.normalize(specMgr.byPointer(pointer), pointer);
2016-06-22 19:13:57 +03:00
});
it('should resolve array schema', () => {
expect(resolved.$ref).toBeUndefined();
expect(resolved.items.$ref).toBeUndefined();
resolved.type.should.be.equal('array');
resolved._pointer.should.be.equal('#/definitions/ArrayOfSimple');
resolved.items._pointer.should.be.equal('#/definitions/Simple');
resolved.items.type.should.be.equal('object');
});
});
describe('circular dereference', () => {
let resolved;
beforeAll(() => {
let pointer = '/paths/test4/get/parameters/0';
resolved = normalizer.normalize(specMgr.byPointer(pointer), pointer);
2016-06-22 19:13:57 +03:00
});
it('should resolve circular schema', () => {
expect(resolved.$ref).toBeUndefined();
expect(resolved.items.$ref).toBeUndefined();
resolved.type.should.be.equal('array');
resolved._pointer.should.be.equal('#/definitions/Circular');
2016-06-25 13:58:03 +03:00
});
it('should remove _pointer when detect circularity', () => {
2016-06-22 19:13:57 +03:00
expect(resolved.items._pointer).toBeUndefined();
resolved.items.title.should.be.equal('Circular');
});
2016-06-25 13:58:03 +03:00
it('should resolve transitive circular ref', () => {
let pointer = '/paths/test6/get/parameters/0';
resolved = normalizer.normalize(specMgr.byPointer(pointer), pointer);
expect(resolved.additionalProperties.$ref).toBeUndefined();
expect(resolved.additionalProperties.items.additionalProperties.$ref).toBeUndefined();
resolved.additionalProperties.type.should.be.equal('array');
resolved.additionalProperties._pointer.should.be.equal('#/definitions/CircularTransitive2');
expect(resolved.additionalProperties.items.additionalProperties._pointer).toBeUndefined();
resolved.additionalProperties.items.additionalProperties.title.should.be.equal('CircularTransitive');
});
2016-06-22 19:13:57 +03:00
});
describe('$ref with other fields on the same level', () => {
let resolved;
beforeAll(() => {
let pointer = '/paths/test5/get/parameters/0';
2016-07-01 16:30:45 +03:00
spyOn(console, 'warn').and.stub();
resolved = normalizer.normalize(specMgr.byPointer(pointer), pointer);
2016-06-22 19:13:57 +03:00
});
2016-07-01 16:30:45 +03:00
afterAll(() => {
(<jasmine.Spy>console.warn).and.callThrough();
});
it('should print warning to console', () => {
expect(console.warn).toHaveBeenCalled();
});
2016-06-22 19:13:57 +03:00
it('should skip other fields', () => {
expect(resolved.$ref).toBeUndefined();
expect(resolved.title).toBeDefined();
resolved.title.should.be.equal('Simple');
});
it('should preserve description field', () => {
expect(resolved.$ref).toBeUndefined();
expect(resolved.description).toBeDefined();
resolved.description.should.be.equal('test');
});
});
});
describe('mergeAllOf', () => {
beforeAll((done) => {
specMgr.load('tests/schemas/base-component-joinallof.json').then(() => done());
2016-06-22 19:13:57 +03:00
});
describe('Simple allOf merge', () => {
let joined;
beforeAll(() => {
let pointer = '/definitions/SimpleAllOf';
joined = normalizer.normalize(specMgr.byPointer(pointer), pointer);
2016-06-22 19:13:57 +03:00
});
it('should remove $allOf field', () => {
expect(joined.allOf).toBeNull();
});
it('should set type object', () => {
joined.type.should.be.equal('object');
});
it('should merge properties', () => {
Object.keys(joined.properties).length.should.be.equal(3);
Object.keys(joined.properties).should.be.deepEqual(['prop1', 'prop2', 'prop3']);
});
it('should merge required', () => {
joined.required.length.should.be.equal(2);
joined.required.should.be.deepEqual(['prop1', 'prop3']);
});
});
describe('AllOf with refrence', () => {
let joined;
beforeAll(() => {
let pointer = '/definitions/AllOfWithRef';
joined = normalizer.normalize(specMgr.byPointer(pointer), pointer);
2016-06-22 19:13:57 +03:00
});
it('should remove $allOf field', () => {
expect(joined.allOf).toBeNull();
});
it('should set type object', () => {
joined.type.should.be.equal('object');
});
it('should merge properties', () => {
Object.keys(joined.properties).length.should.be.equal(2);
Object.keys(joined.properties).should.be.deepEqual(['id', 'prop3']);
});
it('should merge required', () => {
joined.required.length.should.be.equal(2);
joined.required.should.be.deepEqual(['id', 'prop3']);
});
});
describe('AllOf with other properties on the allOf level', () => {
let joined;
beforeAll(() => {
let pointer = '/definitions/AllOfWithOther';
joined = normalizer.normalize(specMgr.byPointer(pointer), pointer);
2016-06-22 19:13:57 +03:00
});
it('should remove $allOf field', () => {
expect(joined.allOf).toBeNull();
});
it('should set type object', () => {
joined.type.should.be.equal('object');
});
it('should merge properties', () => {
Object.keys(joined.properties).length.should.be.equal(1);
Object.keys(joined.properties).should.be.deepEqual(['id']);
});
it('should merge required', () => {
joined.required.length.should.be.equal(1);
joined.required.should.be.deepEqual(['id']);
});
it('should preserve parent properties', () => {
joined.description.should.be.equal('Test');
joined.readOnly.should.be.equal(true);
});
});
describe('allOf edgecases', () => {
it('should merge properties and required when defined on allOf level', () => {
let pointer = '/definitions/PropertiesOnAllOfLevel';
let joined;
(() => joined = normalizer.normalize(specMgr.byPointer(pointer), pointer)).should.not.throw();
2016-06-22 19:13:57 +03:00
Object.keys(joined.properties).length.should.be.equal(3);
});
it('should throw when merging schemas with different types', () => {
let pointer = '/definitions/BadAllOf1';
(() => normalizer.normalize(specMgr.byPointer(pointer), pointer)).should.throw();
2016-06-22 19:13:57 +03:00
});
it('should handle nested allOF', () => {
let pointer = '/definitions/NestedAllOf';
let joined;
(() => joined = normalizer.normalize(specMgr.byPointer(pointer), pointer)).should.not.throw();
2016-06-22 19:13:57 +03:00
Object.keys(joined.properties).length.should.be.equal(4);
Object.keys(joined.properties).should.be.deepEqual(['prop1', 'prop2', 'prop3', 'prop4']);
joined.required.should.be.deepEqual(['prop1', 'prop3']);
});
});
xdescribe('Merge array allOf', () => {
//emtpy
});
});
});