Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #649: correct URLs of OperationModel servers #2081

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/utils/__tests__/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,24 @@ describe('Utils', () => {
expect(res).toEqual([{ url: 'https://base.com/sandbox/test', description: 'test' }]);
});

it('should remove query string and hash from url', () => {
const originalWindow = { ...window };
const windowSpy: jest.SpyInstance = jest.spyOn(global, 'window', 'get');
windowSpy.mockImplementation(() => ({
...originalWindow,
location: {
...originalWindow.location,
href: 'https://base.com/subpath/?param=value#tag',
},
}));
const res = normalizeServers(undefined, [
{
url: 'sandbox/test',
},
]);
expect(res).toEqual([{ url: 'https://base.com/subpath/sandbox/test', description: '' }]);
});

it('should expand variables', () => {
const servers = normalizeServers('', [
{
Expand Down
3 changes: 2 additions & 1 deletion src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,11 @@ export function titleize(text: string) {
return text.charAt(0).toUpperCase() + text.slice(1);
}

export function removeQueryString(serverUrl: string): string {
export function removeQueryStringAndHash(serverUrl: string): string {
try {
const url = parseURL(serverUrl);
url.search = '';
url.hash = '';
return url.toString();
} catch (e) {
// when using with redoc-cli serverUrl can be empty resulting in crash
Expand Down
4 changes: 2 additions & 2 deletions src/utils/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
Referenced,
} from '../types';
import { IS_BROWSER } from './dom';
import { isNumeric, removeQueryString, resolveUrl, isArray, isBoolean } from './helpers';
import { isNumeric, removeQueryStringAndHash, resolveUrl, isArray, isBoolean } from './helpers';

function isWildcardStatusCode(statusCode: string | number): statusCode is string {
return typeof statusCode === 'string' && /\dxx/i.test(statusCode);
Expand Down Expand Up @@ -602,7 +602,7 @@ export function normalizeServers(
return href.endsWith('.html') ? dirname(href) : href;
};

const baseUrl = specUrl === undefined ? removeQueryString(getHref()) : dirname(specUrl);
const baseUrl = specUrl === undefined ? removeQueryStringAndHash(getHref()) : dirname(specUrl);

if (servers.length === 0) {
// Behaviour defined in OpenAPI spec: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#openapi-object
Expand Down