Skip to content

Commit 2246e80

Browse files
committed
Fix collection typed property which has non-public setter are not handled correctly. Issue #62.
This commit changes filter in non-annotated classes to allow properties which have non-public setters. In addition, same issue exists in code generation even if classes in annotated, so unnecessary reflection based accesses are emitted. This commit also includes additional unit testing cases.
1 parent 5da5e3e commit 2246e80

17 files changed

+68
-19
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,4 @@ Relase 0.5.9 - 2015/2/01
218218

219219
BUG FIXES
220220
* Fix NullReferenceException is thrown for annotated non-public members serialization/deserialization in WinRT.
221+
* Fix collection typed property which has non-public setter are not handled correctly. Issue #62.

src/MsgPack/ReflectionAbstractions.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,28 +203,26 @@ public static IEnumerable<Type> GetInterfaces( this Type source )
203203

204204
public static MethodInfo GetGetMethod( this PropertyInfo source )
205205
{
206-
return source.GetMethod;
206+
return GetGetMethod( source, false );
207207
}
208208

209209
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "containsNonPublic", Justification = "For API compabitility" )]
210210
public static MethodInfo GetGetMethod( this PropertyInfo source, bool containsNonPublic )
211211
{
212-
Contract.Assert( containsNonPublic ); // false is not supported now.
213-
214-
return source.GetMethod;
212+
var getter = source.GetMethod;
213+
return ( containsNonPublic || getter.IsPublic ) ? getter : null;
215214
}
216215

217216
public static MethodInfo GetSetMethod( this PropertyInfo source )
218217
{
219-
return source.SetMethod;
218+
return GetSetMethod( source, false );
220219
}
221220

222221
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "containsNonPublic", Justification = "For API compabitility" )]
223222
public static MethodInfo GetSetMethod( this PropertyInfo source, bool containsNonPublic )
224223
{
225-
Contract.Assert( containsNonPublic ); // false is not supported now.
226-
227-
return source.SetMethod;
224+
var setter = source.SetMethod;
225+
return ( containsNonPublic || setter.IsPublic ) ? setter : null;
228226
}
229227

230228
public static IEnumerable<Type> FindInterfaces( this Type source, Func<Type, object, bool> filter, object filterCriteria )

src/MsgPack/Serialization/AbstractSerializers/SerializerBuilder`3.CommonConstructs.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// MessagePack for CLI
44
//
5-
// Copyright (C) 2010-2013 FUJIWARA, Yusuke
5+
// Copyright (C) 2010-2015 FUJIWARA, Yusuke
66
//
77
// Licensed under the Apache License, Version 2.0 (the "License");
88
// you may not use this file except in compliance with the License.
@@ -315,15 +315,15 @@ private TConstruct EmitGetMemberValueExpression( TContext context, TConstruct in
315315
FieldInfo asField;
316316
if ( ( asField = member as FieldInfo ) != null )
317317
{
318-
return this.EmitGetField( context, instance, asField, !asField.GetIsPublic() );
318+
return this.EmitGetField( context, instance, asField, !asField.GetHasPublicGetter() );
319319
}
320320
else
321321
{
322322
var asProperty = member as PropertyInfo;
323323
#if DEBUG
324324
Contract.Assert( asProperty != null, member.GetType().FullName );
325325
#endif
326-
return this.EmitGetProperty( context, instance, asProperty, !asProperty.GetIsPublic() );
326+
return this.EmitGetProperty( context, instance, asProperty, !asProperty.GetHasPublicGetter() );
327327
}
328328
}
329329

@@ -416,7 +416,7 @@ private TConstruct EmitSetMemberValueStatement( TContext context, TConstruct ins
416416
return this.EmitSetField( context, instance, asField, value, false );
417417
}
418418

419-
getCollection = this.EmitGetField( context, instance, asField, !asField.GetIsPublic() );
419+
getCollection = this.EmitGetField( context, instance, asField, !asField.GetHasPublicGetter() );
420420
traits = asField.FieldType.GetCollectionTraits();
421421
}
422422
else
@@ -431,7 +431,7 @@ private TConstruct EmitSetMemberValueStatement( TContext context, TConstruct ins
431431
return this.EmitSetProprety( context, instance, asProperty, value, false );
432432
}
433433

434-
getCollection = this.EmitGetProperty( context, instance, asProperty, !asProperty.GetIsPublic() );
434+
getCollection = this.EmitGetProperty( context, instance, asProperty, !asProperty.GetHasPublicGetter() );
435435
traits = asProperty.PropertyType.GetCollectionTraits();
436436
}
437437

src/MsgPack/Serialization/ReflectionExtensions.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// MessagePack for CLI
44
//
5-
// Copyright (C) 2010-2014 FUJIWARA, Yusuke
5+
// Copyright (C) 2010-2015 FUJIWARA, Yusuke
66
//
77
// Licensed under the Apache License, Version 2.0 (the "License");
88
// you may not use this file except in compliance with the License.
@@ -484,6 +484,28 @@ public static IEnumerable<Type> FindInterfaces( this Type source, Func<Type, obj
484484
}
485485
#endif
486486

487+
public static bool GetHasPublicGetter( this MemberInfo source )
488+
{
489+
PropertyInfo asProperty;
490+
FieldInfo asField;
491+
if ( ( asProperty = source as PropertyInfo ) != null )
492+
{
493+
#if !NETFX_CORE
494+
return asProperty.GetGetMethod() != null;
495+
#else
496+
return ( asProperty.GetMethod != null && asProperty.GetMethod.IsPublic );
497+
#endif
498+
}
499+
else if ( ( asField = source as FieldInfo ) != null )
500+
{
501+
return asField.IsPublic;
502+
}
503+
else
504+
{
505+
throw new NotSupportedException( source.GetType() + " is not supported." );
506+
}
507+
}
508+
487509
public static bool GetIsPublic( this MemberInfo source )
488510
{
489511
PropertyInfo asProperty;

src/MsgPack/Serialization/SerializationTarget.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// MessagePack for CLI
44
//
5-
// Copyright (C) 2014 FUJIWARA, Yusuke
5+
// Copyright (C) 2014-2015 FUJIWARA, Yusuke
66
//
77
// Licensed under the Apache License, Version 2.0 (the "License");
88
// you may not use this file except in compliance with the License.
@@ -188,10 +188,10 @@ internal static IEnumerable<SerializingMember> GetTargetMembers( Type type )
188188
);
189189
}
190190
#if SILVERLIGHT || NETFX_CORE
191-
return members.Where( member => member.GetIsPublic() ).Select( member => new SerializingMember( member, new DataMemberContract( member ) ) );
191+
return members.Where( member => member.GetHasPublicGetter() ).Select( member => new SerializingMember( member, new DataMemberContract( member ) ) );
192192
#else
193193
return
194-
members.Where( item => item.GetIsPublic() && !Attribute.IsDefined( item, typeof( NonSerializedAttribute ) ) )
194+
members.Where( item => item.GetHasPublicGetter() && !Attribute.IsDefined( item, typeof( NonSerializedAttribute ) ) )
195195
.Select( member => new SerializingMember( member, new DataMemberContract( member ) ) );
196196
#endif
197197
}

test/MsgPack.UnitTest.CodeDom/Serialization/ArrayCodeDomBasedAutoMessagePackSerializerTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ private void TestComplexObjectCore( SerializationContext context )
233233
{
234234
var target = new ComplexType() { Source = new Uri( "http://www.exambple.com" ), TimeStamp = DateTime.Now, Data = new byte[] { 0x1, 0x2, 0x3, 0x4 } };
235235
target.History.Add( DateTime.Now.Subtract( TimeSpan.FromDays( 1 ) ), "Create New" );
236+
target.Points.Add( 123 );
236237
TestCoreWithVerify( target, context );
237238
}
238239

test/MsgPack.UnitTest.CodeDom/Serialization/MapCodeDomBasedAutoMessagePackSerializerTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ private void TestComplexObjectCore( SerializationContext context )
233233
{
234234
var target = new ComplexType() { Source = new Uri( "http://www.exambple.com" ), TimeStamp = DateTime.Now, Data = new byte[] { 0x1, 0x2, 0x3, 0x4 } };
235235
target.History.Add( DateTime.Now.Subtract( TimeSpan.FromDays( 1 ) ), "Create New" );
236+
target.Points.Add( 123 );
236237
TestCoreWithVerify( target, context );
237238
}
238239

test/MsgPack.UnitTest/Serialization/ArrayContextBasedAutoMessagePackSerializerTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ private void TestComplexObjectCore( SerializationContext context )
233233
{
234234
var target = new ComplexType() { Source = new Uri( "http://www.exambple.com" ), TimeStamp = DateTime.Now, Data = new byte[] { 0x1, 0x2, 0x3, 0x4 } };
235235
target.History.Add( DateTime.Now.Subtract( TimeSpan.FromDays( 1 ) ), "Create New" );
236+
target.Points.Add( 123 );
236237
TestCoreWithVerify( target, context );
237238
}
238239

test/MsgPack.UnitTest/Serialization/ArrayExpressionBasedAutoMessagePackSerializerTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ private void TestComplexObjectCore( SerializationContext context )
233233
{
234234
var target = new ComplexType() { Source = new Uri( "http://www.exambple.com" ), TimeStamp = DateTime.Now, Data = new byte[] { 0x1, 0x2, 0x3, 0x4 } };
235235
target.History.Add( DateTime.Now.Subtract( TimeSpan.FromDays( 1 ) ), "Create New" );
236+
target.Points.Add( 123 );
236237
TestCoreWithVerify( target, context );
237238
}
238239

test/MsgPack.UnitTest/Serialization/ArrayFieldBasedAutoMessagePackSerializerTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ private void TestComplexObjectCore( SerializationContext context )
233233
{
234234
var target = new ComplexType() { Source = new Uri( "http://www.exambple.com" ), TimeStamp = DateTime.Now, Data = new byte[] { 0x1, 0x2, 0x3, 0x4 } };
235235
target.History.Add( DateTime.Now.Subtract( TimeSpan.FromDays( 1 ) ), "Create New" );
236+
target.Points.Add( 123 );
236237
TestCoreWithVerify( target, context );
237238
}
238239

0 commit comments

Comments
 (0)