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: operation url in static page #2093

Merged
merged 1 commit into from
Jul 22, 2022
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
14 changes: 7 additions & 7 deletions src/components/Endpoint/Endpoint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,18 @@ export class Endpoint extends React.Component<EndpointProps, EndpointState> {
? expandDefaultServerVariables(server.url, server.variables)
: server.url;
const basePath = getBasePath(normalizedUrl);
const serverBaseUrl =
hideHostname || options.hideHostname
? basePath === '/'
? ''
: basePath
: normalizedUrl;
return (
<ServerItem key={normalizedUrl}>
<Markdown source={server.description || ''} compact={true} />
<SelectOnClick>
<ServerUrl>
<span>
{hideHostname || options.hideHostname
? basePath === '/'
? ''
: basePath
: normalizedUrl}
</span>
<span>{serverBaseUrl}</span>
{operation.path}
</ServerUrl>
</SelectOnClick>
Expand Down
9 changes: 9 additions & 0 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ export function getBasePath(serverUrl: string): string {
}
}

export function getBaseUrl(serverUrl: string): string {
try {
return parseURL(serverUrl).origin;
} catch (e) {
// when using with redoc-cli serverUrl can be empty resulting in crash
return serverUrl;
}
}

export function titleize(text: string) {
return text.charAt(0).toUpperCase() + text.slice(1);
}
Expand Down
5 changes: 2 additions & 3 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, resolveUrl, isArray, isBoolean, getBaseUrl } from './helpers';

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

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

const baseUrl = specUrl === undefined ? getBaseUrl(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
servers = [
Expand Down