fix(typescript-fetch): alias url import to avoid shadowing by url-nam…#12756
Open
grbesicante1 wants to merge 2 commits into
Open
fix(typescript-fetch): alias url import to avoid shadowing by url-nam…#12756grbesicante1 wants to merge 2 commits into
grbesicante1 wants to merge 2 commits into
Conversation
…ed params A parameter literally named `url` (e.g. a webhook/callback query param) previously shadowed the module-level `import * as url from "url"` within the generated method, breaking url.parse/url.format and failing to compile under tsc. Aliasing the import to `nodeUrl` removes the collision without changing any generated public API (param names, FetchArgs.url, etc. are unaffected). Fixes swagger-api#12755
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's broken: In modules/swagger-codegen/src/main/resources/typescript-fetch/api.mustache, the file does import * as url from "url"; (line 5) and later calls url.parse(...) (line 104) and url.format(...) (line 238) inside each generated operation method. But generated method parameters are named directly from the OpenAPI spec's parameter names (line 93), with no collision check against url. AbstractTypeScriptClientCodegen's reserved-word list (lines 53-58) covers TS keywords and a few internal names but not url, and TypeScriptFetchClientCodegen doesn't add it either.
Result: Any operation with a parameter literally named url (e.g. GET /webhooks?url=...) generates a method like:
listWebhooks(url?: string, options: any = {}): FetchArgs {
const localVarUrlObj = url.parse(localVarPath, true); // url = the string param, not the module
...
return { url: url.format(localVarUrlObj), options: localVarRequestOptions };
}
Because the parameter is typed string, this isn't a subtle runtime issue — it's a hard tsc compile error: Property 'parse' does not exist on type 'string'.
Repro spec: minimal Swagger 2.0 doc with one GET operation taking a query param named url — included in full in the saved file.
Related prior art: #6698 (closed, fixed by PR #6717) fixed an almost identical collision for a parameter named path, but only by renaming the template's local variables to localVar*-prefixed names. That fix never touched the import * as url from "url" binding, so this specific case slipped through and is still present on current master (c7dd2fc, pom 2.4.53-SNAPSHOT).
Suggested fix (recommended): alias the import, e.g. import * as _nodeUrl from "url";, and use _nodeUrl.parse/_nodeUrl.format in the template — fixes the whole class of import-vs-parameter-name collisions without touching generated public API (param names, FetchArgs.url, etc. stay the same). Alternatives: add url to the reserved-word list so it gets escaped like other reserved names, or stop relying on a bare top-level import that any parameter name can shadow.