Skip to content

Commit b59afd5

Browse files
authored
Merge pull request #2674 from hey-api/copilot/fix-2673
2 parents f99690b + 762b20e commit b59afd5

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

.changeset/unlucky-foxes-arrive.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hey-api/openapi-ts": patch
3+
---
4+
5+
fix(renderer): replace default import placeholder
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import { TypeScriptRenderer } from '../renderer';
4+
5+
describe('TypeScriptRenderer', () => {
6+
describe('default import placeholder replacement', () => {
7+
it('should replace placeholders in default imports correctly', () => {
8+
const renderer = new TypeScriptRenderer();
9+
10+
// Create a mock project with symbols that have placeholders
11+
const project = {
12+
symbolIdToFiles: () => [],
13+
symbols: new Map(),
14+
} as any;
15+
16+
// Create a symbol with a placeholder
17+
const symbolId = 95;
18+
const symbol = {
19+
id: symbolId,
20+
name: 'foo',
21+
placeholder: '_heyapi_95_',
22+
};
23+
project.symbols.set(symbolId, symbol);
24+
25+
// Create a mock file
26+
const file = {
27+
resolvedNames: new Map([[symbolId, 'foo']]),
28+
} as any;
29+
30+
// Create bindings with a default import that has a placeholder
31+
const bindings = new Map();
32+
bindings.set('foo', {
33+
aliases: {},
34+
defaultBinding: '_heyapi_95_', // Contains placeholder that should be replaced
35+
from: 'foo',
36+
names: [],
37+
typeNames: [],
38+
});
39+
40+
// Generate import lines
41+
const importLines = renderer['getImportLines'](bindings, file, project);
42+
43+
// The import should use 'foo' not '_heyapi_95_'
44+
expect(importLines).toEqual(["import foo from 'foo';"]);
45+
});
46+
});
47+
});

packages/openapi-ts/src/generate/renderer.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,14 @@ export class TypeScriptRenderer implements Renderer {
281281
let isTypeOnly = false;
282282

283283
if (value.defaultBinding) {
284-
defaultBinding = tsc.identifier({ text: value.defaultBinding });
284+
const processedDefaultBinding = renderIds(
285+
value.defaultBinding,
286+
(symbolId) => {
287+
const symbol = project.symbols.get(symbolId);
288+
return this.replacerFn({ file, project, symbol });
289+
},
290+
);
291+
defaultBinding = tsc.identifier({ text: processedDefaultBinding });
285292
if (value.typeDefaultBinding) {
286293
isTypeOnly = true;
287294
}

0 commit comments

Comments
 (0)