Skip to content

Commit 213ee3f

Browse files
committed
fix @type for generic interfaces in TypeScript clients
Type annotation in a comment should match the actual field type.
1 parent 8f72be1 commit 213ee3f

38 files changed

Lines changed: 64 additions & 72 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,25 +1107,17 @@ protected void updateDataTypeWithEnumForMap(CodegenProperty property) {
11071107
// First, set the property's enumName using the property itself (not the inner item)
11081108
property.enumName = toEnumName(property);
11091109

1110-
// Now use property.enumName for datatypeWithEnum instead of toEnumName(baseItem)
1111-
// This ensures the correct enum name (e.g., "ProjectRolesEnum") is used
1112-
// instead of the generic inner item name (e.g., "InnerEnum")
1113-
//
1114-
// TypeScript map types use index signature syntax: { [key: string]: ValueType; }
1115-
// We must only replace the VALUE type, not the key type (which must remain string/number).
1116-
// Target the value position by replacing "]: baseType" patterns.
1110+
// Replace only the LAST occurrence of baseType with enumName.
1111+
// In map types, the value type appears last (after the key type),
1112+
// so this approach is template-agnostic.
11171113
String datatypeWithEnum = property.datatypeWithEnum;
1118-
// Try replacing value type in nested context (e.g., "]: Array<string>>" or "]: string>")
1119-
String replaced = datatypeWithEnum.replace("]: " + baseItem.baseType + ">", "]: " + property.enumName + ">");
1120-
if (replaced.equals(datatypeWithEnum)) {
1121-
// Try replacing value type in simple map context (e.g., "]: string;")
1122-
replaced = datatypeWithEnum.replace("]: " + baseItem.baseType + ";", "]: " + property.enumName + ";");
1123-
}
1124-
if (replaced.equals(datatypeWithEnum)) {
1125-
// Fallback: replace container types like "Array<string>" or "Set<string>"
1126-
replaced = datatypeWithEnum.replace("<" + baseItem.baseType + ">", "<" + property.enumName + ">");
1114+
int lastIndex = datatypeWithEnum.lastIndexOf(baseItem.baseType);
1115+
if (lastIndex >= 0) {
1116+
property.datatypeWithEnum = datatypeWithEnum.substring(0, lastIndex)
1117+
+ property.enumName
1118+
+ datatypeWithEnum.substring(lastIndex + baseItem.baseType.length());
11271119
}
1128-
property.datatypeWithEnum = replaced;
1120+
LOGGER.info("Updated datatypeWithEnum for map property '{}': {}", property.name, property.datatypeWithEnum);
11291121

11301122
// set default value for variable with inner enum
11311123
if (property.defaultValue != null) {

modules/openapi-generator/src/main/resources/typescript-fetch/modelGenericInterfaces.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{
1010
{{#vars}}
1111
/**
1212
* {{#lambda.indented_star_4}}{{{unescapedDescription}}}{{/lambda.indented_star_4}}
13-
* @type {{=<% %>=}}{<%&datatype%>}<%={{ }}=%>
13+
* @type {{=<% %>=}}{<%&datatypeWithEnum%>}<%={{ }}=%>
1414
* @memberof {{classname}}
1515
{{#deprecated}}
1616
* @deprecated

samples/client/petstore/typescript-axios/builds/test-petstore/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ export type MammalAnyofTypeEnum = typeof MammalAnyofTypeEnum[keyof typeof Mammal
329329

330330
export interface MapTest {
331331
'map_map_of_string'?: { [key: string]: { [key: string]: string; }; };
332-
'map_of_enum_string'?: { [key: string]: string; };
332+
'map_of_enum_string'?: { [key: string]: MapTestMapOfEnumStringEnum; };
333333
'direct_map'?: { [key: string]: boolean; };
334334
'indirect_map'?: { [key: string]: boolean; };
335335
}

samples/client/petstore/typescript-axios/builds/with-fake-endpoints-models-for-testing-with-http-signature/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ export type Mammal = { className: 'whale' } & Whale | { className: 'zebra' } & Z
227227

228228
export interface MapTest {
229229
'map_map_of_string'?: { [key: string]: { [key: string]: string; }; };
230-
'map_of_enum_string'?: { [key: string]: string; };
230+
'map_of_enum_string'?: { [key: string]: MapTestMapOfEnumStringEnum; };
231231
'direct_map'?: { [key: string]: boolean; };
232232
'indirect_map'?: { [key: string]: boolean; };
233233
}

samples/client/petstore/typescript-fetch/builds/default-v3.0/models/EnumArrays.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ import { mapValues } from '../runtime';
2121
export interface EnumArrays {
2222
/**
2323
*
24-
* @type {string}
24+
* @type {EnumArraysJustSymbolEnum}
2525
* @memberof EnumArrays
2626
*/
2727
justSymbol?: EnumArraysJustSymbolEnum;
2828
/**
2929
*
30-
* @type {Array<string>}
30+
* @type {Array<EnumArraysArrayEnumEnum>}
3131
* @memberof EnumArrays
3232
*/
3333
arrayEnum?: Array<EnumArraysArrayEnumEnum>;

samples/client/petstore/typescript-fetch/builds/default-v3.0/models/EnumTest.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,25 @@ import {
5050
export interface EnumTest {
5151
/**
5252
*
53-
* @type {string}
53+
* @type {EnumTestEnumStringEnum}
5454
* @memberof EnumTest
5555
*/
5656
enumString?: EnumTestEnumStringEnum;
5757
/**
5858
*
59-
* @type {string}
59+
* @type {EnumTestEnumStringRequiredEnum}
6060
* @memberof EnumTest
6161
*/
6262
enumStringRequired: EnumTestEnumStringRequiredEnum;
6363
/**
6464
*
65-
* @type {number}
65+
* @type {EnumTestEnumIntegerEnum}
6666
* @memberof EnumTest
6767
*/
6868
enumInteger?: EnumTestEnumIntegerEnum;
6969
/**
7070
*
71-
* @type {number}
71+
* @type {EnumTestEnumNumberEnum}
7272
* @memberof EnumTest
7373
*/
7474
enumNumber?: EnumTestEnumNumberEnum;

samples/client/petstore/typescript-fetch/builds/default-v3.0/models/MapTest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ export interface MapTest {
2727
mapMapOfString?: { [key: string]: { [key: string]: string; }; };
2828
/**
2929
*
30-
* @type {{ [key: string]: string; }}
30+
* @type {{ [key: string]: MapTestMapOfEnumStringEnum; }}
3131
* @memberof MapTest
3232
*/
33-
mapOfEnumString?: { [key: string]: string; };
33+
mapOfEnumString?: { [key: string]: MapTestMapOfEnumStringEnum; };
3434
/**
3535
*
3636
* @type {{ [key: string]: boolean; }}

samples/client/petstore/typescript-fetch/builds/default-v3.0/models/Order.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export interface Order {
4545
shipDate?: Date;
4646
/**
4747
* Order Status
48-
* @type {string}
48+
* @type {OrderStatusEnum}
4949
* @memberof Order
5050
*/
5151
status?: OrderStatusEnum;

samples/client/petstore/typescript-fetch/builds/default-v3.0/models/ParentWithNullable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { type ChildWithNullable, ChildWithNullableFromJSONTyped, ChildWithNullab
2222
export interface ParentWithNullable {
2323
/**
2424
*
25-
* @type {string}
25+
* @type {ParentWithNullableTypeEnum}
2626
* @memberof ParentWithNullable
2727
*/
2828
type?: ParentWithNullableTypeEnum;

samples/client/petstore/typescript-fetch/builds/default-v3.0/models/Pet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export interface Pet {
6666
tags?: Array<Tag>;
6767
/**
6868
* pet status in the store
69-
* @type {string}
69+
* @type {PetStatusEnum}
7070
* @memberof Pet
7171
*/
7272
status?: PetStatusEnum;

0 commit comments

Comments
 (0)