Skip to content

Commit

Permalink
fix: server url contains spec name if not specified in the spec
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Jul 23, 2018
1 parent ea2210a commit b41b181
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 37 deletions.
33 changes: 1 addition & 32 deletions src/services/models/Operation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { action, observable } from 'mobx';
import { join as joinPaths } from 'path';
import { parse as urlParse } from 'url';

import { IMenuItem } from '../MenuStore';
import { GroupModel } from './Group.model';
Expand All @@ -11,13 +9,11 @@ import { OpenAPIExternalDocumentation, OpenAPIServer } from '../../types';
import {
getOperationSummary,
getStatusCodeType,
IS_BROWSER,
isAbsolutePath,
isStatusCode,
JsonPointer,
mergeParams,
normalizeServers,
sortByRequired,
stripTrailingSlash,
} from '../../utils';
import { ContentItemModel, ExtendedOpenAPIOperation } from '../MenuBuilder';
import { OpenAPIParser } from '../OpenAPIParser';
Expand Down Expand Up @@ -148,30 +144,3 @@ export class OperationModel implements IMenuItem {
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 || '',
};
});
}
40 changes: 40 additions & 0 deletions src/utils/__tests__/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
isOperationName,
isPrimitiveType,
mergeParams,
normalizeServers,
} from '../';

import { OpenAPIParser } from '../../services';
Expand Down Expand Up @@ -222,4 +223,43 @@ describe('Utils', () => {
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' }]);
});
});
});
4 changes: 0 additions & 4 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ export function stripTrailingSlash(path: string): string {
return path;
}

export function isAbsolutePath(path: string): boolean {
return /^(?:[a-z]+:)?/i.test(path);
}

export function isNumeric(n: any): n is number {
return !isNaN(parseFloat(n)) && isFinite(n);
}
Expand Down
37 changes: 36 additions & 1 deletion src/utils/openapi.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { dirname } from 'path';
import { parse as urlParse, resolve as resolveUrl } from 'url';

import { OpenAPIParser } from '../services/OpenAPIParser';
import {
OpenAPIMediaType,
OpenAPIOperation,
OpenAPIParameter,
OpenAPISchema,
OpenAPIServer,
Referenced,
} from '../types';
import { isNumeric } from './helpers';
import { IS_BROWSER } from './dom';
import { isNumeric, stripTrailingSlash } from './helpers';

function isWildcardStatusCode(statusCode: string | number): statusCode is string {
return typeof statusCode === 'string' && /\dxx/i.test(statusCode);
Expand Down Expand Up @@ -235,4 +240,34 @@ export function mergeSimilarMediaTypes(types: Dict<OpenAPIMediaType>): Dict<Open
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/';

0 comments on commit b41b181

Please sign in to comment.