Skip to content

Commit e0c14c8

Browse files
committed
CSHARP-4534: Allow anonymous types in ObjectSerializer.DefaultAllowedTypes.
1 parent 4f9b7ad commit e0c14c8

File tree

4 files changed

+50
-17
lines changed

4 files changed

+50
-17
lines changed

src/MongoDB.Bson/Serialization/BsonClassMap.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public BsonClassMap(Type classType)
7171
_classType = classType;
7272
_creatorMaps = new List<BsonCreatorMap>();
7373
_conventionPack = ConventionRegistry.Lookup(classType);
74-
_isAnonymous = IsAnonymousType(classType);
74+
_isAnonymous = classType.IsAnonymousType();
7575
_allMemberMaps = new List<BsonMemberMap>();
7676
_allMemberMapsReadonly = _allMemberMaps.AsReadOnly();
7777
_declaredMemberMaps = new List<BsonMemberMap>();
@@ -1370,16 +1370,6 @@ private Func<object, bool> GetShouldSerializeMethod(MemberInfo memberInfo)
13701370
}
13711371
}
13721372

1373-
private bool IsAnonymousType(Type type)
1374-
{
1375-
// don't test for too many things in case implementation details change in the future
1376-
var typeInfo = type.GetTypeInfo();
1377-
return
1378-
typeInfo.GetCustomAttributes<CompilerGeneratedAttribute>(false).Any() &&
1379-
typeInfo.IsGenericType &&
1380-
type.Name.Contains("Anon"); // don't check for more than "Anon" so it works in mono also
1381-
}
1382-
13831373
private void ThrowFrozenException()
13841374
{
13851375
var message = string.Format("Class map for {0} has been frozen and no further changes are allowed.", _classType.FullName);

src/MongoDB.Bson/Serialization/Serializers/ObjectSerializer.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -567,20 +567,16 @@ private static class DefaultFrameworkAllowedTypes
567567

568568
private static bool AllowedTypesImplementation(Type type)
569569
{
570-
if (typeof(BsonValue).IsAssignableFrom(type))
571-
{
572-
return true;
573-
}
574-
575570
return type.IsConstructedGenericType ? IsAllowedGenericType(type) : IsAllowedType(type);
576571

577572
static bool IsAllowedType(Type type) =>
573+
typeof(BsonValue).IsAssignableFrom(type) ||
578574
__allowedNonGenericTypesSet.Contains(type) ||
579575
type.IsArray && AllowedTypesImplementation(type.GetElementType()) ||
580576
type.IsEnum;
581577

582578
static bool IsAllowedGenericType(Type type) =>
583-
__allowedGenericTypesSet.Contains(type.GetGenericTypeDefinition()) &&
579+
(__allowedGenericTypesSet.Contains(type.GetGenericTypeDefinition()) || type.IsAnonymousType()) &&
584580
type.GetGenericArguments().All(__allowedTypes);
585581
}
586582
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.Linq;
18+
using System.Runtime.CompilerServices;
19+
20+
namespace MongoDB.Bson.Serialization
21+
{
22+
internal static class TypeExtensions
23+
{
24+
public static bool IsAnonymousType(this Type type)
25+
{
26+
// don't test for too many things in case implementation details change in the future
27+
return
28+
type.GetCustomAttributes(false).Any(x => x is CompilerGeneratedAttribute) &&
29+
type.IsGenericType &&
30+
type.Name.Contains("Anon"); // don't check for more than "Anon" so it works in mono also
31+
}
32+
}
33+
}

tests/MongoDB.Bson.Tests/Serialization/Serializers/ObjectSerializerTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,4 +828,18 @@ public void Serialize_guid_should_throw_when_guid_representation_is_unspecified_
828828
#pragma warning restore 618
829829
}
830830
}
831+
832+
public class DefaultAllowedTypesTests
833+
{
834+
[Fact]
835+
public void DefaultAllowedTypes_with_anonymous_type_should_return_true()
836+
{
837+
var anonymousInstance = new { X = 1 };
838+
var anonymousType = anonymousInstance.GetType();
839+
840+
var result = ObjectSerializer.DefaultAllowedTypes(anonymousType);
841+
842+
result.Should().BeTrue();
843+
}
844+
}
831845
}

0 commit comments

Comments
 (0)