mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-20 17:43:24 +00:00
Strip trailing slash from OpenAPI server URLs (#4029)
This commit is contained in:
@@ -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 type { OpenAPIClientContext } from './context';
|
||||||
import { formatPath } from './formatPath';
|
import { formatPath } from './formatPath';
|
||||||
import type { OpenAPIServerWithCustomProperties } from './types';
|
import type { OpenAPIServerWithCustomProperties } from './types';
|
||||||
import { getDefaultServerURL } from './util/server';
|
import { getDefaultServerURL, interpolateServerURL } from './util/server';
|
||||||
import { createStateKey } from './utils';
|
import { createStateKey } from './utils';
|
||||||
|
|
||||||
export const serversStateKey = createStateKey('servers');
|
export const serversStateKey = createStateKey('servers');
|
||||||
@@ -29,11 +29,14 @@ export function OpenAPIPathMultipleServers(
|
|||||||
.filter(
|
.filter(
|
||||||
(server): server is OpenAPIServerWithCustomProperties & { url: string } => !!server.url
|
(server): server is OpenAPIServerWithCustomProperties & { url: string } => !!server.url
|
||||||
)
|
)
|
||||||
.map((server) => ({
|
.map((server) => {
|
||||||
key: server.url,
|
const url = interpolateServerURL(server);
|
||||||
label: server.url,
|
return {
|
||||||
description: server.description,
|
key: url,
|
||||||
}));
|
label: url,
|
||||||
|
description: server.description,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<OpenAPIPathItem
|
<OpenAPIPathItem
|
||||||
|
|||||||
@@ -23,6 +23,25 @@ describe('#interpolateServerURL', () => {
|
|||||||
expect(result).toBe('https://{username}.example.com/{basePath}');
|
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', () => {
|
it('returns the URL with mixed placeholders and default values', () => {
|
||||||
const server: OpenAPIV3.ServerObject = {
|
const server: OpenAPIV3.ServerObject = {
|
||||||
url: 'https://{username}.example.com/{basePath}',
|
url: 'https://{username}.example.com/{basePath}',
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ export function getDefaultServerURL(servers: OpenAPIV3.ServerObject[]): string {
|
|||||||
export function interpolateServerURL(server: OpenAPIV3.ServerObject) {
|
export function interpolateServerURL(server: OpenAPIV3.ServerObject) {
|
||||||
const parts = parseServerURL(server?.url ?? '');
|
const parts = parseServerURL(server?.url ?? '');
|
||||||
|
|
||||||
return parts
|
const url = parts
|
||||||
.map((part) => {
|
.map((part) => {
|
||||||
if (part.kind === 'text') {
|
if (part.kind === 'text') {
|
||||||
return part.text;
|
return part.text;
|
||||||
@@ -27,6 +27,9 @@ export function interpolateServerURL(server: OpenAPIV3.ServerObject) {
|
|||||||
return server.variables?.[part.name]?.default ?? `{${part.name}}`;
|
return server.variables?.[part.name]?.default ?? `{${part.name}}`;
|
||||||
})
|
})
|
||||||
.join('');
|
.join('');
|
||||||
|
|
||||||
|
// Remove trailing slash to avoid double slashes when concatenated with paths
|
||||||
|
return url.endsWith('/') ? url.slice(0, -1) : url;
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseServerURL(url: string) {
|
function parseServerURL(url: string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user