-
-
Notifications
You must be signed in to change notification settings - Fork 91
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
"Invalid Date" result for Date[] mappings #397
Comments
I guess this is due to this limitation: https://automapperts.netlify.app/docs/misc/limitations#date-time |
The thing is, even when providing a custom mapFrom function on the configuration, I'm getting my source object with this 'Invalid Date' value instead of the Date[] value.. any workaround you can think of? |
@roblopz I'll take a look tomorrow. Thank you for reporting |
… source object instantiation instantiation of source object mappings are incorrectly treating Date[] types as Date (by only validating metadata info). Adding Array check. nartc#397
I've created a quite simple PR #398 that I think shouldn't break anything else. Thanks @nartc, @micalevisk. |
instantiate.util.ts@classes and map@core weren't mapping Date[] types correctly. Add checks for this case. nartc#397
New and corrected PR #399 Thanks! |
Pojos instantiate util wasn't correctly handling Date[] types, adding support for them. nartc#397
* fix(classes, core): add ability to map Date[] types instantiate.util.ts@classes and map@core weren't mapping Date[] types correctly. Add checks for this case. #397 * fix(pojos): adding pojos support for Date[] types Pojos instantiate util wasn't correctly handling Date[] types, adding support for them. #397 Co-authored-by: Roberto <macbookpro@Robertos-MacBook-Pro.local>
Is there an existing issue for this?
Describe the issue
It seems there's a bug on the classes plugin when mapping properties of type Date[]. Date[] properties are mapped to 'Invalid Date' instead of the expected array of date objects.
Models/DTOs/VMs
export class Invite {
@AutoMap({ typeFn: () => String })
_id: string;
@AutoMap({ typeFn: () => String })
main: string;
@AutoMap({ typeFn: () => Date })
logins?: Date[];
@AutoMap({ typeFn: () => Date })
sentAt: Date;
}
export class InviteDTO {
@AutoMap({ typeFn: () => String })
_id: string;
@AutoMap({ typeFn: () => String })
main: string;
@AutoMap({ typeFn: () => Date })
logins?: Date[];
@AutoMap({ typeFn: () => Date })
sentAt: Date;
}
Mapping configuration
import { createMapper } from '@automapper/core';
import { classes } from '@automapper/classes';
export const mapper = createMapper({
name: 'appMapper',
pluginInitializer: classes
});
Steps to reproduce
/*
Using above described classes and mapping config...
*/
function inviteFactory() {
const invite = new Invite();
invite._id = 'SomeId';
invite.main = 'SomeString';
invite.sentAt = new Date();
invite.logins = [new Date(), new Date(), new Date()];
return invite;
}
const invite = inviteFactory();
const dto = this.mapper.map(invite, InviteDTO, Invite);
// OK, Date-type is correctly being mapped
expect(dto.sentAt).toBeValidDate();
// FAIL, Date[]-type is being mapped to an invalid date object
expect(dto.logins).toBeAnArray();
// FAIL, dto.logins is NOT an array here as it should be, it's an invalid date object
dto.logins.forEach(date => expect(date).toBeValidDate);
Expected behavior
Date[] properties are correctly mapped.
Screenshots
No response
Minimum reproduction code
No response
Package
@automapper/core
@automapper/classes
@automapper/nestjs
@automapper/pojos
@automapper/sequelize
@automapper/types
Other package and its version
No response
AutoMapper version
6.2.1
Additional context
No response
The text was updated successfully, but these errors were encountered: