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

Is there an easy way to deal with Source object having null values #15

Closed
hwong668 opened this issue Jul 18, 2016 · 3 comments
Closed

Comments

@hwong668
Copy link

We have a nested source object like:
{
homeAddress: {
address1: '100 Main St',
address2: 'Apt# 1',
city: 'Los Angeles',
state: 'CA',
zip: '90000'
},
businessAddress: {
address1: '200 Main St',
city: 'Los Angeles',
state: 'CA',
zip: '90000'
}
}

However, homeAddress sometimes could be null because businessAddress is filled in. When I try to map the source object to a DTO, I'll get an exception because automapper cannot read homeAddress.address1. Is there a way to initialize automapper to map anything it couldn't resolve to null? in this case (homeAddress = null) the destination object should simply be:
{
homeAddress: null,
businessAddress: {
address1: '200 Main St',
address2: null,
city: 'Los Angeles',
state: 'CA',
zip: '90000'
}
}

thanks

@adamjbradley
Copy link

adamjbradley commented Sep 6, 2016

I have a similar situation, though for me, using your data in my example homeAddress would not be present at all! This is true of all other attributes in the inbound message - it's a SCIM 2.0 PUT request. Hoping there's a neat way to use AutoMapper to cater for this.

So what I'm really after is map and transform only if the source attribute exits.

@loedeman
Copy link
Owner

loedeman commented Sep 6, 2016

Hi both, thanks for commenting! I have been busy lately starting my own IT consultancy firm ;). I will try to react in detail somewhere this week! Thanks for your patience! Cheers, Bert

@loedeman
Copy link
Owner

loedeman commented Sep 8, 2016

Hi both, I have just pushed a fix (1.7.2) to the repository. I will push it to NPM as soon as possible. Please let me know if this fix does work for you. FYI, if you specify nested mappings, you currently have to specify all of them, since the current solution does either use auto mapping or property mapping. I did already write a unit test to reproduce this issue, the fix is a bit harder than I would like to implement at this very moment ;)...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants