[MNT-25274] Fixed issue while uploading files with multi-valued properties (#11182)

* [MNT-25274] Fixed issue while uploading files with multi-valued properties

* [MNT-25274] set a more specific type
This commit is contained in:
Mykyta Maliarchuk
2025-09-09 10:59:16 +02:00
committed by GitHub
parent e7024d1970
commit 923feccdb9
2 changed files with 30 additions and 2 deletions

View File

@@ -15,7 +15,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { isConstructor, getQueryParamsWithCustomEncoder, removeNilValues } from './alfresco-api.utils'; import { isConstructor, getQueryParamsWithCustomEncoder, removeNilValues, convertObjectToFormData } from './alfresco-api.utils';
describe('AlfrescoApiUtils', () => { describe('AlfrescoApiUtils', () => {
describe('isConstructor', () => { describe('isConstructor', () => {
@@ -93,4 +93,30 @@ describe('AlfrescoApiUtils', () => {
expect(actual?.getAll('key2')).toEqual(['value2', 'value3']); expect(actual?.getAll('key2')).toEqual(['value2', 'value3']);
}); });
}); });
describe('convertObjectToFormData', () => {
it('should create correct FormData entries for string values', () => {
const testParams: Record<string, string> = { name: 'file name', description: 'file description' };
const result = convertObjectToFormData(testParams);
expect(result.get('name')).toBe('file name');
expect(result.get('description')).toBe('file description');
});
it('should handle Blob files correctly', () => {
const testFile = new File(['content'], 'test.txt', { type: 'text/plain' });
const testParams: Record<string, Blob> = { file: testFile };
const result = convertObjectToFormData(testParams);
expect(result.get('file')).toEqual(testFile);
});
it('should create multiple entries with same key for arrays', () => {
const testParams: Record<string, Array<string>> = { categories: ['category1', 'category2', 'category3'] };
const result = convertObjectToFormData(testParams);
const values = result.getAll('categories');
expect(values).toEqual(['category1', 'category2', 'category3']);
});
});
}); });

View File

@@ -85,7 +85,7 @@ export const removeNilValues = (obj: Record<string | number, unknown>) => {
}, {}); }, {});
}; };
export const convertObjectToFormData = (formParams: Record<string | number, string | Blob>): FormData => { export const convertObjectToFormData = (formParams: Record<string | number, string | Blob | Array<string | Blob>>): FormData => {
const formData = new FormData(); const formData = new FormData();
for (const key in formParams) { for (const key in formParams) {
@@ -93,6 +93,8 @@ export const convertObjectToFormData = (formParams: Record<string | number, stri
const value = formParams[key]; const value = formParams[key];
if (value instanceof File) { if (value instanceof File) {
formData.append(key, value, value.name); formData.append(key, value, value.name);
} else if (Array.isArray(value)) {
value.forEach((item) => formData.append(key, item));
} else { } else {
formData.append(key, value); formData.append(key, value);
} }