Skip to content

Fixes operationId starting with integers crashes generation #1326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .changeset/fix-operationid-starting-with-number.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"swagger-typescript-api": patch
---

Fix TypeScript generation failure for operationIds starting with numbers

**What:** Fixed an issue where operationIds starting with numbers (e.g., "123getUser") would cause TypeScript generation to fail due to invalid identifier names.

**Why:** OperationIds that start with numbers are not valid JavaScript identifiers, causing syntax errors in the generated TypeScript code.

**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.

This resolves GitHub issue #952.
3 changes: 2 additions & 1 deletion templates/base/route-type.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { _, pascalCase, require } = utils;
const { query, payload, pathParams, headers } = route.request;

const routeDocs = includeFile("@base/route-docs", { config, route, utils });
const isValidIdentifier = (name) => /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
Copy link
Preview

Copilot AI Jul 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracting isValidIdentifier into a shared utility would avoid repeating the same regex in multiple template files and ensure consistency if validation rules change.

Copilot uses AI. Check for mistakes.

const routeNamespace = pascalCase(route.routeName.usage);

%>
Expand All @@ -14,7 +15,7 @@ const routeNamespace = pascalCase(route.routeName.usage);
<%~ routeDocs.lines %>

*/
export namespace <%~ routeNamespace %> {
export namespace <% if (isValidIdentifier(routeNamespace)) { %><%~ routeNamespace %><% } else { %>"<%~ routeNamespace %>"<% } %> {
export type RequestParams = <%~ (pathParams && pathParams.type) || '{}' %>;
export type RequestQuery = <%~ (query && query.type) || '{}' %>;
export type RequestBody = <%~ (payload && payload.type) || 'never' %>;
Expand Down
4 changes: 3 additions & 1 deletion templates/default/procedure-call.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ const describeReturnType = () => {
}
}

const isValidIdentifier = (name) => /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);

%>
/**
<%~ routeDocs.description %>
Expand All @@ -88,7 +90,7 @@ const describeReturnType = () => {
<%~ routeDocs.lines %>

*/
<%~ route.routeName.usage %><%~ route.namespace ? ': ' : ' = ' %>(<%~ wrapperArgs %>)<%~ config.toJS ? `: ${describeReturnType()}` : "" %> =>
<% if (isValidIdentifier(route.routeName.usage)) { %><%~ route.routeName.usage %><%~ route.namespace ? ': ' : ' = ' %><% } else { %>"<%~ route.routeName.usage %>"<%~ route.namespace ? ': ' : ' = ' %><% } %>(<%~ wrapperArgs %>)<%~ config.toJS ? `: ${describeReturnType()}` : "" %> =>
<%~ config.singleHttpClient ? 'this.http.request' : 'this.request' %><<%~ type %>, <%~ errorType %>>({
path: `<%~ path %>`,
method: '<%~ _.upperCase(method) %>',
Expand Down
6 changes: 4 additions & 2 deletions templates/modular/procedure-call.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ const describeReturnType = () => {
}
}

const isValidIdentifier = (name) => /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
Copy link
Preview

Copilot AI Jul 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The helper function isValidIdentifier is duplicated across multiple templates; consider extracting it into the shared utils object or a common helper file to centralize this logic and simplify future updates.

Copilot uses AI. Check for mistakes.


%>
/**
<%~ routeDocs.description %>
Expand All @@ -88,7 +90,7 @@ const describeReturnType = () => {
<%~ routeDocs.lines %>

*/
<%~ route.routeName.usage %> = (<%~ wrapperArgs %>)<%~ config.toJS ? `: ${describeReturnType()}` : "" %> =>
<% if (isValidIdentifier(route.routeName.usage)) { %><%~ route.routeName.usage %><%~ route.namespace ? ': ' : ' = ' %><% } else { %>"<%~ route.routeName.usage %>"<%~ route.namespace ? ': ' : ' = ' %><% } %>(<%~ wrapperArgs %>)<%~ config.toJS ? `: ${describeReturnType()}` : "" %> =>
<%~ config.singleHttpClient ? 'this.http.request' : 'this.request' %><<%~ type %>, <%~ errorType %>>({
path: `<%~ path %>`,
method: '<%~ _.upperCase(method) %>',
Expand All @@ -98,4 +100,4 @@ const describeReturnType = () => {
<%~ bodyContentKindTmpl ? `type: ${bodyContentKindTmpl},` : '' %>
<%~ responseFormatTmpl ? `format: ${responseFormatTmpl},` : '' %>
...<%~ _.get(requestConfigParam, "name") %>,
})
})<%~ route.namespace ? ',' : '' %>
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy