fix(typescript-fetch): add anyToJSON to runtime.ts#24216
Conversation
There was a problem hiding this comment.
1 issue found across 22 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="samples/client/others/typescript-fetch/additional-properties-in-multipart-issue/runtime.ts">
<violation number="1" location="samples/client/others/typescript-fetch/additional-properties-in-multipart-issue/runtime.ts:371">
P1: The new `anyToJSON` helper prevents the reported `ReferenceError`, but its raw pass-through implementation silently mis-serializes object or array values when they are appended to `FormData`. `FormData.append` stringifies non-Blob values via `String(value)`, so an object becomes `"[object Object]"` instead of a JSON string, which can corrupt multipart payloads at runtime without throwing. Consider using `JSON.stringify` for non-Blob objects so the function matches its `*ToJSON` naming and contract, or handling primitives and Blobs explicitly to avoid this silent data loss.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| } | ||
|
|
||
| // Pass-through serializer for `any`-typed properties in form data. See #1877. | ||
| export function anyToJSON(value: any): any { |
There was a problem hiding this comment.
P1: The new anyToJSON helper prevents the reported ReferenceError, but its raw pass-through implementation silently mis-serializes object or array values when they are appended to FormData. FormData.append stringifies non-Blob values via String(value), so an object becomes "[object Object]" instead of a JSON string, which can corrupt multipart payloads at runtime without throwing. Consider using JSON.stringify for non-Blob objects so the function matches its *ToJSON naming and contract, or handling primitives and Blobs explicitly to avoid this silent data loss.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/others/typescript-fetch/additional-properties-in-multipart-issue/runtime.ts, line 371:
<comment>The new `anyToJSON` helper prevents the reported `ReferenceError`, but its raw pass-through implementation silently mis-serializes object or array values when they are appended to `FormData`. `FormData.append` stringifies non-Blob values via `String(value)`, so an object becomes `"[object Object]"` instead of a JSON string, which can corrupt multipart payloads at runtime without throwing. Consider using `JSON.stringify` for non-Blob objects so the function matches its `*ToJSON` naming and contract, or handling primitives and Blobs explicitly to avoid this silent data loss.</comment>
<file context>
@@ -367,6 +367,11 @@ export function mapValues(data: any, fn: (item: any) => any) {
}
+// Pass-through serializer for `any`-typed properties in form data. See #1877.
+export function anyToJSON(value: any): any {
+ return value;
+}
</file context>
| export function anyToJSON(value: any): any { | |
| export function anyToJSON(value: any): any { | |
| if (value instanceof Blob) { | |
| return value; | |
| } | |
| if (typeof value === 'object' && value !== null) { | |
| return JSON.stringify(value); | |
| } | |
| return value; | |
| } |
There was a problem hiding this comment.
Thanks for the review!
anyToJSON is always called wrapped in JSON.stringify(anyToJSON(value)) in the generated code, so a plain pass-through is correct — adding JSON.stringify inside would cause double serialization.
377065a to
d14bcb2
Compare
|
Looking at the failed actions |
|
The 'Samples TS clients' check appears to be a pre-existing failure on master (all recent runs are failing as far as I can see). |
The generated API code calls anyToJSON() for any-typed properties in form data, but the function was never defined in runtime.ts, causing a ReferenceError at runtime. Fixes OpenAPITools#1877
d14bcb2 to
37c949c
Compare
|
agreed the TS Fetch sample failure is not caused by this PR i'll try to fix the CI tests later this week |
|
update: merged #24219 to fix the ts fetch tests |
The generated API code calls
anyToJSON()forany-typed properties in form data (e.g. photo, file uploads), but the function was never defined inruntime.ts, causing aReferenceErrorat runtime.Fixes #1877
PR checklist
./bin/generate-samples.sh ./bin/configs/typescript-fetch*cc @TiFu @taxpon @sebastianhaas @kenisteward @Vrolijkx @macjohnny @topce @akehir @petejohansonxo @amakhrov @davidgamero @mkusaka @joscha @dennisameling
Summary by cubic
Adds a pass-through
anyToJSONto thetypescript-fetchruntime and uses it for free-form object fields in multipart form data to prevent a runtime ReferenceError. Fixes #1877.Bug Fixes
anyToJSON(value: any): anyinruntime.ts.apis.mustacheto callruntime.anyToJSONfor free-form object fields; regenerate samples; add3_0/typescript-fetch/any_type_property.json.Dependencies
jaxrs-spec-enumpom.xml: upgrademaven-surefire-plugin/maven-failsafe-pluginto 3.5.6 and switch tests to JUnit 5 (org.junit.jupiter:junit-jupiter-engine5.14.4).Written for commit 37c949c. Summary will update on new commits.