Skip to content

question: object id is transformed into a completely different one #494

@leon19

Description

@leon19

Description

When the classToClass or plainToClass methods are used in conjunction with and ObjectId instance (using the latest version of the bson module) the object id becomes a completely different one

Minimal code-snippet showcasing the problem

npm install bson class-transformer
import { ObjectId } from 'bson';
import { plainToClass } from 'class-transformer';
import assert from 'assert';

class User {
  _id!: ObjectId;
}

const plainUser = { _id: new ObjectId() };
const user = plainToClass(User, plainUser);

assert(user._id.toHexString() === plainUser._id.toHexString());

Expected behavior

Assuming how this module works I expect user._id to be a different instance than plainUser._id but both sharing the same hexadecimal value

assert(user._id instanceof ObjectId);
assert(user._id !== plainUser._id);
assert(user._id.toHexString() === plainUser._id.toHexString());

Actual behavior

The id is converted into a completely different one

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions