Skip to content

Commit d24d597

Browse files
committed
remove x-gitbook-token-placeholder for http
1 parent 8747382 commit d24d597

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

packages/react-openapi/src/OpenAPICodeSample.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,28 @@ describe('getSecurityHeaders', () => {
4949
Authorization: 'Token MY_CUSTOM_TOKEN',
5050
});
5151
});
52+
53+
it('should not use x-gitbook-prefix for http scheme', () => {
54+
const securities: OpenAPIOperationData['securities'] = [
55+
[
56+
'customAuth',
57+
{
58+
type: 'apiKey',
59+
in: 'header',
60+
name: 'Authorization',
61+
scheme: 'bearer',
62+
'x-gitbook-prefix': 'Token',
63+
},
64+
],
65+
];
66+
67+
const result = getSecurityHeaders({
68+
securityRequirement: [{ customAuth: [] }],
69+
securities,
70+
});
71+
72+
expect(result).toEqual({
73+
Authorization: 'Bearer YOUR_SECRET_TOKEN',
74+
});
75+
});
5276
});

packages/react-openapi/src/OpenAPICodeSample.tsx

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ export function getSecurityHeaders(args: {
314314
for (const security of selectedSecurity.schemes) {
315315
switch (security.type) {
316316
case 'http': {
317-
const scheme = security.scheme;
317+
// We do not use x-gitbook-prefix for http schemes to avoid confusion with the standard.
318+
let scheme = security.scheme;
318319
const defaultPlaceholderValue = scheme?.toLowerCase()?.includes('basic')
319320
? 'username:password'
320321
: 'YOUR_SECRET_TOKEN';
@@ -323,21 +324,17 @@ export function getSecurityHeaders(args: {
323324
defaultPlaceholderValue,
324325
});
325326

326-
// Use x-gitbook-prefix if provided, otherwise fall back to default logic
327-
let prefix = security['x-gitbook-prefix'];
328-
if (!prefix) {
329-
if (scheme?.includes('bearer')) {
330-
prefix = 'Bearer';
331-
} else if (scheme?.includes('basic')) {
332-
prefix = 'Basic';
333-
} else if (scheme?.includes('token')) {
334-
prefix = 'Token';
335-
} else {
336-
prefix = scheme ?? '';
337-
}
327+
if (scheme?.includes('bearer')) {
328+
scheme = 'Bearer';
329+
} else if (scheme?.includes('basic')) {
330+
scheme = 'Basic';
331+
} else if (scheme?.includes('token')) {
332+
scheme = 'Token';
333+
} else {
334+
scheme = scheme ?? '';
338335
}
339336

340-
headers.Authorization = `${prefix} ${format}`;
337+
headers.Authorization = `${scheme} ${format}`;
341338
break;
342339
}
343340
case 'apiKey': {

packages/react-openapi/src/util/tryit-prefill.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,8 @@ describe('resolvePrefillCodePlaceholderFromSecurityScheme (integration style)',
419419
it('should prioritize x-gitbook-prefill over x-gitbook-token-placeholder when both are present', () => {
420420
const result = resolvePrefillCodePlaceholderFromSecurityScheme({
421421
security: {
422-
type: 'http',
423-
scheme: 'bearer',
422+
type: 'apiKey',
423+
in: 'header',
424424
'x-gitbook-prefill': '{{ visitor.claims.apiToken }}',
425425
'x-gitbook-token-placeholder': 'API_TOKEN_KEY',
426426
},

0 commit comments

Comments
 (0)