From 8db7cc92bb8944b7a9527f563a4bf7f81de86ecc Mon Sep 17 00:00:00 2001 From: paza0322 Date: Mon, 18 Jul 2022 13:38:10 +0300 Subject: [PATCH 1/2] fix: correct URLs of OperationModel servers --- src/utils/helpers.ts | 3 ++- src/utils/openapi.ts | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index 16ed57c1c3..2b02fab352 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -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 diff --git a/src/utils/openapi.ts b/src/utils/openapi.ts index 272f04f9f7..946defd054 100644 --- a/src/utils/openapi.ts +++ b/src/utils/openapi.ts @@ -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); @@ -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 From eba0265e6fd67cbd4d6295ab87c5b5af458fe069 Mon Sep 17 00:00:00 2001 From: paza0322 Date: Wed, 20 Jul 2022 12:12:37 +0300 Subject: [PATCH 2/2] test: add a new test for normalizeServers --- src/utils/__tests__/openapi.test.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/utils/__tests__/openapi.test.ts b/src/utils/__tests__/openapi.test.ts index 3abbcf9989..5249403cfd 100644 --- a/src/utils/__tests__/openapi.test.ts +++ b/src/utils/__tests__/openapi.test.ts @@ -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('', [ {