Cannot map `Value` or `HasValue` Property of `Nullable<T>`
See original GitHub issueDescribe the bug I upgraded from next-2 to next-3 and now I am receiving a build error
ModelMapper.g.cs(705,78): error CS1061: 'JsonElement' does not contain a definition for 'Value' and no accessible extension method 'Value' accepting a first argument of type 'JsonElement' could be found (are you missing a using directive or an assembly reference?)
It looks like the generated code is calling .Value
twice:
// Generated code
{
var target = new global::SellerAmend()
{
Kind = source.Kind,
SellerPartnerId = source.SellerPartnerId,
SellerLoanId = source.SellerLoanId,
DateCreated = source.DateCreated
};
if (source.ListingAmendment != null)
{
// .Value.Value should just be .Value
target.ListingAmendmentValue = source.ListingAmendment.Value.Value.ToString();
}
return target;
}
Issue Analytics
- State:
- Created 2 months ago
- Comments:15 (6 by maintainers)
Top Results From Across the Web
How to map a nullable property to a DTO using AutoMapper?
A simple way to do this would be .ConstructUsing : Mapper.CreateMap<int?, string>() .ConstructUsing(i => i.HasValue ? i.Value.ToString() : null); ...
Read more >Nullable value types - C# reference
Value gets the value of an underlying type if HasValue is true . If HasValue is false , the Value property throws an...
Read more >AutoMapper 6.0 can't map nullable types containing null ...
Mapping an object that contains a nullable property which contains null throws an AutoMapperMappingException with the following message:.
Read more >C# Nullable Features thru the times - csharp.christiannagel.com
A value type cannot be null which made it difficult to map it with technologies that allow for optional values, such as the...
Read more >Understanding null safety
Since Null is no longer a subtype, no type except the special Null class permits the value null . We've made all types...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@TimothyMakkison The test you provided, which should reproduce the bug described by OP, fails on v2.8.0. So this probably exists since a longer time and isn’t a regression of
v2.9.0-next.3
@TimothyMakkison sorry my bad, I adjusted the test to verify that the bug does not exist. If I use exact your version, it fails on
v2.8.0
as well as onv2.9.0-next.3
. The problem is that the target property has a auto-flattened name of ending withValue
. When resolving the name, autoflattening resolves it toP.Value
(the value property ofNullable<T>
). Then when building the mapping, the builder encounters a nullable value type and adds another.Value
to access the value inside the null if condition. It relates to https://github.com/riok/mapperly/issues/572. IfNullable<T>
properties are treated asT
while resolving mappable members, the problem shouldn’t occur.