-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5757: The problem of filtering by derived types #1812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+217
−6
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| /* Copyright 2010-present MongoDB Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using MongoDB.Driver.TestHelpers; | ||
| using FluentAssertions; | ||
| using MongoDB.Bson; | ||
| using MongoDB.Bson.IO; | ||
| using MongoDB.Bson.Serialization; | ||
| using MongoDB.Bson.Serialization.Conventions; | ||
| using MongoDB.Bson.Serialization.Serializers; | ||
| using Xunit; | ||
|
|
||
| namespace MongoDB.Driver.Tests.Jira; | ||
|
|
||
| public class CSharp5757Tests : LinqIntegrationTest<CSharp5757Tests.ClassFixture> | ||
| { | ||
| static CSharp5757Tests() | ||
| { | ||
| var scalarDiscriminatorConvention = new AnimalDiscriminatorConvention(); | ||
| var hierarchicalDiscriminatorConvention = new PersonDiscriminatorConvention(); | ||
| BsonSerializer.RegisterDiscriminatorConvention(typeof(Animal), scalarDiscriminatorConvention); | ||
| BsonSerializer.RegisterDiscriminatorConvention(typeof(Person), hierarchicalDiscriminatorConvention); | ||
| } | ||
|
|
||
| public CSharp5757Tests(ClassFixture fixture) | ||
| : base(fixture) | ||
| { | ||
| } | ||
|
|
||
| [Fact] | ||
| public void HierarchicalDiscriminator_with_Filter_OfType_HealthCareWorker_should_throw() | ||
| { | ||
| var collection = Fixture.PersonCollection; | ||
| var filter = Builders<Person>.Filter.OfType<HealthCareWorker>(); | ||
|
|
||
| var renderedArgs = | ||
| new RenderArgs<Person>(collection.DocumentSerializer, BsonSerializer.SerializerRegistry); | ||
|
|
||
| var exception = Record.Exception(() => filter.Render(renderedArgs)); | ||
| exception.Should().BeOfType<NotSupportedException>(); | ||
| exception.Message.Should().Be("Discriminator value for type HealthCareWorker is null which is not allowed with hierarchical discriminator conventions."); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void HierarchicalDiscriminator_with_Queryable_OfType_HealthCareWorker_should_throw() | ||
| { | ||
| var collection = Fixture.PersonCollection; | ||
| var queryable = collection.AsQueryable() | ||
| .OfType<HealthCareWorker>(); | ||
|
|
||
| var exception = Record.Exception(() => Translate(collection, queryable)); | ||
| exception.Should().BeOfType<NotSupportedException>(); | ||
| exception.Message.Should().Be("Discriminator value for type HealthCareWorker is null which is not allowed with hierarchical discriminator conventions."); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ScalarDiscriminator_with_Filter_OfType_Mammal_should_work() | ||
| { | ||
| var collection = Fixture.AnimalCollection; | ||
| var filter = Builders<Animal>.Filter.OfType<Mammal>(); | ||
|
|
||
| var renderedFilter = filter.Render(new RenderArgs<Animal>(collection.DocumentSerializer, BsonSerializer.SerializerRegistry)); | ||
| renderedFilter.Should().Be("{ _t : { $in : ['Cat', 'Dog'] } }"); | ||
|
|
||
| var results = collection.FindSync(filter).ToList(); | ||
| results.Select(x => x.Id).Should().Equal(1, 2); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ScalarDiscriminator_with_Queryable_OfType_Mammal_should_work() | ||
| { | ||
| var collection = Fixture.AnimalCollection; | ||
|
|
||
| var queryable = collection.AsQueryable() | ||
| .OfType<Mammal>(); | ||
|
|
||
| var stages = Translate(collection, queryable); | ||
| AssertStages(stages, "{ $match : { _t : { $in : ['Cat', 'Dog'] } } }"); | ||
|
|
||
| var results = queryable.ToList(); | ||
| results.Select(x => x.Id).Should().Equal(1, 2); | ||
| } | ||
|
|
||
| public abstract class Person | ||
| { | ||
| } | ||
|
|
||
| public abstract class HealthCareWorker : Person | ||
| { | ||
| } | ||
|
|
||
| public class Doctor : HealthCareWorker | ||
| { | ||
| } | ||
|
|
||
| public class Nurse : HealthCareWorker | ||
| { | ||
| } | ||
|
|
||
| public class PersonDiscriminatorConvention : IHierarchicalDiscriminatorConvention | ||
| { | ||
| public string ElementName => "_t"; | ||
|
|
||
| public Type GetActualType(IBsonReader bsonReader, Type nominalType) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| public BsonValue GetDiscriminator(Type nominalType, Type actualType) | ||
| => actualType.IsAbstract ? null : actualType.Name; | ||
| } | ||
|
|
||
| public abstract class Animal | ||
| { | ||
| public int Id { get; set; } | ||
| } | ||
|
|
||
| public abstract class Mammal : Animal | ||
| { | ||
| } | ||
|
|
||
| public class Cat : Mammal | ||
| { | ||
| } | ||
|
|
||
| public class Dog : Mammal | ||
| { | ||
| } | ||
|
|
||
| public class AnimalDiscriminatorConvention : IScalarDiscriminatorConvention | ||
| { | ||
| public string ElementName => "_t"; | ||
|
|
||
| public Type GetActualType(IBsonReader bsonReader, Type nominalType) | ||
| { | ||
| var discriminatorValue = ReadDiscriminatorValue(bsonReader); | ||
| return discriminatorValue switch | ||
| { | ||
| "Cat" => typeof(Cat), | ||
| "Dog" => typeof(Dog), | ||
| _ => throw new Exception($"Invalid discriminator value: {discriminatorValue}.") | ||
| }; | ||
| } | ||
|
|
||
| public BsonValue GetDiscriminator(Type nominalType, Type actualType) | ||
| => actualType.IsAbstract ? null : actualType.Name; | ||
|
|
||
| public BsonValue[] GetDiscriminatorsForTypeAndSubTypes(Type type) | ||
| => type.Name switch | ||
| { | ||
| "Animal" => ["Cat", "Dog"], | ||
| "Mammal" => ["Cat", "Dog"], | ||
| "Cat" => ["Cat"], | ||
| "Dog" => ["Dog"], | ||
| _ => throw new ArgumentException($"Invalid type: {type.Name}.") | ||
| }; | ||
|
|
||
| private string ReadDiscriminatorValue(IBsonReader bsonReader) | ||
| { | ||
| string discriminatorValue = null; | ||
|
|
||
| var bsonType = bsonReader.GetCurrentBsonType(); | ||
| if (bsonType == BsonType.Document) | ||
| { | ||
| var bookmark = bsonReader.GetBookmark(); | ||
| bsonReader.ReadStartDocument(); | ||
| if (bsonReader.FindElement("_t")) | ||
| { | ||
| var context = BsonDeserializationContext.CreateRoot(bsonReader); | ||
| if (BsonValueSerializer.Instance.Deserialize(context) is BsonString bsonString) | ||
| { | ||
| discriminatorValue = bsonString.Value; | ||
| } | ||
| } | ||
| bsonReader.ReturnToBookmark(bookmark); | ||
| } | ||
|
|
||
| return discriminatorValue; | ||
| } | ||
| } | ||
|
|
||
| public sealed class ClassFixture : MongoDatabaseFixture | ||
| { | ||
| public IMongoCollection<Animal> AnimalCollection { get; private set; } | ||
| public IMongoCollection<Person> PersonCollection { get; private set; } | ||
|
|
||
| protected override void InitializeFixture() | ||
| { | ||
| AnimalCollection = CreateCollection<Animal>("animalCollection"); | ||
| AnimalCollection.InsertMany([ | ||
| new Cat { Id = 1 }, | ||
| new Dog { Id = 2 }]); | ||
|
|
||
| PersonCollection = CreateCollection<Person>("personCollection"); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These new tests should probably be integration tests like the tests for scalar discriminator conventions.
See CSharp5231Tests.cs for an example of a file that uses two collections for integration tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.