Strip trailing slash from OpenAPI server URLs (#4029)

This commit is contained in:
Nolann B.
2026-02-20 14:40:36 +01:00
committed by GitHub
parent 9dfa9c2db0
commit 93eea0b6d9
4 changed files with 37 additions and 7 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@gitbook/react-openapi": patch
---
Strip trailing slash from OpenAPI server URLs to avoid double slashes in rendered paths
@@ -7,7 +7,7 @@ import { OpenAPITooltip } from './OpenAPITooltip';
import type { OpenAPIClientContext } from './context';
import { formatPath } from './formatPath';
import type { OpenAPIServerWithCustomProperties } from './types';
import { getDefaultServerURL } from './util/server';
import { getDefaultServerURL, interpolateServerURL } from './util/server';
import { createStateKey } from './utils';
export const serversStateKey = createStateKey('servers');
@@ -29,11 +29,14 @@ export function OpenAPIPathMultipleServers(
.filter(
(server): server is OpenAPIServerWithCustomProperties & { url: string } => !!server.url
)
.map((server) => ({
key: server.url,
label: server.url,
description: server.description,
}));
.map((server) => {
const url = interpolateServerURL(server);
return {
key: url,
label: url,
description: server.description,
};
});
return (
<OpenAPIPathItem
@@ -23,6 +23,25 @@ describe('#interpolateServerURL', () => {
expect(result).toBe('https://{username}.example.com/{basePath}');
});
it('strips trailing slash from the server URL', () => {
const server: OpenAPIV3.ServerObject = {
url: '/butler/api/',
};
const result = interpolateServerURL(server);
expect(result).toBe('/butler/api');
});
it('strips trailing slash after variable interpolation', () => {
const server: OpenAPIV3.ServerObject = {
url: 'https://example.com/{basePath}/',
variables: {
basePath: { default: 'v1' },
},
};
const result = interpolateServerURL(server);
expect(result).toBe('https://example.com/v1');
});
it('returns the URL with mixed placeholders and default values', () => {
const server: OpenAPIV3.ServerObject = {
url: 'https://{username}.example.com/{basePath}',
+4 -1
View File
@@ -19,7 +19,7 @@ export function getDefaultServerURL(servers: OpenAPIV3.ServerObject[]): string {
export function interpolateServerURL(server: OpenAPIV3.ServerObject) {
const parts = parseServerURL(server?.url ?? '');
return parts
const url = parts
.map((part) => {
if (part.kind === 'text') {
return part.text;
@@ -27,6 +27,9 @@ export function interpolateServerURL(server: OpenAPIV3.ServerObject) {
return server.variables?.[part.name]?.default ?? `{${part.name}}`;
})
.join('');
// Remove trailing slash to avoid double slashes when concatenated with paths
return url.endsWith('/') ? url.slice(0, -1) : url;
}
function parseServerURL(url: string) {