Skip to content

Commit 39bf645

Browse files
committed
fix(transformers): use symbols for additionalProperties key variables
Replace literal string 'key' with plugin.symbol('key') in the additionalProperties for-loop generation. This ensures the planner assigns unique names (key, key2, key3, ...) when nested or sibling additionalProperties produce multiple for-loops, preventing the TypeScript error caused by duplicate variable names in nested scopes. Closes #3838
1 parent 216ba90 commit 39bf645

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

  • packages
    • openapi-ts-tests/main/test/__snapshots__/3.1.x/transformers-additional-properties
    • openapi-ts/src/plugins/@hey-api/transformers

packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/transformers-additional-properties/transformers.gen.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ const barSchemaResponseTransformer = (data: any) => {
1313
data.baz[key] = fooSchemaResponseTransformer(data.baz[key]);
1414
}
1515
if (data.qux) {
16-
for (const key of Object.keys(data.qux)) {
17-
if (!['quux'].includes(key)) {
18-
data.qux[key] = new Date(data.qux[key]);
16+
for (const key2 of Object.keys(data.qux)) {
17+
if (!['quux'].includes(key2)) {
18+
data.qux[key2] = new Date(data.qux[key2]);
1919
}
2020
}
2121
}

packages/openapi-ts/src/plugins/@hey-api/transformers/plugin.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,17 @@ function processSchemaType({
195195
}
196196

197197
if (schema.additionalProperties && dataExpression) {
198+
const key = plugin.symbol('key');
198199
const entryValueNodes = processSchemaType({
199-
dataExpression: $(dataExpression).attr('key').computed(),
200+
dataExpression: $(dataExpression).attr(key).computed(),
200201
plugin,
201202
schema: schema.additionalProperties,
202203
});
203204

204205
if (entryValueNodes.length) {
205206
const properties = Object.keys(schema.properties ?? {});
206207
nodes.push(
207-
$.for($.const('key'))
208+
$.for($.const(key))
208209
.of($('Object').attr('keys').call(dataExpression))
209210
.$if(
210211
properties.length,
@@ -214,7 +215,7 @@ function processSchemaType({
214215
$.not(
215216
$.array(...properties)
216217
.attr('includes')
217-
.call('key'),
218+
.call(key),
218219
),
219220
).do(...entryValueNodes),
220221
),

0 commit comments

Comments
 (0)