Flattening nullable nested objects #355
Replies: 2 comments 1 reply
-
Please ignore - sorry for the noise! I double-checked my code and it turns out some of the source properties in question were nullable but assigned to |
Beta Was this translation helpful? Give feedback.
-
What do I miss during flattening? In my opinion I have the same structure, like in the docs. Two EF Core 6 entity classes.
The child class:
I want to flatten it to a ProductDto class:
If I use pascal case notation in the dto class it works like intended, but it looks pretty stupid, having properties
But that leads into errors:
I tried several, different approaches, but ran into the same errors, or the mapping was just null. Does somebody see my mistake? What the heck do I oversee? ^^ |
Beta Was this translation helpful? Give feedback.
-
Hi all,
I've been encouraged to look at Mapperly to reduce the amount of custom mapping code I'm writing for a project.
The code it generates currently does not appear to null-check any lower than the first level of a nested object that's being flattened, which causes a
NullReferenceException
when a null is encountered.For example from the docs,
[MapProperty(Source = new[] { nameof(Car), nameof(Car.Make), nameof(Car.Make.Id) }, Target = new[] { nameof(CarDto), nameof(CarDto.MakeId) })]
If
car.Make.Id
is null this will throw.Is there any workaround for this problem? Ideally I'd like the code-gen to implement something like
car?.Make?.Id ?? default
whenMake
andId
are themselves nullable.Thanks!
Beta Was this translation helpful? Give feedback.
All reactions