Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/TransformOperationExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ export class TransformOperationExecutor {
} else if (!!getGlobal().Buffer && (targetType === Buffer || value instanceof Buffer) && !isMap) {
if (value === null || value === undefined) return value;
return Buffer.from(value);
} else if ((targetType === Uint8Array || value instanceof Uint8Array) && !isMap) {
if (value === null || value === undefined) return value;
return new Uint8Array(value);
} else if (isPromise(value) && !isMap) {
return new Promise((resolve, reject) => {
value.then(
Expand Down
23 changes: 23 additions & 0 deletions test/functional/basic-functionality.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,29 @@ describe('basic functionality', () => {
});
});

it('should preserve Uint8Array values when converting to class instances', () => {
defaultMetadataStorage.clear();

class User {
@Type(() => Uint8Array)
bytes: Uint8Array;
}

const bytes = new Uint8Array([1, 2, 3]);
const fromPlainUser = { bytes };

const transformedUser = plainToInstance(User, fromPlainUser);
expect(transformedUser).toBeInstanceOf(User);
expect(transformedUser.bytes).toBeInstanceOf(Uint8Array);
expect(Array.from(transformedUser.bytes)).toEqual([1, 2, 3]);

const fromExistUser = new User();
const fromExistTransformedUser = plainToClassFromExist(fromExistUser, fromPlainUser);
expect(fromExistTransformedUser).toBe(fromExistUser);
expect(fromExistTransformedUser.bytes).toBeInstanceOf(Uint8Array);
expect(Array.from(fromExistTransformedUser.bytes)).toEqual([1, 2, 3]);
});

it('should transform nested objects too and make sure their decorators are used too', () => {
defaultMetadataStorage.clear();

Expand Down