Skip to content

Commit d1cf901

Browse files
authored
CSHARP-4040: Use AppContext switch to disable CSHARP-4040 validation (#1825)
1 parent 45d5593 commit d1cf901

File tree

2 files changed

+189
-0
lines changed

2 files changed

+189
-0
lines changed

src/MongoDB.Bson/Serialization/BsonClassMap.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,11 @@ internal IDiscriminatorConvention GetDiscriminatorConvention()
13371337

13381338
void EnsureNoMemberMapConflicts(string elementName)
13391339
{
1340+
if (AppContext.TryGetSwitch("Switch.MongoDB.Driver.DisableDiscriminatorFieldConflictCheck", out bool disableConflictCheck) && disableConflictCheck)
1341+
{
1342+
return;
1343+
}
1344+
13401345
var conflictingMemberMap = _allMemberMaps.FirstOrDefault(memberMap => memberMap.ElementName == elementName);
13411346

13421347
if (conflictingMemberMap != null)
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Collections.Generic;
18+
using System.Linq;
19+
using MongoDB.Driver.TestHelpers;
20+
using FluentAssertions;
21+
using MongoDB.Bson.Serialization;
22+
using MongoDB.Bson.Serialization.Conventions;
23+
using MongoDB.Bson.Serialization.Serializers;
24+
using MongoDB.Driver.Linq;
25+
using Xunit;
26+
27+
namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira;
28+
29+
public class CSharp4040SwitchTests : LinqIntegrationTest<CSharp4040SwitchTests.ClassFixture>
30+
{
31+
public CSharp4040SwitchTests(ClassFixture fixture)
32+
: base(fixture)
33+
{
34+
}
35+
36+
[Fact]
37+
public void Documents_should_serialize_as_expected()
38+
{
39+
var collection = Fixture.Collection;
40+
41+
var seralizedDocuments = collection.AsQueryable().As(BsonDocumentSerializer.Instance).ToList();
42+
43+
seralizedDocuments.Count.Should().Be(2);
44+
seralizedDocuments[0].Should().Be("{ _id : 1, TypeNames : ['C', 'D'] }");
45+
seralizedDocuments[1].Should().Be("{ _id : 2, TypeNames : ['C', 'D', 'E'] }");
46+
}
47+
48+
[Fact]
49+
public void OfType_C_should_work()
50+
{
51+
var collection = Fixture.Collection;
52+
53+
var queryable = collection.AsQueryable()
54+
.OfType<C>();
55+
56+
var stages = Translate(collection, queryable);
57+
AssertStages(stages, []);
58+
59+
var results = queryable.ToList();
60+
results.Select(x => x.Id).Should().Equal(1, 2);
61+
}
62+
63+
[Fact]
64+
public void OfType_D_should_work()
65+
{
66+
var collection = Fixture.Collection;
67+
68+
var queryable = collection.AsQueryable()
69+
.OfType<D>();
70+
71+
var stages = Translate(collection, queryable);
72+
AssertStages(stages, "{ $match : { TypeNames : 'D' } }");
73+
74+
var results = queryable.ToList();
75+
results.Select(x => x.Id).Should().Equal(1, 2);
76+
}
77+
78+
[Fact]
79+
public void OfType_E_should_work()
80+
{
81+
var collection = Fixture.Collection;
82+
83+
var queryable = collection.AsQueryable()
84+
.OfType<E>();
85+
86+
var stages = Translate(collection, queryable);
87+
AssertStages(stages, "{ $match : { TypeNames : 'E' } }");
88+
89+
var results = queryable.ToList();
90+
results.Select(x => x.Id).Should().Equal(2);
91+
}
92+
93+
[Fact]
94+
public void Where_TypeNames_Contains_C_should_work()
95+
{
96+
var collection = Fixture.Collection;
97+
98+
var queryable = collection.AsQueryable()
99+
.Where(x => x.TypeNames.Contains("C"));
100+
101+
var stages = Translate(collection, queryable);
102+
AssertStages(stages, "{ $match : { TypeNames : 'C' } }");
103+
104+
var results = queryable.ToList();
105+
results.Select(x => x.Id).Should().Equal(1, 2);
106+
}
107+
108+
[Fact]
109+
public void Where_TypeNames_Contains_D_should_work()
110+
{
111+
var collection = Fixture.Collection;
112+
113+
var queryable = collection.AsQueryable()
114+
.Where(x => x.TypeNames.Contains("D"));
115+
116+
var stages = Translate(collection, queryable);
117+
AssertStages(stages, "{ $match : { TypeNames : 'D' } }");
118+
119+
var results = queryable.ToList();
120+
results.Select(x => x.Id).Should().Equal(1, 2);
121+
}
122+
123+
[Fact]
124+
public void Where_TypeNames_Contains_E_should_work()
125+
{
126+
var collection = Fixture.Collection;
127+
128+
var queryable = collection.AsQueryable()
129+
.Where(x => x.TypeNames.Contains("E"));
130+
131+
var stages = Translate(collection, queryable);
132+
AssertStages(stages, "{ $match : { TypeNames : 'E' } }");
133+
134+
var results = queryable.ToList();
135+
results.Select(x => x.Id).Should().Equal(2);
136+
}
137+
138+
public abstract class C
139+
{
140+
public int Id { get; set; }
141+
public virtual IReadOnlyList<string> TypeNames => ["C"];
142+
}
143+
144+
public class D : C
145+
{
146+
public override IReadOnlyList<string> TypeNames => ["C", "D"];
147+
}
148+
149+
public class E : D
150+
{
151+
public override IReadOnlyList<string> TypeNames => ["C", "D", "E"];
152+
}
153+
154+
public sealed class ClassFixture : MongoCollectionFixture<C>
155+
{
156+
public ClassFixture()
157+
{
158+
AppContext.SetSwitch("Switch.MongoDB.Driver.DisableDiscriminatorFieldConflictCheck", true);
159+
160+
var discriminatorConvention = new HierarchicalDiscriminatorConvention("TypeNames");
161+
BsonSerializer.RegisterDiscriminatorConvention(typeof(C), discriminatorConvention);
162+
163+
BsonClassMap.RegisterClassMap<C>(cm =>
164+
{
165+
cm.AutoMap();
166+
cm.SetIsRootClass(true);
167+
cm.SetDiscriminatorIsRequired(true);
168+
cm.MapMember(x => x.TypeNames).SetShouldSerializeMethod(_ => false);
169+
});
170+
}
171+
172+
protected override IEnumerable<C> InitialData =>
173+
[
174+
new D { Id = 1 },
175+
new E { Id = 2 }
176+
];
177+
178+
public override void Dispose()
179+
{
180+
base.Dispose();
181+
AppContext.SetSwitch("Switch.MongoDB.Driver.DisableDiscriminatorFieldConflictCheck", false);
182+
}
183+
}
184+
}

0 commit comments

Comments
 (0)