-
Notifications
You must be signed in to change notification settings - Fork 323
fix(typescript): guard against null/undefined in file upload core utility #16708
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
base: main
Are you sure you want to change the base?
Changes from all commits
73275ed
4f1cf60
43ab7f4
f112f94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| - summary: | | ||
| Fix `TypeError: Cannot use 'in' operator to search for 'path' in undefined` | ||
| crash when `null` or `undefined` is passed to a file upload parameter at | ||
| runtime. The error is now a clear `TypeError` describing the expected types | ||
| instead of a cryptic `in` operator failure. | ||
| type: fix |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,6 +79,11 @@ export class Node18FormData implements CrossPlatformFormData { | |
| } | ||
|
|
||
| public async appendFile(key: string, value: Uploadable): Promise<void> { | ||
| if (value == null) { | ||
| throw new Error( | ||
| `File upload for "${key}" received ${value === null ? "null" : "undefined"}. The generated code should not call appendFile with null/undefined — check that optional file fields are guarded before this call.` | ||
| ); | ||
| } | ||
| const { data, filename } = await toMultipartDataPart(value); | ||
|
|
||
| if (data instanceof Blob) { | ||
|
|
@@ -140,6 +145,11 @@ export class Node16FormData implements CrossPlatformFormData { | |
| } | ||
|
|
||
| public async appendFile(key: string, value: Uploadable): Promise<void> { | ||
| if (value == null) { | ||
| throw new Error( | ||
| `File upload for "${key}" received ${value === null ? "null" : "undefined"}. The generated code should not call appendFile with null/undefined — check that optional file fields are guarded before this call.` | ||
| ); | ||
| } | ||
| const { data, filename } = await toMultipartDataPart(value); | ||
|
|
||
| let bufferedValue; | ||
|
|
@@ -181,6 +191,11 @@ export class WebFormData implements CrossPlatformFormData { | |
| } | ||
|
|
||
| public async appendFile(key: string, value: Uploadable): Promise<void> { | ||
| if (value == null) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should throw here. appendFile should not be allowed to be called with null.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above — this is part of the same decision. If we throw in
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed — now throws. |
||
| throw new Error( | ||
| `File upload for "${key}" received ${value === null ? "null" : "undefined"}. The generated code should not call appendFile with null/undefined — check that optional file fields are guarded before this call.` | ||
| ); | ||
| } | ||
| const { data, filename, contentType } = await toMultipartDataPart(value); | ||
|
|
||
| if (data instanceof Blob) { | ||
|
|
@@ -221,6 +236,11 @@ export class FormDataWrapper { | |
| } | ||
|
|
||
| public async appendFile(key: string, value: Uploadable): Promise<void> { | ||
| if (value == null) { | ||
| throw new Error( | ||
| `File upload for "${key}" received ${value === null ? "null" : "undefined"}. The generated code should not call appendFile with null/undefined — check that optional file fields are guarded before this call.` | ||
| ); | ||
| } | ||
| const { data, filename, contentType } = await toMultipartDataPart(value); | ||
| const blob = await convertToBlob(data, contentType); | ||
| if (filename) { | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should throw here. appendFile should not be allowed to be called with null.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The warn+skip behavior here was specifically requested to match the old 3.12.x SDK tolerance — the customer's optional file field passes
undefinedthrough the generated code intoappendFile, and they expect it not to crash.If the intent is to throw here instead, the fix would need to move upstream: the generated code that calls
appendFilewould need to guard optional file fields withif (value != null)before callingappendFile. Without that generated-code change, throwing here would reproduce the original crash (just with a clearer error message).@cadesark — could you weigh in on the approach? Should we:
appendFile(current PR — tolerant core utility), orappendFilebut also fix the generated caller code to not callappendFilewith null for optional fields?Option 2 is the cleaner design but requires changes to the TypeScript SDK generator's request body serialization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done —
appendFilenow throws anErrorinstead of warn+skip. The generated code already guards optional file fields withif (request.maybe_file != null)before callingappendFile, so this is safe.