Skip to content

Commit 58afad7

Browse files
rbugginsviavincentkam
authored andcommitted
CSHARP-2405: ImmutableTypeClassMapConvention should not consider static properties
1 parent e856650 commit 58afad7

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Please see our [guidelines](CONTRIBUTING.md) for contributing to the driver.
9191
* Bar Arnon https://github.com/I3arnon
9292
* Wan Bachtiar https://github.com/sindbach
9393
* Mark Benvenuto https://github.com/markbenvenuto
94+
* Ross Buggins https://github.com/rbugginsvia
9495
* Ethan Celletti https://github.com/Gekctek
9596
* Bit Diffusion Limited [email protected]
9697
* Nima Boscarino https://github.com/NimaBoscarino

src/MongoDB.Bson/Serialization/Conventions/ImmutableTypeClassMapConvention.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void Apply(BsonClassMap classMap)
3939
return;
4040
}
4141

42-
var properties = typeInfo.GetProperties();
42+
var properties = typeInfo.GetProperties(BindingFlags.Instance | BindingFlags.Public );
4343
if (properties.Any(p => p.CanWrite))
4444
{
4545
return; // a type that has any writable properties is not immutable

tests/MongoDB.Bson.Tests/Serialization/Conventions/ImmutableTypeClassMapConventionTests.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,33 @@ public TestClassC(string a, string b)
6060
}
6161
}
6262

63+
private class TestClassD
64+
{
65+
public TestClassD(int a) { A = a; }
66+
67+
public static TestClassD Default => new TestClassD(0);
68+
69+
public int A { get; }
70+
}
71+
72+
private class TestClassE
73+
{
74+
public TestClassE(int a) { A = a; }
75+
76+
public static TestClassE Default => new TestClassE(0);
77+
78+
public int A { get; set; }
79+
}
80+
81+
private class TestClassF
82+
{
83+
public TestClassF(int a) { A = a; }
84+
85+
public static TestClassF Default { get; set; } = new TestClassF(0);
86+
87+
public int A { get; }
88+
}
89+
6390
[Fact]
6491
public void Anonymous_class_should_map_correctly()
6592
{
@@ -125,5 +152,35 @@ public void TestNoDefaultConstructorClassMapConventionWithTestClassC()
125152
Assert.True(classMap.HasCreatorMaps);
126153
Assert.Equal(1, classMap.CreatorMaps.Count());
127154
}
155+
156+
[Fact]
157+
public void TestNoDefaultConstructorClassMapConventionWithTestClassD()
158+
{
159+
var convention = new ImmutableTypeClassMapConvention();
160+
var classMap = new BsonClassMap<TestClassD>();
161+
convention.Apply(classMap);
162+
Assert.True(classMap.HasCreatorMaps);
163+
Assert.Equal(1, classMap.CreatorMaps.Count());
164+
}
165+
166+
[Fact]
167+
public void TestNoDefaultConstructorClassMapConventionWithTestClassE()
168+
{
169+
var convention = new ImmutableTypeClassMapConvention();
170+
var classMap = new BsonClassMap<TestClassE>();
171+
convention.Apply(classMap);
172+
Assert.False(classMap.HasCreatorMaps);
173+
Assert.Equal(0, classMap.CreatorMaps.Count());
174+
}
175+
176+
[Fact]
177+
public void TestNoDefaultConstructorClassMapConventionWithTestClassF()
178+
{
179+
var convention = new ImmutableTypeClassMapConvention();
180+
var classMap = new BsonClassMap<TestClassF>();
181+
convention.Apply(classMap);
182+
Assert.True(classMap.HasCreatorMaps);
183+
Assert.Equal(1, classMap.CreatorMaps.Count());
184+
}
128185
}
129186
}

0 commit comments

Comments
 (0)