question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot map `Value` or `HasValue` Property of `Nullable<T>`

See original GitHub issue

Describe 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:closed
  • Created 2 months ago
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
latonzcommented, Jul 17, 2023

@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

1reaction
latonzcommented, Jul 17, 2023

@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 on v2.9.0-next.3. The problem is that the target property has a auto-flattened name of ending with Value. When resolving the name, autoflattening resolves it to P.Value (the value property of Nullable<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. If Nullable<T> properties are treated as T while resolving mappable members, the problem shouldn’t occur.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found