@@ -340,6 +340,40 @@ export class TypeScriptFile {
340
340
}
341
341
}
342
342
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
+
343
377
interface EnsureUniqueIdentifierData {
344
378
$ref : string ;
345
379
case : StringCase | undefined ;
@@ -360,8 +394,7 @@ const ensureUniqueIdentifier = ({
360
394
nameTransformer,
361
395
namespace,
362
396
} : EnsureUniqueIdentifierData ) : Identifier => {
363
- const parts = $ref . split ( '/' ) ;
364
- const name = parts [ parts . length - 1 ] || '' ;
397
+ const { baseRef, name, properties } = parseRefPath ( $ref ) ;
365
398
366
399
if ( ! name ) {
367
400
return {
@@ -370,11 +403,15 @@ const ensureUniqueIdentifier = ({
370
403
} ;
371
404
}
372
405
373
- const refValue = namespace [ $ref ] ;
406
+ const refValue = namespace [ baseRef ] ;
374
407
if ( refValue ) {
408
+ let name = refValue . name ;
409
+ if ( properties . length ) {
410
+ name += properties . map ( ( property ) => `['${ property } ']` ) . join ( '' ) ;
411
+ }
375
412
return {
376
413
created : false ,
377
- name : refValue . name ,
414
+ name : name as string ,
378
415
} ;
379
416
}
380
417
@@ -390,15 +427,15 @@ const ensureUniqueIdentifier = ({
390
427
391
428
let nameValue = namespace [ nameWithCasing ] ;
392
429
if ( nameValue ) {
393
- if ( nameValue . $ref === $ref ) {
430
+ if ( nameValue . $ref === baseRef ) {
394
431
return {
395
432
created : false ,
396
433
name : nameValue . name ,
397
434
} ;
398
435
}
399
436
400
437
return ensureUniqueIdentifier ( {
401
- $ref,
438
+ $ref : baseRef ,
402
439
case : identifierCase ,
403
440
count : count + 1 ,
404
441
create,
@@ -415,7 +452,7 @@ const ensureUniqueIdentifier = ({
415
452
}
416
453
417
454
nameValue = {
418
- $ref,
455
+ $ref : baseRef ,
419
456
name : ensureValidIdentifier ( nameWithCasing ) ,
420
457
} ;
421
458
namespace [ nameWithCasing ] = nameValue ;
0 commit comments