mirror of
https://github.com/Redocly/redoc.git
synced 2025-07-11 16:52:32 +03:00
Redesigned specs + finish schemaManager spec
This commit is contained in:
parent
ddbad9bfd3
commit
d2d856067b
67
tests/schemas/schema-mgr-methodparams.json
Normal file
67
tests/schemas/schema-mgr-methodparams.json
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
{
|
||||||
|
"swagger": "2.0",
|
||||||
|
"info": {
|
||||||
|
"version": "1.0.0",
|
||||||
|
"title": "Test schema"
|
||||||
|
},
|
||||||
|
"host": "petstore.swagger.io",
|
||||||
|
"basePath": "/v2/",
|
||||||
|
"parameters": {
|
||||||
|
"extparam": {
|
||||||
|
"name": "extParam",
|
||||||
|
"in": "query",
|
||||||
|
"type": "integer"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"paths": {
|
||||||
|
"test1": {
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"name": "pathParam",
|
||||||
|
"in": "path",
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"get": {
|
||||||
|
"summary": "test get",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"name": "methodParam",
|
||||||
|
"in": "path",
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"test2": {
|
||||||
|
"parameters": [
|
||||||
|
{ "$ref": "#/parameters/extparam" }
|
||||||
|
],
|
||||||
|
"get": {
|
||||||
|
"summary": "test get",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"name": "methodParam",
|
||||||
|
"in": "path",
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"test3": {
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"name": "pathParam",
|
||||||
|
"in": "path",
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"get": {
|
||||||
|
"summary": "test get",
|
||||||
|
"parameters": [
|
||||||
|
{ "$ref": "#/parameters/extparam" }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,126 +1,202 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
import SchemaManager from 'lib/utils/SchemaManager';
|
import SchemaManager from 'lib/utils/SchemaManager';
|
||||||
describe("Schema manager", () => {
|
describe('Schema manager', () => {
|
||||||
let schemaMgr;
|
let schemaMgr;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
schemaMgr = new SchemaManager();
|
schemaMgr = new SchemaManager();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should initialize with empty schema", ()=> {
|
it('Should initialize with empty schema', ()=> {
|
||||||
schemaMgr.schema.should.be.empty;
|
schemaMgr.schema.should.be.empty;
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should be a singleton", ()=> {
|
it('Should be a singleton', ()=> {
|
||||||
(new SchemaManager()).should.be.equal(schemaMgr);
|
(new SchemaManager()).should.be.equal(schemaMgr);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("load should return a promise", ()=> {
|
it('load should return a promise', ()=> {
|
||||||
schemaMgr.load('/tests/schemas/extended-petstore.json').should.be.instanceof(Promise);
|
schemaMgr.load('/tests/schemas/extended-petstore.json').should.be.instanceof(Promise);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("load should resolve promise for valid url", (done)=> {
|
it('load should resolve promise for valid url', (done)=> {
|
||||||
schemaMgr.load('/tests/schemas/extended-petstore.json').then(() => {
|
schemaMgr.load('/tests/schemas/extended-petstore.json').then(() => {
|
||||||
done();
|
done();
|
||||||
}, (err) => {
|
}, () => {
|
||||||
throw new Error("Error handler should not be called")
|
throw new Error('Error handler should not be called');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Schema manager with loaded schema", ()=> {
|
describe('Schema manager basic functionality', ()=> {
|
||||||
before(function (done) {
|
before(function (done) {
|
||||||
schemaMgr.load('/tests/schemas/extended-petstore.json').then(() => {
|
schemaMgr.load('/tests/schemas/extended-petstore.json').then(() => {
|
||||||
done();
|
done();
|
||||||
}, (err) => {
|
}, () => {
|
||||||
throw new Error("Error handler should not be called")
|
throw new Error('Error handler should not be called');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it("should contain non-empty schema", ()=> {
|
it('should contain non-empty schema', ()=> {
|
||||||
schemaMgr.schema.should.be.an("object");
|
schemaMgr.schema.should.be.an('object');
|
||||||
schemaMgr.schema.should.be.not.empty;
|
schemaMgr.schema.should.be.not.empty;
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should correctly init api url", ()=> {
|
it('should correctly init api url', ()=> {
|
||||||
schemaMgr.apiUrl.should.be.equal("http://petstore.swagger.io/v2");
|
schemaMgr.apiUrl.should.be.equal('http://petstore.swagger.io/v2');
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("byPointer method", () => {
|
describe('byPointer method', () => {
|
||||||
it("should return correct schema part", ()=> {
|
it('should return correct schema part', ()=> {
|
||||||
let part = schemaMgr.byPointer('/tags/3');
|
let part = schemaMgr.byPointer('/tags/3');
|
||||||
part.should.be.deep.equal(schemaMgr.schema.tags[3]);
|
part.should.be.deep.equal(schemaMgr.schema.tags[3]);
|
||||||
part.should.be.equal(schemaMgr.schema.tags[3]);
|
part.should.be.equal(schemaMgr.schema.tags[3]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return null for incorrect pointer", ()=> {
|
it('should return null for incorrect pointer', ()=> {
|
||||||
let part = schemaMgr.byPointer('/incorrect/pointer');
|
let part = schemaMgr.byPointer('/incorrect/pointer');
|
||||||
should.not.exist(part);
|
should.not.exist(part);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("getTagsMap method", () => {
|
describe('getTagsMap method', () => {
|
||||||
it("should return correct tags map", () => {
|
before(function () {
|
||||||
|
schemaMgr._schema = {
|
||||||
|
tags: [
|
||||||
|
{name: 'tag1', description: 'info1'},
|
||||||
|
{name: 'tag2', description: 'info2', 'x-traitTag': true}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return correct tags map', () => {
|
||||||
let tagsMap = schemaMgr.getTagsMap();
|
let tagsMap = schemaMgr.getTagsMap();
|
||||||
let i = 0;
|
let expectedResult = {
|
||||||
let origTags = schemaMgr.schema.tags;
|
tag1: {description: 'info1', 'x-traitTag': false},
|
||||||
|
tag2: {description: 'info2', 'x-traitTag': true}
|
||||||
origTags.length.should.be.equal(Object.keys(tagsMap).length);
|
};
|
||||||
for (let tagName of Object.keys(tagsMap)) {
|
tagsMap.should.be.deep.equal(expectedResult);
|
||||||
tagName.should.be.equal(origTags[i].name);
|
|
||||||
tagsMap[tagName].description.should.be.equal(origTags[i].description);
|
|
||||||
if (origTags[i]['x-traitTag']) {
|
|
||||||
tagsMap[tagName]['x-traitTag'].should.be.equal(origTags[i]['x-traitTag']);
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("buildMenuTree method", () => {
|
describe('buildMenuTree method', () => {
|
||||||
var menuTree;
|
let suitSchema = {
|
||||||
|
tags: [
|
||||||
|
{name: 'tag1', description: 'info1', 'x-traitTag': true},
|
||||||
|
{name: 'tag2', description: 'info2'}
|
||||||
|
],
|
||||||
|
paths: {
|
||||||
|
test: {
|
||||||
|
put: {
|
||||||
|
tags: ['tag1', 'tag3'],
|
||||||
|
summary: 'test put'
|
||||||
|
},
|
||||||
|
get: {
|
||||||
|
tags: ['tag1', 'tag2'],
|
||||||
|
summary: 'test get'
|
||||||
|
},
|
||||||
|
// no tags
|
||||||
|
post: {
|
||||||
|
summary: 'test post'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let menuTree;
|
||||||
let entries;
|
let entries;
|
||||||
|
|
||||||
before(() => {
|
before(() => {
|
||||||
|
schemaMgr._schema = suitSchema;
|
||||||
menuTree = schemaMgr.buildMenuTree();
|
menuTree = schemaMgr.buildMenuTree();
|
||||||
entries = Array.from(menuTree.entries());
|
entries = Array.from(menuTree.entries());
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return instance of Map", () => {
|
it('should return instance of Map', () => {
|
||||||
menuTree.should.be.instanceof(Map);
|
menuTree.should.be.instanceof(Map);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return Map with correct number of entries", () => {
|
it('should return Map with correct number of entries', () => {
|
||||||
entries.length.should.be.at.least(schemaMgr.schema.tags.length);
|
//2 - defined tags, 1 - tag3 and 1 [other] tag for no-tags method
|
||||||
|
entries.length.should.be.equal(2 + 1 + 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("methods for tag should contain valid pointer and summary", () => {
|
it('should append not defined tags to the end of list', () => {
|
||||||
|
let [tag, info] = entries[2];
|
||||||
|
tag.should.be.equal('tag3');
|
||||||
|
info.methods.length.should.be.equal(1);
|
||||||
|
info.methods[0].summary.should.be.equal('test put');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should append methods without tags to [other] tag', () => {
|
||||||
|
let [tag, info] = entries[3];
|
||||||
|
tag.should.be.equal('[Other]');
|
||||||
|
info.methods.length.should.be.equal(1);
|
||||||
|
info.methods[0].summary.should.be.equal('test post');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should map x-traitTag to empty methods list', () => {
|
||||||
|
let [, info] = entries[0];
|
||||||
|
info['x-traitTag'].should.be.true;
|
||||||
|
info.methods.should.be.empty;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('methods for tag should contain valid pointer and summary', () => {
|
||||||
for (let entr of entries) {
|
for (let entr of entries) {
|
||||||
let [tag, info] = entr;
|
let [, info] = entr;
|
||||||
info.should.be.an("object");
|
info.should.be.an('object');
|
||||||
info.methods.should.be.an("array");
|
info.methods.should.be.an('array');
|
||||||
for (let methodInfo of info.methods) {
|
for (let methodInfo of info.methods) {
|
||||||
methodInfo.should.include.keys('pointer', 'summary');
|
methodInfo.should.include.keys('pointer', 'summary');
|
||||||
let methSchema = schemaMgr.byPointer(methodInfo.pointer);
|
let methSchema = schemaMgr.byPointer(methodInfo.pointer);
|
||||||
should.exist(methSchema);
|
should.exist(methSchema);
|
||||||
if (methSchema.summary) {
|
if (methSchema.summary) {
|
||||||
methSchema.summary.should.be.equal(methodInfo.summary)
|
methSchema.summary.should.be.equal(methodInfo.summary);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("should map x-traitTag to empty methods list", () => {
|
describe('getMethodParams method', () => {
|
||||||
for (let entr of entries) {
|
before((done) => {
|
||||||
let [tag, info] = entr;
|
schemaMgr.load('/tests/schemas/schema-mgr-methodparams.json').then(() => {
|
||||||
info.should.be.an("object");
|
done();
|
||||||
if (info['x-traitTag']) {
|
}, () => {
|
||||||
info.methods.should.be.empty;
|
done(new Error('Error handler should not be called'));
|
||||||
}
|
});
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should merge path and method parameters', () => {
|
||||||
|
let params = schemaMgr.getMethodParams('/paths/test1/get');
|
||||||
|
params.length.should.be.equal(2);
|
||||||
|
params[0].name.should.be.equal('methodParam');
|
||||||
|
params[1].name.should.be.equal('pathParam');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should inject correct pointers', () => {
|
||||||
|
let params = schemaMgr.getMethodParams('/paths/test1/get');
|
||||||
|
params[0]._pointer.should.be.equal('/paths/test1/get/parameters/0');
|
||||||
|
params[1]._pointer.should.be.equal('/paths/test1/parameters/0');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should resolve path params from Parameters Definitions Object', () => {
|
||||||
|
let params = schemaMgr.getMethodParams('/paths/test2/get', true);
|
||||||
|
params.length.should.be.equal(2);
|
||||||
|
params[0].name.should.be.equal('methodParam');
|
||||||
|
params[1].name.should.be.equal('extParam');
|
||||||
|
params[1]._pointer.should.be.equal('#/parameters/extparam');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should resolve method params from Parameters Definitions Object', () => {
|
||||||
|
let params = schemaMgr.getMethodParams('/paths/test3/get', true);
|
||||||
|
params.length.should.be.equal(2);
|
||||||
|
params[0].name.should.be.equal('extParam');
|
||||||
|
params[0]._pointer.should.be.equal('#/parameters/extparam');
|
||||||
|
params[1].name.should.be.equal('pathParam');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user