Skip to content

Commit c25a374

Browse files
authored
Merge pull request #272 from msgpack/fix/issue-269-2
Fix ext type type error related issues
2 parents ef57f2e + 3fa4daf commit c25a374

File tree

5 files changed

+59
-10
lines changed

5 files changed

+59
-10
lines changed

CHANGES.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,4 +741,7 @@ Release 1.0.0-rc1 T.B.D.
741741

742742
BUG FIXES
743743
* Fix NRE in .NET Standard 1.1/1.3 build (this issue got mixed in beta2).
744-
* Fix built-in Guid/BigInteger always output raw type even if PackerCompatibilityOptions.PackBinaryAsRaw is not specified. #270
744+
* Fix built-in Guid/BigInteger always output raw type even if PackerCompatibilityOptions.PackBinaryAsRaw is not specified. #270
745+
* Fix MessagePackObject.UnderlyingType reports wrong type for ext types. Part of #269.
746+
This bug also caused misleading error message for incompatible type conversion.
747+
* Fix exceptions thrown by MessagePackObject.AsBinary()/AsString() reports internal type name. Part of #269.

src/MsgPack/MessagePackObject.Utilities.cs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,14 @@ public Type UnderlyingType
10371037
{
10381038
return asMps.GetUnderlyingType();
10391039
}
1040+
else if ( this._handleOrTypeCode is byte[] )
1041+
{
1042+
// It should be MPETO
1043+
#if DEBUG
1044+
Contract.Assert( ( this._value & 0xFFFFFFFFFFFFFF00 ) == 0, "( " + this._value.ToString( "X16" ) + " & 0xFFFFFFFFFFFFFF00 ) != 0" );
1045+
#endif // DEBUG
1046+
return typeof( MessagePackExtendedTypeObject );
1047+
}
10401048
else
10411049
{
10421050
return this._handleOrTypeCode.GetType();
@@ -1355,7 +1363,7 @@ public string AsString( Encoding encoding )
13551363
return null;
13561364
}
13571365

1358-
VerifyUnderlyingType<MessagePackString>( this, null );
1366+
VerifyUnderlyingRawType<string>( this, null );
13591367

13601368
try
13611369
{
@@ -1395,7 +1403,7 @@ public string AsStringUtf8()
13951403
/// </remarks>
13961404
public string AsStringUtf16()
13971405
{
1398-
VerifyUnderlyingType<byte[]>( this, null );
1406+
VerifyUnderlyingRawType<string>( this, null );
13991407
Contract.EndContractBlock();
14001408

14011409
if ( this.IsNil )
@@ -1509,6 +1517,10 @@ public MessagePackObjectDictionary AsDictionary()
15091517

15101518
private static void VerifyUnderlyingType<T>( MessagePackObject instance, string parameterName )
15111519
{
1520+
#if DEBUG
1521+
Contract.Assert( typeof( T ) != typeof( MessagePackString ), "Should use VerifyUnderlyingRawType()" );
1522+
#endif // DEBUG
1523+
15121524
if ( instance.IsNil )
15131525
{
15141526
if ( !typeof( T ).GetIsValueType() || Nullable.GetUnderlyingType( typeof( T ) ) != null )
@@ -1540,6 +1552,24 @@ private static void VerifyUnderlyingType<T>( MessagePackObject instance, string
15401552
}
15411553
}
15421554

1555+
private static void VerifyUnderlyingRawType<T>( MessagePackObject instance, string parameterName )
1556+
{
1557+
if ( instance._handleOrTypeCode == null || instance._handleOrTypeCode is MessagePackString )
1558+
{
1559+
// nil or MPS (eventually string or byte[])
1560+
return;
1561+
}
1562+
1563+
if ( parameterName != null )
1564+
{
1565+
throw new ArgumentException( String.Format( CultureInfo.CurrentCulture, "Do not convert {0} MessagePackObject to {1}.", instance.UnderlyingType, typeof( T ) ), parameterName );
1566+
}
1567+
else
1568+
{
1569+
ThrowInvalidTypeAs<T>( instance );
1570+
}
1571+
}
1572+
15431573
private static void ThrowCannotBeNilAs<T>()
15441574
{
15451575
throw new InvalidOperationException( String.Format( CultureInfo.CurrentCulture, "Do not convert nil MessagePackObject to {0}.", typeof( T ) ) );
@@ -1845,7 +1875,7 @@ public static implicit operator MessagePackObject( MessagePackObject[] value )
18451875
return new MessagePackObject( value, false );
18461876
}
18471877

1848-
#endregion -- Conversion Operator Overloads --
1878+
#endregion -- Conversion Operator Overloads --
18491879

18501880
#if DEBUG
18511881
internal string DebugDump()

src/MsgPack/MessagePackObject.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ public Double AsDouble()
774774
/// <returns><see cref="String" /> instance corresponds to this instance.</returns>
775775
public String AsString()
776776
{
777-
VerifyUnderlyingType<MessagePackString>( this, null );
777+
VerifyUnderlyingRawType<string>( this, null );
778778

779779
if( this._handleOrTypeCode == null )
780780
{
@@ -793,7 +793,7 @@ public String AsString()
793793
/// <returns><see cref="Byte" />[] instance corresponds to this instance.</returns>
794794
public Byte[] AsBinary()
795795
{
796-
VerifyUnderlyingType<MessagePackString>( this, null );
796+
VerifyUnderlyingRawType<byte[]>( this, null );
797797

798798
if( this._handleOrTypeCode == null )
799799
{
@@ -1603,7 +1603,7 @@ public static explicit operator Double( MessagePackObject value )
16031603
/// <returns><see cref="String" /> instance corresponds to <paramref name="value"/>.</returns>
16041604
public static explicit operator String( MessagePackObject value )
16051605
{
1606-
VerifyUnderlyingType<MessagePackString>( value, "value" );
1606+
VerifyUnderlyingRawType<string>( value, "value" );
16071607

16081608
if( value._handleOrTypeCode == null )
16091609
{
@@ -1623,7 +1623,7 @@ public static explicit operator String( MessagePackObject value )
16231623
/// <returns><see cref="Byte" />[] instance corresponds to <paramref name="value"/>.</returns>
16241624
public static explicit operator Byte[]( MessagePackObject value )
16251625
{
1626-
VerifyUnderlyingType<MessagePackString>( value, "value" );
1626+
VerifyUnderlyingRawType<byte[]>( value, "value" );
16271627

16281628
if( value._handleOrTypeCode == null )
16291629
{

src/MsgPack/MessagePackObject.tt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ private void GenerateAsT( string val, Object typeOrTypeName, bool passParameterN
408408
else if ( t == typeof( byte[] ) )
409409
{
410410
#>
411-
VerifyUnderlyingType<MessagePackString>( <#= val #>, <#= passParameterName ? "\"" + val + "\"" : "null" #> );
411+
VerifyUnderlyingRawType<byte[]>( <#= val #>, <#= passParameterName ? "\"" + val + "\"" : "null" #> );
412412

413413
if( <#= val #>._handleOrTypeCode == null )
414414
{
@@ -424,7 +424,7 @@ private void GenerateAsT( string val, Object typeOrTypeName, bool passParameterN
424424
else if ( t == typeof( string ) )
425425
{
426426
#>
427-
VerifyUnderlyingType<MessagePackString>( <#= val #>, <#= passParameterName ? "\"" + val + "\"" : "null" #> );
427+
VerifyUnderlyingRawType<string>( <#= val #>, <#= passParameterName ? "\"" + val + "\"" : "null" #> );
428428

429429
if( <#= val #>._handleOrTypeCode == null )
430430
{

test/MsgPack.UnitTest/Serialization/RegressionTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,5 +531,21 @@ public async Task TestIssue270Async_AsRaw()
531531
}
532532

533533
#endif // FEATURE_TAP
534+
535+
[Test]
536+
public void TestIssue269()
537+
{
538+
var input = new MessagePackObject( Timestamp.UtcNow.Encode() );
539+
var target = MessagePackSerializer.UnpackMessagePackObject( MessagePackSerializer.Get<MessagePackObject>().PackSingleObject( input ) );
540+
Assert.That( target.UnderlyingType, Is.EqualTo( typeof( MessagePackExtendedTypeObject ) ) );
541+
Assert.That( target.IsTypeOf<byte[]>(), Is.False );
542+
Assert.That( target.IsTypeOf<MessagePackExtendedTypeObject>(), Is.True );
543+
544+
var forBinary = Assert.Throws<InvalidOperationException>( () => target.AsBinary() );
545+
Assert.That( forBinary.Message, Is.EqualTo( "Do not convert MsgPack.MessagePackExtendedTypeObject MessagePackObject to System.Byte[]." ) );
546+
547+
var forString = Assert.Throws<InvalidOperationException>( () => target.AsString() );
548+
Assert.That( forString.Message, Is.EqualTo( "Do not convert MsgPack.MessagePackExtendedTypeObject MessagePackObject to System.String." ) );
549+
}
534550
}
535551
}

0 commit comments

Comments
 (0)