Skip to content

Commit c85bf73

Browse files
committed
Fix [MessagePackRuntimeType] fails when the type is declared in global namespace. Issue #137.
This commit fixes TypeInfoEncoder to enable handling global namespace which is represented as null Namespace property. This commit also adds unit tests for it.
1 parent c0ce6b0 commit c85bf73

17 files changed

+324
-12
lines changed

CHANGES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,3 +375,7 @@ Release 0.6.5.1 - 2015/10/25
375375
BUG FIXES
376376
* mpu.exe -l option does not work in non-Windows environment. Issue #125.
377377

378+
Release 0.6.6 - 2015/12/20
379+
380+
BUG FIXES
381+
* Fix [MessagePackRuntimeType] fails when the type is declared in global namespace. Issue #137.

src/MsgPack/Serialization/Polymorphic/TypeInfoEncoder.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@
2323
#endif
2424

2525
using System;
26-
#if !UNITY
27-
#if XAMIOS || XAMDROID
28-
using Contract = MsgPack.MPContract;
29-
#else
30-
using System.Diagnostics.Contracts;
31-
#endif // XAMIOS || XAMDROID
32-
#endif // !UNITY
3326
using System.Globalization;
3427
using System.Reflection;
3528
using System.Runtime.Serialization;
@@ -58,18 +51,14 @@ public static void Encode( Packer packer, Type type )
5851
new AssemblyName( type.GetAssembly().FullName );
5952
#endif // !SILVERLIGHT
6053

61-
#if DEBUG && !UNITY
62-
Contract.Assert( type.Namespace != null, "type.Namespace != null" );
63-
#endif // DEBUG && !UNITY
64-
6554
packer.PackArrayHeader( 2 );
6655
packer.PackArrayHeader( 6 );
6756

6857
packer.Pack( ( byte )TypeInfoEncoding.RawCompressed );
6958

7059
// Omit namespace prefix when it equals to declaring assembly simple name.
7160
var compressedTypeName =
72-
type.Namespace.StartsWith( assemblyName.Name, StringComparison.Ordinal )
61+
( type.Namespace != null && type.Namespace.StartsWith( assemblyName.Name, StringComparison.Ordinal ) )
7362
? Elipsis + type.FullName.Substring( assemblyName.Name.Length + 1 )
7463
: type.FullName;
7564
var version = new byte[ 16 ];

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11741,6 +11741,28 @@ public void TestAttribute_KnownAndRuntimeTupleItem_Fail()
1174111741
Assert.Throws<SerializationException>( ()=> context.GetSerializer<KnownAndRuntimeTupleItem>() );
1174211742
}
1174311743
#endif // !NETFX_35 && !UNITY
11744+
// Issue 137
11745+
[Test]
11746+
[Category( "PolymorphicSerialization" )]
11747+
public void TestGlobalNamespace()
11748+
{
11749+
var context = NewSerializationContext( PackerCompatibilityOptions.None );
11750+
var target = new HasGlobalNamespaceType { GlobalType = new TypeInGlobalNamespace { Value = "ABC" } };
11751+
var serializer = context.GetSerializer<HasGlobalNamespaceType>();
11752+
11753+
using ( var buffer = new MemoryStream() )
11754+
{
11755+
serializer.Pack( buffer, target );
11756+
buffer.Position = 0;
11757+
var result = serializer.Unpack( buffer );
11758+
11759+
Assert.That( result, Is.Not.Null );
11760+
Assert.That( result, Is.Not.SameAs( target ) );
11761+
Assert.That( result.GlobalType, Is.Not.Null );
11762+
Assert.That( result.GlobalType, Is.Not.SameAs( target.GlobalType ) );
11763+
Assert.That( result.GlobalType.Value, Is.EqualTo( target.GlobalType.Value ) );
11764+
}
11765+
}
1174411766

1174511767
#endregion -- Polymorphism --
1174611768
[Test]

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11747,6 +11747,28 @@ public void TestAttribute_KnownAndRuntimeTupleItem_Fail()
1174711747
Assert.Throws<SerializationException>( ()=> context.GetSerializer<KnownAndRuntimeTupleItem>() );
1174811748
}
1174911749
#endif // !NETFX_35 && !UNITY
11750+
// Issue 137
11751+
[Test]
11752+
[Category( "PolymorphicSerialization" )]
11753+
public void TestGlobalNamespace()
11754+
{
11755+
var context = NewSerializationContext( PackerCompatibilityOptions.None );
11756+
var target = new HasGlobalNamespaceType { GlobalType = new TypeInGlobalNamespace { Value = "ABC" } };
11757+
var serializer = context.GetSerializer<HasGlobalNamespaceType>();
11758+
11759+
using ( var buffer = new MemoryStream() )
11760+
{
11761+
serializer.Pack( buffer, target );
11762+
buffer.Position = 0;
11763+
var result = serializer.Unpack( buffer );
11764+
11765+
Assert.That( result, Is.Not.Null );
11766+
Assert.That( result, Is.Not.SameAs( target ) );
11767+
Assert.That( result.GlobalType, Is.Not.Null );
11768+
Assert.That( result.GlobalType, Is.Not.SameAs( target.GlobalType ) );
11769+
Assert.That( result.GlobalType.Value, Is.EqualTo( target.GlobalType.Value ) );
11770+
}
11771+
}
1175011772

1175111773
#endregion -- Polymorphism --
1175211774
[Test]

test/MsgPack.UnitTest/Serialization/ArrayContextBasedAutoMessagePackSerializerTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11741,6 +11741,28 @@ public void TestAttribute_KnownAndRuntimeTupleItem_Fail()
1174111741
Assert.Throws<SerializationException>( ()=> context.GetSerializer<KnownAndRuntimeTupleItem>() );
1174211742
}
1174311743
#endif // !NETFX_35 && !UNITY
11744+
// Issue 137
11745+
[Test]
11746+
[Category( "PolymorphicSerialization" )]
11747+
public void TestGlobalNamespace()
11748+
{
11749+
var context = NewSerializationContext( PackerCompatibilityOptions.None );
11750+
var target = new HasGlobalNamespaceType { GlobalType = new TypeInGlobalNamespace { Value = "ABC" } };
11751+
var serializer = context.GetSerializer<HasGlobalNamespaceType>();
11752+
11753+
using ( var buffer = new MemoryStream() )
11754+
{
11755+
serializer.Pack( buffer, target );
11756+
buffer.Position = 0;
11757+
var result = serializer.Unpack( buffer );
11758+
11759+
Assert.That( result, Is.Not.Null );
11760+
Assert.That( result, Is.Not.SameAs( target ) );
11761+
Assert.That( result.GlobalType, Is.Not.Null );
11762+
Assert.That( result.GlobalType, Is.Not.SameAs( target.GlobalType ) );
11763+
Assert.That( result.GlobalType.Value, Is.EqualTo( target.GlobalType.Value ) );
11764+
}
11765+
}
1174411766

1174511767
#endregion -- Polymorphism --
1174611768
[Test]

test/MsgPack.UnitTest/Serialization/ArrayExpressionBasedAutoMessagePackSerializerTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11741,6 +11741,28 @@ public void TestAttribute_KnownAndRuntimeTupleItem_Fail()
1174111741
Assert.Throws<SerializationException>( ()=> context.GetSerializer<KnownAndRuntimeTupleItem>() );
1174211742
}
1174311743
#endif // !NETFX_35 && !UNITY
11744+
// Issue 137
11745+
[Test]
11746+
[Category( "PolymorphicSerialization" )]
11747+
public void TestGlobalNamespace()
11748+
{
11749+
var context = NewSerializationContext( PackerCompatibilityOptions.None );
11750+
var target = new HasGlobalNamespaceType { GlobalType = new TypeInGlobalNamespace { Value = "ABC" } };
11751+
var serializer = context.GetSerializer<HasGlobalNamespaceType>();
11752+
11753+
using ( var buffer = new MemoryStream() )
11754+
{
11755+
serializer.Pack( buffer, target );
11756+
buffer.Position = 0;
11757+
var result = serializer.Unpack( buffer );
11758+
11759+
Assert.That( result, Is.Not.Null );
11760+
Assert.That( result, Is.Not.SameAs( target ) );
11761+
Assert.That( result.GlobalType, Is.Not.Null );
11762+
Assert.That( result.GlobalType, Is.Not.SameAs( target.GlobalType ) );
11763+
Assert.That( result.GlobalType.Value, Is.EqualTo( target.GlobalType.Value ) );
11764+
}
11765+
}
1174411766

1174511767
#endregion -- Polymorphism --
1174611768
[Test]

test/MsgPack.UnitTest/Serialization/ArrayFieldBasedAutoMessagePackSerializerTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11741,6 +11741,28 @@ public void TestAttribute_KnownAndRuntimeTupleItem_Fail()
1174111741
Assert.Throws<SerializationException>( ()=> context.GetSerializer<KnownAndRuntimeTupleItem>() );
1174211742
}
1174311743
#endif // !NETFX_35 && !UNITY
11744+
// Issue 137
11745+
[Test]
11746+
[Category( "PolymorphicSerialization" )]
11747+
public void TestGlobalNamespace()
11748+
{
11749+
var context = NewSerializationContext( PackerCompatibilityOptions.None );
11750+
var target = new HasGlobalNamespaceType { GlobalType = new TypeInGlobalNamespace { Value = "ABC" } };
11751+
var serializer = context.GetSerializer<HasGlobalNamespaceType>();
11752+
11753+
using ( var buffer = new MemoryStream() )
11754+
{
11755+
serializer.Pack( buffer, target );
11756+
buffer.Position = 0;
11757+
var result = serializer.Unpack( buffer );
11758+
11759+
Assert.That( result, Is.Not.Null );
11760+
Assert.That( result, Is.Not.SameAs( target ) );
11761+
Assert.That( result.GlobalType, Is.Not.Null );
11762+
Assert.That( result.GlobalType, Is.Not.SameAs( target.GlobalType ) );
11763+
Assert.That( result.GlobalType.Value, Is.EqualTo( target.GlobalType.Value ) );
11764+
}
11765+
}
1174411766

1174511767
#endregion -- Polymorphism --
1174611768
[Test]

test/MsgPack.UnitTest/Serialization/ArrayGenerationBasedAutoMessagePackSerializerTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10547,6 +10547,28 @@ public void TestAttribute_KnownAndRuntimeTupleItem_Fail()
1054710547
Assert.Throws<SerializationException>( ()=> context.GetSerializer<KnownAndRuntimeTupleItem>() );
1054810548
}
1054910549
#endif // !NETFX_35 && !UNITY
10550+
// Issue 137
10551+
[Test]
10552+
[Category( "PolymorphicSerialization" )]
10553+
public void TestGlobalNamespace()
10554+
{
10555+
var context = NewSerializationContext( PackerCompatibilityOptions.None );
10556+
var target = new HasGlobalNamespaceType { GlobalType = new TypeInGlobalNamespace { Value = "ABC" } };
10557+
var serializer = context.GetSerializer<HasGlobalNamespaceType>();
10558+
10559+
using ( var buffer = new MemoryStream() )
10560+
{
10561+
serializer.Pack( buffer, target );
10562+
buffer.Position = 0;
10563+
var result = serializer.Unpack( buffer );
10564+
10565+
Assert.That( result, Is.Not.Null );
10566+
Assert.That( result, Is.Not.SameAs( target ) );
10567+
Assert.That( result.GlobalType, Is.Not.Null );
10568+
Assert.That( result.GlobalType, Is.Not.SameAs( target.GlobalType ) );
10569+
Assert.That( result.GlobalType.Value, Is.EqualTo( target.GlobalType.Value ) );
10570+
}
10571+
}
1055010572

1055110573
#endregion -- Polymorphism --
1055210574
[Test]

test/MsgPack.UnitTest/Serialization/ArrayReflectionBasedAutoMessagePackSerializerTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11642,6 +11642,28 @@ public void TestAttribute_KnownAndRuntimeTupleItem_Fail()
1164211642
Assert.Throws<SerializationException>( ()=> context.GetSerializer<KnownAndRuntimeTupleItem>() );
1164311643
}
1164411644
#endif // !NETFX_35 && !UNITY
11645+
// Issue 137
11646+
[Test]
11647+
[Category( "PolymorphicSerialization" )]
11648+
public void TestGlobalNamespace()
11649+
{
11650+
var context = NewSerializationContext( PackerCompatibilityOptions.None );
11651+
var target = new HasGlobalNamespaceType { GlobalType = new TypeInGlobalNamespace { Value = "ABC" } };
11652+
var serializer = context.GetSerializer<HasGlobalNamespaceType>();
11653+
11654+
using ( var buffer = new MemoryStream() )
11655+
{
11656+
serializer.Pack( buffer, target );
11657+
buffer.Position = 0;
11658+
var result = serializer.Unpack( buffer );
11659+
11660+
Assert.That( result, Is.Not.Null );
11661+
Assert.That( result, Is.Not.SameAs( target ) );
11662+
Assert.That( result.GlobalType, Is.Not.Null );
11663+
Assert.That( result.GlobalType, Is.Not.SameAs( target.GlobalType ) );
11664+
Assert.That( result.GlobalType.Value, Is.EqualTo( target.GlobalType.Value ) );
11665+
}
11666+
}
1164511667

1164611668
#endregion -- Polymorphism --
1164711669
[Test]

test/MsgPack.UnitTest/Serialization/AutoMessagePackSerializerTest.Polymorphism.ttinclude

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,33 @@ private IEnumerable<TestTargetType> GeneratePolymorphismTestPartsCore( bool gene
482482

483483
if ( generateTypes || generateMethods )
484484
{
485+
if ( generateMethods )
486+
{
487+
#>
488+
// Issue 137
489+
[Test]
490+
[Category( "PolymorphicSerialization" )]
491+
public void TestGlobalNamespace()
492+
{
493+
var context = NewSerializationContext( PackerCompatibilityOptions.None );
494+
var target = new HasGlobalNamespaceType { GlobalType = new TypeInGlobalNamespace { Value = "ABC" } };
495+
var serializer = context.GetSerializer<HasGlobalNamespaceType>();
496+
497+
using ( var buffer = new MemoryStream() )
498+
{
499+
serializer.Pack( buffer, target );
500+
buffer.Position = 0;
501+
var result = serializer.Unpack( buffer );
502+
503+
Assert.That( result, Is.Not.Null );
504+
Assert.That( result, Is.Not.SameAs( target ) );
505+
Assert.That( result.GlobalType, Is.Not.Null );
506+
Assert.That( result.GlobalType, Is.Not.SameAs( target.GlobalType ) );
507+
Assert.That( result.GlobalType.Value, Is.EqualTo( target.GlobalType.Value ) );
508+
}
509+
}
510+
<#+
511+
}
485512
#>
486513

487514
#endregion -- Polymorphism --

0 commit comments

Comments
 (0)