Skip to content

Commit 99b5f50

Browse files
authored
Fixes operationId starting with integers crashes generation (#1326)
1 parent 9eb9c55 commit 99b5f50

File tree

7 files changed

+484
-4
lines changed

7 files changed

+484
-4
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
"swagger-typescript-api": patch
3+
---
4+
5+
Fix TypeScript generation failure for operationIds starting with numbers
6+
7+
**What:** Fixed an issue where operationIds starting with numbers (e.g., "123getUser") would cause TypeScript generation to fail due to invalid identifier names.
8+
9+
**Why:** OperationIds that start with numbers are not valid JavaScript identifiers, causing syntax errors in the generated TypeScript code.
10+
11+
**How:** Modified the template logic to quote property names for invalid identifiers. OperationIds starting with numbers are now generated as quoted properties (e.g., `"123GetUser": ...`) instead of unquoted invalid identifiers.
12+
13+
This resolves GitHub issue #952.

templates/base/route-type.ejs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { _, pascalCase, require } = utils;
44
const { query, payload, pathParams, headers } = route.request;
55
66
const routeDocs = includeFile("@base/route-docs", { config, route, utils });
7+
const isValidIdentifier = (name) => /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
78
const routeNamespace = pascalCase(route.routeName.usage);
89
910
%>
@@ -14,7 +15,7 @@ const routeNamespace = pascalCase(route.routeName.usage);
1415
<%~ routeDocs.lines %>
1516

1617
*/
17-
export namespace <%~ routeNamespace %> {
18+
export namespace <% if (isValidIdentifier(routeNamespace)) { %><%~ routeNamespace %><% } else { %>"<%~ routeNamespace %>"<% } %> {
1819
export type RequestParams = <%~ (pathParams && pathParams.type) || '{}' %>;
1920
export type RequestQuery = <%~ (query && query.type) || '{}' %>;
2021
export type RequestBody = <%~ (payload && payload.type) || 'never' %>;

templates/default/procedure-call.ejs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ const describeReturnType = () => {
7979
}
8080
}
8181
82+
const isValidIdentifier = (name) => /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
83+
8284
%>
8385
/**
8486
<%~ routeDocs.description %>
@@ -88,7 +90,7 @@ const describeReturnType = () => {
8890
<%~ routeDocs.lines %>
8991

9092
*/
91-
<%~ route.routeName.usage %><%~ route.namespace ? ': ' : ' = ' %>(<%~ wrapperArgs %>)<%~ config.toJS ? `: ${describeReturnType()}` : "" %> =>
93+
<% if (isValidIdentifier(route.routeName.usage)) { %><%~ route.routeName.usage %><%~ route.namespace ? ': ' : ' = ' %><% } else { %>"<%~ route.routeName.usage %>"<%~ route.namespace ? ': ' : ' = ' %><% } %>(<%~ wrapperArgs %>)<%~ config.toJS ? `: ${describeReturnType()}` : "" %> =>
9294
<%~ config.singleHttpClient ? 'this.http.request' : 'this.request' %><<%~ type %>, <%~ errorType %>>({
9395
path: `<%~ path %>`,
9496
method: '<%~ _.upperCase(method) %>',

templates/modular/procedure-call.ejs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ const describeReturnType = () => {
7979
}
8080
}
8181
82+
const isValidIdentifier = (name) => /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
83+
8284
%>
8385
/**
8486
<%~ routeDocs.description %>
@@ -88,7 +90,7 @@ const describeReturnType = () => {
8890
<%~ routeDocs.lines %>
8991

9092
*/
91-
<%~ route.routeName.usage %> = (<%~ wrapperArgs %>)<%~ config.toJS ? `: ${describeReturnType()}` : "" %> =>
93+
<% if (isValidIdentifier(route.routeName.usage)) { %><%~ route.routeName.usage %><%~ route.namespace ? ': ' : ' = ' %><% } else { %>"<%~ route.routeName.usage %>"<%~ route.namespace ? ': ' : ' = ' %><% } %>(<%~ wrapperArgs %>)<%~ config.toJS ? `: ${describeReturnType()}` : "" %> =>
9294
<%~ config.singleHttpClient ? 'this.http.request' : 'this.request' %><<%~ type %>, <%~ errorType %>>({
9395
path: `<%~ path %>`,
9496
method: '<%~ _.upperCase(method) %>',
@@ -98,4 +100,4 @@ const describeReturnType = () => {
98100
<%~ bodyContentKindTmpl ? `type: ${bodyContentKindTmpl},` : '' %>
99101
<%~ responseFormatTmpl ? `format: ${responseFormatTmpl},` : '' %>
100102
...<%~ _.get(requestConfigParam, "name") %>,
101-
})
103+
})<%~ route.namespace ? ',' : '' %>

0 commit comments

Comments
 (0)