fix: server url contains spec name if not specified in the spec

This commit is contained in:
Roman Hotsiy 2018-07-23 10:47:01 +03:00
parent ea2210a3c3
commit b41b181dd0
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
4 changed files with 77 additions and 37 deletions

View File

@ -1,6 +1,4 @@
import { action, observable } from 'mobx'; import { action, observable } from 'mobx';
import { join as joinPaths } from 'path';
import { parse as urlParse } from 'url';
import { IMenuItem } from '../MenuStore'; import { IMenuItem } from '../MenuStore';
import { GroupModel } from './Group.model'; import { GroupModel } from './Group.model';
@ -11,13 +9,11 @@ import { OpenAPIExternalDocumentation, OpenAPIServer } from '../../types';
import { import {
getOperationSummary, getOperationSummary,
getStatusCodeType, getStatusCodeType,
IS_BROWSER,
isAbsolutePath,
isStatusCode, isStatusCode,
JsonPointer, JsonPointer,
mergeParams, mergeParams,
normalizeServers,
sortByRequired, sortByRequired,
stripTrailingSlash,
} from '../../utils'; } from '../../utils';
import { ContentItemModel, ExtendedOpenAPIOperation } from '../MenuBuilder'; import { ContentItemModel, ExtendedOpenAPIOperation } from '../MenuBuilder';
import { OpenAPIParser } from '../OpenAPIParser'; import { OpenAPIParser } from '../OpenAPIParser';
@ -148,30 +144,3 @@ export class OperationModel implements IMenuItem {
this.active = false; this.active = false;
} }
} }
function normalizeServers(specUrl: string | undefined, servers: OpenAPIServer[]): OpenAPIServer[] {
const baseUrl = specUrl === undefined ? (IS_BROWSER ? window.location.href : '') : specUrl;
if (servers.length === 0) {
return [
{
url: baseUrl,
},
];
}
function normalizeUrl(url: string): string {
url = isAbsolutePath(url) ? url : joinPaths(baseUrl, url);
return stripTrailingSlash(url.startsWith('//') ? `${specProtocol}${url}` : url);
}
const { protocol: specProtocol } = urlParse(baseUrl);
return servers.map(server => {
return {
...server,
url: normalizeUrl(server.url),
description: server.description || '',
};
});
}

View File

@ -5,6 +5,7 @@ import {
isOperationName, isOperationName,
isPrimitiveType, isPrimitiveType,
mergeParams, mergeParams,
normalizeServers,
} from '../'; } from '../';
import { OpenAPIParser } from '../../services'; import { OpenAPIParser } from '../../services';
@ -222,4 +223,43 @@ describe('Utils', () => {
expect(res[2]).toEqual(operationParams[1]); expect(res[2]).toEqual(operationParams[1]);
}); });
}); });
describe('normalize servers', () => {
it('should make url absolute and strip spec name', () => {
const res = normalizeServers('http://base.com/spec.yaml', [
{
url: '/sandbox/test',
},
]);
expect(res).toEqual([{ url: 'http://base.com/sandbox/test', description: '' }]);
});
it('should prefer server host over spec`s one', () => {
const res = normalizeServers('http://base.com/spec.yaml', [
{
url: 'https://otherbase.com/sandbox/test',
},
]);
expect(res).toEqual([{ url: 'https://otherbase.com/sandbox/test', description: '' }]);
});
it('should strip trailing slash', () => {
const res = normalizeServers('', [
{
url: 'https://otherbase.com/sandbox/test/',
},
]);
expect(res).toEqual([{ url: 'https://otherbase.com/sandbox/test', description: '' }]);
});
it('should set correct protocol', () => {
const res = normalizeServers('https://base.com', [
{
url: '//base.com/sandbox/test',
description: 'test',
},
]);
expect(res).toEqual([{ url: 'https://base.com/sandbox/test', description: 'test' }]);
});
});
}); });

View File

@ -64,10 +64,6 @@ export function stripTrailingSlash(path: string): string {
return path; return path;
} }
export function isAbsolutePath(path: string): boolean {
return /^(?:[a-z]+:)?/i.test(path);
}
export function isNumeric(n: any): n is number { export function isNumeric(n: any): n is number {
return !isNaN(parseFloat(n)) && isFinite(n); return !isNaN(parseFloat(n)) && isFinite(n);
} }

View File

@ -1,12 +1,17 @@
import { dirname } from 'path';
import { parse as urlParse, resolve as resolveUrl } from 'url';
import { OpenAPIParser } from '../services/OpenAPIParser'; import { OpenAPIParser } from '../services/OpenAPIParser';
import { import {
OpenAPIMediaType, OpenAPIMediaType,
OpenAPIOperation, OpenAPIOperation,
OpenAPIParameter, OpenAPIParameter,
OpenAPISchema, OpenAPISchema,
OpenAPIServer,
Referenced, Referenced,
} from '../types'; } from '../types';
import { isNumeric } from './helpers'; import { IS_BROWSER } from './dom';
import { isNumeric, stripTrailingSlash } from './helpers';
function isWildcardStatusCode(statusCode: string | number): statusCode is string { function isWildcardStatusCode(statusCode: string | number): statusCode is string {
return typeof statusCode === 'string' && /\dxx/i.test(statusCode); return typeof statusCode === 'string' && /\dxx/i.test(statusCode);
@ -235,4 +240,34 @@ export function mergeSimilarMediaTypes(types: Dict<OpenAPIMediaType>): Dict<Open
return mergedTypes; return mergedTypes;
} }
export function normalizeServers(
specUrl: string | undefined,
servers: OpenAPIServer[],
): OpenAPIServer[] {
const baseUrl =
specUrl === undefined ? (IS_BROWSER ? window.location.href : '') : dirname(specUrl);
if (servers.length === 0) {
return [
{
url: baseUrl,
},
];
}
const { protocol: specProtocol } = urlParse(baseUrl);
function normalizeUrl(url: string): string {
url = resolveUrl(baseUrl, url);
return stripTrailingSlash(url.startsWith('//') ? `${specProtocol}${url}` : url);
}
return servers.map(server => {
return {
...server,
url: normalizeUrl(server.url),
description: server.description || '',
};
});
}
export const SECURITY_SCHEMES_SECTION = 'section/Authentication/'; export const SECURITY_SCHEMES_SECTION = 'section/Authentication/';