Skip to content

Commit f77b0b7

Browse files
committed
Merge branch 'master' of github.com:ferdikoomen/openapi-typescript-codegen
# Conflicts: # src/templates/core/fetch/getHeaders.hbs # src/templates/core/node/getHeaders.hbs # src/templates/core/xhr/getHeaders.hbs
2 parents 691794b + 4f056c6 commit f77b0b7

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

src/openApi/v3/parser/getRef.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,28 @@ describe('getRef', () => {
3434
type: 'integer',
3535
});
3636
});
37+
38+
it('should produce correct result for encoded ref path', () => {
39+
expect(
40+
getRef(
41+
{
42+
openapi: '3.0',
43+
info: {
44+
title: 'dummy',
45+
version: '1.0',
46+
},
47+
paths: {
48+
'/api/user/{id}': {
49+
description: 'This is an Example path',
50+
},
51+
},
52+
},
53+
{
54+
$ref: '#/paths/~1api~1user~1%7Bid%7D',
55+
}
56+
)
57+
).toEqual({
58+
description: 'This is an Example path',
59+
});
60+
});
3761
});

src/openApi/v3/parser/getRef.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import type { OpenApi } from '../interfaces/OpenApi';
22
import type { OpenApiReference } from '../interfaces/OpenApiReference';
33

4+
const escapedSlash = /~1/g;
5+
const escapedTilde = /~0/g;
6+
47
export function getRef<T>(openApi: OpenApi, item: T & OpenApiReference): T {
58
if (item.$ref) {
69
// Fetch the paths to the definitions, this converts:
@@ -14,6 +17,7 @@ export function getRef<T>(openApi: OpenApi, item: T & OpenApiReference): T {
1417
// if we cannot find it, then we throw an error.
1518
let result: any = openApi;
1619
paths.forEach((path: string): void => {
20+
path = decodeURIComponent(path.replace(escapedSlash, '/').replace(escapedTilde, '~'));
1721
if (result.hasOwnProperty(path)) {
1822
result = result[path];
1923
} else {

test/__snapshots__/index.spec.js.snap

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,20 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
165165
const password = await resolve(options, OpenAPI.PASSWORD);
166166
const defaultHeaders = await resolve(options, OpenAPI.HEADERS);
167167

168-
const headers = new Headers({
168+
const filteredHeaders = Object.entries({
169169
Accept: 'application/json',
170170
...defaultHeaders,
171171
...options.headers,
172-
});
172+
}).reduce((acc, [headerKey, headerValue]) => {
173+
if (typeof headerValue !== 'undefined') {
174+
return {
175+
...acc,
176+
[headerKey]: headerValue,
177+
};
178+
}
179+
return acc;
180+
}, {});
181+
const headers = new Headers(filteredHeaders);
173182

174183
if (isStringWithValue(token)) {
175184
headers.append('Authorization', \`Bearer \${token}\`);
@@ -2520,11 +2529,20 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
25202529
const password = await resolve(options, OpenAPI.PASSWORD);
25212530
const defaultHeaders = await resolve(options, OpenAPI.HEADERS);
25222531

2523-
const headers = new Headers({
2532+
const filteredHeaders = Object.entries({
25242533
Accept: 'application/json',
25252534
...defaultHeaders,
25262535
...options.headers,
2527-
});
2536+
}).reduce((acc, [headerKey, headerValue]) => {
2537+
if (typeof headerValue !== 'undefined') {
2538+
return {
2539+
...acc,
2540+
[headerKey]: headerValue,
2541+
};
2542+
}
2543+
return acc;
2544+
}, {});
2545+
const headers = new Headers(filteredHeaders);
25282546

25292547
if (isStringWithValue(token)) {
25302548
headers.append('Authorization', \`Bearer \${token}\`);

0 commit comments

Comments
 (0)