From 51965f94b527a1e42e9e8a1daf3bf219b85154de Mon Sep 17 00:00:00 2001 From: Mike Stead Date: Mon, 1 Aug 2016 09:46:34 +1000 Subject: [PATCH] Use api host if schema host is undefined --- lib/utils/SpecManager.ts | 6 ++++-- tests/unit/SpecManager.spec.ts | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/utils/SpecManager.ts b/lib/utils/SpecManager.ts index 74f4d347..3b5776e6 100644 --- a/lib/utils/SpecManager.ts +++ b/lib/utils/SpecManager.ts @@ -43,15 +43,17 @@ export class SpecManager { /* calculate common used values */ init() { let protocol; + const urlParts = urlParse(this._url); if (!this._schema.schemes || !this._schema.schemes.length) { - protocol = this._url ? urlParse(this._url).protocol : 'http'; + protocol = this._url ? urlParts.protocol : 'http'; } else { protocol = this._schema.schemes[0]; if (protocol === 'http' && this._schema.schemes.indexOf('https') >= 0) { protocol = 'https'; } } - this.apiUrl = protocol + '://' + this._schema.host + this._schema.basePath; + let host = (!this._schema.host && urlParts.host) ? urlParts.host : this._schema.host; + this.apiUrl = protocol + '://' + host + this._schema.basePath; if (this.apiUrl.endsWith('/')) { this.apiUrl = this.apiUrl.substr(0, this.apiUrl.length - 1); } diff --git a/tests/unit/SpecManager.spec.ts b/tests/unit/SpecManager.spec.ts index 869392d1..1a32af5e 100644 --- a/tests/unit/SpecManager.spec.ts +++ b/tests/unit/SpecManager.spec.ts @@ -59,6 +59,13 @@ describe('Utils', () => { specMgr.apiUrl.should.be.equal('https://petstore.swagger.io/v2'); }); + it('should substitute api host when spec host is undefined', () => { + specMgr._schema.host = undefined; + specMgr._url = 'https://petstore.swagger.io/v2'; + specMgr.init(); + specMgr.apiUrl.should.be.equal('https://petstore.swagger.io/v2'); + }); + describe('byPointer method', () => { it('should return correct schema part', ()=> { let part = specMgr.byPointer('/tags/3');