-
-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: make instantiate assign undefined (instead of a new Date instance) for Date property when Source is undefined #254
Comments
Hi @abouroubi , thanks for the kind words.
Would that work? In addition, would you argue that |
Thanks for the quick reply. Indeed it's a lot of code rewrite for just the undefined part. My take on this, is that if my property is optional ( |
I'm with you @abouroubi I didn't understand why mapper/packages/classes/src/lib/utils/instantiate.util.ts Lines 51 to 54 in a062a77
mapper/packages/classes/src/lib/utils/instantiate.util.ts Lines 59 to 62 in a062a77
|
@abouroubi That would work but that would also increase the complexity in the core a lot. Introducing an option that affects how Automapper maps a property would require conditional check in various places, and the new option isn't actually mandatory when there are workarounds to achieve the same thing. That said, I'll change how |
I'm stuck in an issue while doing unit tests due to this I'm basically using the same approach as here https://github.com/nartc/mapper/blob/eaef6813ec6fa26cee4cc60094c14abf68b9e326/packages/nestjs-integration-test/src/app/profiles/user.profile.spec.ts In my test suite class User {
@AutoMap(() => Date)
someDateNullField: Date | null
}
class UserVm {
@AutoMap()
// @AutoMap(() => Date)
someDateNullField: Date | null
}
// ...
const user = new User()
user.someDateNullField = null
const expectedUser = new UserVm()
expectedUser.someDateNullField = null
const userVm = mapper.map(user, UserVm, User)
expect(userVm).toEqual(expectedUser) // this fails ✕
// because `userVm.someDateNullField` is `new Date()`
const usersVm = mapper.mapArray([user], UserVm, User)
expect(usersVm).toEqual([expectedUser]) // this passes, which is right @nartc can you please add this case in your user model fixture? |
@micalevisk Yeah, there is a slight inconsistency between |
Hi, and thanks for the great job with this new version.
I'm trying to understand how the classes mapper work, I have two situation where an empty class is created instead of just returning undefined.
The first is when I have an undefined object returned from the database, when I send it to it to the mapper, instead of returning just undefined, it returns an instance of the destination class with empty values.
The second is kind of the same but with properties of date type, I have a property lastEdited which is of type Date, and can be undefined, unfortunately when this value is undefined in the source object, automapper will initialize it with the current date, how can I avoid this behavior ?
The text was updated successfully, but these errors were encountered: