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

Mapster returning null for Collections instead of empty collection #227

Closed
hann4n opened this issue Mar 13, 2020 · 4 comments
Closed

Mapster returning null for Collections instead of empty collection #227

hann4n opened this issue Mar 13, 2020 · 4 comments

Comments

@hann4n
Copy link

hann4n commented Mar 13, 2020

I recently migrated to Mapster from Automapper.
We have a huge application and applied mapster and it looks good.
But we are facing an issue that When collections are mapped they are returning as null instead of empty collection when source object property is null.

Is there any workaround or feature so that we can have Empty collection instead of null just like Automapper.

Thanks

@chaowlert
Copy link
Collaborator

You can use AddDestinationTransform.

TypeAdapterConfig.GlobalSettings.Default
    .AddDestinationTransform((IReadOnlyList<ChildDto> list) => list ?? new List<ChildDto>());

Problem is you need to list all target types to handle null.

I think Mapster need to support this in future version.

@chaowlert
Copy link
Collaborator

Added generic support for AddDestinationTransform.

config.Default.AddDestinationTransform(DestinationTransform.EmptyCollectionIfNull);

@hann4n
Copy link
Author

hann4n commented Mar 14, 2020

Thank you very much. This is great

@hann4n
Copy link
Author

hann4n commented Mar 19, 2020

@chaowlert This is working great when Collections are a property of a class however when we map Collections directly its still returning null

var lst3 = mapster.Map<List<CA2>>(lst); returns null

Here I have tried using both config.Default.AddDestinationTransform((List<CA2> list) => list ?? new List<CA2>()); and config.Default.AddDestinationTransform(DestinationTransform.EmptyCollectionIfNull);

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

2 participants