Skip to content

Commit d6e88f6

Browse files
committed
fix: enhance ensureUniqueIdentifier to handle properties in $ref
1 parent 1d53b41 commit d6e88f6

File tree

1 file changed

+44
-7
lines changed
  • packages/openapi-ts/src/generate

1 file changed

+44
-7
lines changed

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

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,40 @@ export class TypeScriptFile {
340340
}
341341
}
342342

343+
function parseRefPath(ref: string): {
344+
baseRef: string;
345+
name: string;
346+
properties: string[];
347+
} {
348+
let baseRef = ref;
349+
const properties: string[] = [];
350+
351+
const parts = baseRef.split('/');
352+
let name = parts[parts.length - 1] || '';
353+
354+
let propIndex = parts.indexOf('properties');
355+
356+
if (propIndex !== -1) {
357+
baseRef = parts.slice(0, propIndex).join('/');
358+
name = parts[propIndex - 1] || '';
359+
360+
while (propIndex + 1 < parts.length) {
361+
const prop = parts[propIndex + 1];
362+
if (!prop) {
363+
throw new Error(`Invalid $ref: ${ref}`);
364+
}
365+
properties.push(prop);
366+
propIndex += 2;
367+
}
368+
}
369+
370+
return {
371+
baseRef,
372+
name,
373+
properties,
374+
};
375+
}
376+
343377
interface EnsureUniqueIdentifierData {
344378
$ref: string;
345379
case: StringCase | undefined;
@@ -360,8 +394,7 @@ const ensureUniqueIdentifier = ({
360394
nameTransformer,
361395
namespace,
362396
}: EnsureUniqueIdentifierData): Identifier => {
363-
const parts = $ref.split('/');
364-
const name = parts[parts.length - 1] || '';
397+
const { baseRef, name, properties } = parseRefPath($ref);
365398

366399
if (!name) {
367400
return {
@@ -370,11 +403,15 @@ const ensureUniqueIdentifier = ({
370403
};
371404
}
372405

373-
const refValue = namespace[$ref];
406+
const refValue = namespace[baseRef];
374407
if (refValue) {
408+
let name = refValue.name;
409+
if (properties.length) {
410+
name += properties.map((property) => `['${property}']`).join('');
411+
}
375412
return {
376413
created: false,
377-
name: refValue.name,
414+
name: name as string,
378415
};
379416
}
380417

@@ -390,15 +427,15 @@ const ensureUniqueIdentifier = ({
390427

391428
let nameValue = namespace[nameWithCasing];
392429
if (nameValue) {
393-
if (nameValue.$ref === $ref) {
430+
if (nameValue.$ref === baseRef) {
394431
return {
395432
created: false,
396433
name: nameValue.name,
397434
};
398435
}
399436

400437
return ensureUniqueIdentifier({
401-
$ref,
438+
$ref: baseRef,
402439
case: identifierCase,
403440
count: count + 1,
404441
create,
@@ -415,7 +452,7 @@ const ensureUniqueIdentifier = ({
415452
}
416453

417454
nameValue = {
418-
$ref,
455+
$ref: baseRef,
419456
name: ensureValidIdentifier(nameWithCasing),
420457
};
421458
namespace[nameWithCasing] = nameValue;

0 commit comments

Comments
 (0)