Driver-LINQ discriminator filter uses raw value, mismatching a value-converted/represented discriminator (OfType/polymorphic queries return empty)

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Fixed
    • Priority: Unknown
    • 10.0.3, 9.1.3, 8.4.3
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Dotnet Drivers
    • Not Needed
    • None
    • None
    • None
    • None
    • None
    • None

      Pre-existing driver-LINQ correctness bug surfaced during EF-347 (native OfType). NOT introduced by EF-347EF-347 preserves parity by making the native path fall back for this case (see below).

      Mechanism: a TPH discriminator is a normal IProperty, so it is WRITTEN through its property serializer — which applies any Mongo:BsonRepresentation / value converter configured on the discriminator property. The stored t value is therefore the _converted/represented value. But the driver-LINQ polymorphic filter is generated from MongoEFDiscriminator.GetDiscriminator / GetDiscriminatorsForTypeAndSubTypes (src/MongoDB.EntityFrameworkCore/Serializers/MongoEFDiscriminator.cs:39-51), which builds the filter value via BsonValue.Create(entityType.GetDiscriminatorValue()) — the RAW discriminator value, bypassing the converter/representation. So the filter looks for the raw value while the stored _t holds the converted value → they never match → OfType<Derived>() (and any discriminator-narrowed / polymorphic query) returns EMPTY on the driver-LINQ path for a model whose discriminator property is value-converted or has a non-default BsonRepresentation.

      Scope note: no model in the repo currently configures a converted/represented discriminator, so this is latent. EF-347 (native OfType) intentionally does NOT diverge — its TryBuildDiscriminatorPredicate guard rejects a value-converted/non-default-BsonRepresentation discriminator and falls back to driver-LINQ (mirroring NativeGroupByBinder.HasDefaultKeySerialization), so native == driver-LINQ (both empty) rather than native being silently more-correct-but-different. Interestingly the native predicate (which serializes through the property serializer via MongoConstantExpression.ForSerialization) WOULD match the stored _t — so fixing this bug and then removing the native guard would let native handle represented discriminators correctly.

      Fix: make MongoEFDiscriminator.GetDiscriminator/GetDiscriminatorsForTypeAndSubTypes produce the filter value using the discriminator property's serializer (same transform as the write path) instead of raw BsonValue.Create, so the filter matches the stored _t. Add a functional test with a value-converted (and a BsonRepresentation) discriminator asserting OfType<Derived>() returns the correct rows under DriverLinq (currently empty). Once fixed, revisit EF-347's OfType guard (it could then admit represented discriminators natively). Related: EF-347 (native OfType), EF-345/EF-346 (native GroupBy follow-ups).

            Assignee:
            Arthur Vickers
            Reporter:
            Arthur Vickers
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: