[CSHARP-1885] Don't cast nullable types (int?, sbyte?, uint? ..) Created: 06/Jan/17  Updated: 07/Mar/17  Resolved: 07/Mar/17

Status: Closed
Project: C# Driver
Component/s: Linq
Affects Version/s: 2.4
Fix Version/s: None

Type: Bug Priority: Critical - P2
Reporter: Vorobjev Valery Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: Bug, cast, driver, nullable, query
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 10 (stable), mongodb 3.4, driver 2.4.1, .NET core


Attachments: PNG File bug1.png     PNG File success.png    
Issue Links:
Related
is related to CSHARP-1919 Filter builder Where method with null... Closed

 Description   

If class contains nullable property (example sbyte?) the query does not work!!! For Example: I have class with property unit.room_count (sbyte?). The final query should look as follows ...unit.room_count:

{...}

but query look as room_count:

{..}

.

In attache contains the correct version of the query. Bug1.png then room_count as sbyte?. Success.png then room_count as sbyte.



 Comments   
Comment by Robert Stam [ 07/Mar/17 ]

Since the issue reported by moc777 was separately resolved in CSHARP-1919 and no further information has been provided by the original submitter I am closing this ticket as "Gone Away".

Comment by Robert Stam [ 13/Feb/17 ]

moc777 Note also that the behavior is better but not yet completely correct in 2.4.2.

Comment by Robert Stam [ 13/Feb/17 ]

moc777 Thanks for the repro info. I'm not sure that your scenario is the same as that of the original submitter (his scenario involved the field name not being correct).

I've created a new JIRA ticket to describe your scenario:

https://jira.mongodb.org/browse/CSHARP-1919

I've simplified the scenario down to what I believe is the essence of the problem.

Comment by Matthew O'Connell [ 13/Feb/17 ]

I can confirm this bug.

Here's our example:

    public enum PaymentType 
    {
        TypeA,
        TypeB
    }
 
    public class FeeProductDataDb     
    {        
        public List<FeeDataDb> Fees { get; set; }
    }
 
    public class FeeDataDb
     {
        [BsonRepresentation(BsonType.String)]
        public PaymentType PaymentType { get; set; }
    }
 
    public class MyRepo
    {
          // fails under 2.4
          public async Task<IEnumerable<FeeProductDataDb>> GetFeeProductsAsync(PaymentType? paymentType = null)
        {
            return await (paymentType.HasValue ? _mongoCollection.Find(x => x.Fees.Any(f => f.PaymentType == paymentType)).ToListAsync() : _mongoCollection.Find(x => true).ToListAsync());
        }
 
        // the fix
        public async Task<IEnumerable<FeeProductDataDb>> GetProductsAsync(PaymentType? paymentType = null)
        {
            if (!paymentType.HasValue)
                return _mongoCollection.Find(x => true).ToListAsync();
            var pt = paymentType.Value;
            return _mongoCollection.Find(x => x.Fees.Any(f => f.PaymentType == pt)).ToListAsync();
        }
    }

Comment by Robert Stam [ 07/Feb/17 ]

There is not enough information here to attempt to reproduce this.

Can you please provide additional information? Primarily what is needed are the types and values of all the variables and the class definitions.

Thanks.

Generated at Wed Feb 07 21:40:56 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.