@@ -176,9 +176,27 @@ const addFormValue = async (form: FormData, key: string, value: unknown): Promis
176176 } else if ( Array . isArray ( value ) ) {
177177 await Promise . all ( value . map ( ( entry ) => addFormValue ( form , key + '[]' , entry ) ) ) ;
178178 } else if ( typeof value === 'object' ) {
179- await Promise . all (
180- Object . entries ( value ) . map ( ( [ name , prop ] ) => addFormValue ( form , `${ key } [${ name } ]` , prop ) ) ,
181- ) ;
179+ // Special case: env_vars should always be flattened for backward compatibility
180+ // with APIs that expect env_vars[KEY] format
181+ const shouldAlwaysFlatten = key === 'env_vars' ;
182+ // If the object doesn't contain any uploadable values,
183+ // serialize it as JSON instead of flattening it into bracketed keys.
184+ // This handles fields with contentType: application/json in the OpenAPI spec.
185+ if ( ! shouldAlwaysFlatten && ! hasUploadableValue ( value ) ) {
186+ // Filter out undefined values to check if object has any actual content
187+ const entries = Object . entries ( value ) . filter ( ( [ _ , v ] ) => v !== undefined ) ;
188+ if ( entries . length > 0 ) {
189+ form . append ( key , JSON . stringify ( value ) ) ;
190+ }
191+ // If all properties are undefined, don't add anything to the form
192+ } else {
193+ // Flatten objects that:
194+ // - Contain uploadable values (files/blobs), or
195+ // - Are explicitly marked to always flatten (like env_vars)
196+ await Promise . all (
197+ Object . entries ( value ) . map ( ( [ name , prop ] ) => addFormValue ( form , `${ key } [${ name } ]` , prop ) ) ,
198+ ) ;
199+ }
182200 } else {
183201 throw new TypeError (
184202 `Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${ value } instead` ,
0 commit comments