Skip to content
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

Closed
2 of 9 tasks
roblopz opened this issue Jan 9, 2022 · 5 comments
Closed
2 of 9 tasks

"Invalid Date" result for Date[] mappings #397

roblopz opened this issue Jan 9, 2022 · 5 comments
Labels
bug Something isn't working

Comments

@roblopz
Copy link
Contributor

roblopz commented Jan 9, 2022

Is there an existing issue for this?

  • I have searched the existing issues

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

  • I don't know.
  • @automapper/core
  • @automapper/classes
  • @automapper/nestjs
  • @automapper/pojos
  • @automapper/sequelize
  • @automapper/types
  • Other (see below)

Other package and its version

No response

AutoMapper version

6.2.1

Additional context

No response

@roblopz roblopz added the bug Something isn't working label Jan 9, 2022
@micalevisk
Copy link
Contributor

I guess this is due to this limitation: https://automapperts.netlify.app/docs/misc/limitations#date-time

@roblopz
Copy link
Contributor Author

roblopz commented Jan 9, 2022

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?

@nartc
Copy link
Owner

nartc commented Jan 9, 2022

@roblopz I'll take a look tomorrow. Thank you for reporting

roblopz pushed a commit to roblopz/mapper that referenced this issue Jan 9, 2022
… 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
@roblopz
Copy link
Contributor Author

roblopz commented Jan 9, 2022

I've created a quite simple PR #398 that I think shouldn't break anything else.

Thanks @nartc, @micalevisk.

roblopz pushed a commit to roblopz/mapper that referenced this issue Jan 9, 2022
instantiate.util.ts@classes and map@core weren't mapping Date[] types correctly. Add checks for this
case.

nartc#397
@roblopz
Copy link
Contributor Author

roblopz commented Jan 9, 2022

New and corrected PR #399

Thanks!

roblopz pushed a commit to roblopz/mapper that referenced this issue Jan 11, 2022
Pojos instantiate util wasn't correctly handling Date[] types, adding support for them.

nartc#397
nartc pushed a commit that referenced this issue Jan 11, 2022
* 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>
@nartc nartc closed this as completed Jan 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants