Bug 27133 - DataContractSerializer fails to parse type name
Summary: DataContractSerializer fails to parse type name
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: WCF assemblies (show other bugs)
Version: master
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-02-17 06:58 UTC by Alexander Jesner
Modified: 2015-02-17 07:15 UTC (History)
2 users (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments

Description Alexander Jesner 2015-02-17 06:58:56 UTC
When using DataContractSerializer with nested classes where the outer class is generic, the serializer fails to correctly parse the type name of the inner class.

When trying to parse the name of the inner class, a System.ArgumentOutOfRangeException is thrown.

Consider the following code:

{code}
using System;
using System.IO;
using System.Runtime.Serialization;
using NUnit.Framework;

namespace Tests
{
    [KnownType("GetKnownType")]
    public class MyOuterClass<T>
    {
        public class MyInnerClass
        {
            public string MyProperty { get; set; }
        }

        private static Type[] GetKnownType()
        {
            // Provide types for DataContractSerializer.
            // Cannot be done via attributes because of generics.

            return new[]
            {
                typeof (T),
                typeof (MyOuterClass<T>),
                typeof (MyInnerClass),
            };
        }
    }

    [TestFixture]
    public class TestDataContractBug
    {
        [Test]
        public void TrySerialize()
        {
            using (var mem = new MemoryStream())
            {
                var o = new MyOuterClass<int>();
                var ser = new DataContractSerializer(typeof (MyOuterClass<long>));
                ser.WriteObject(mem, o);
            }
        }
    }
}
{code}

when running NUnit the following stack trace is printed:

{stacktrace}
2) Tests.TestDataContractBug.TrySerialize : System.ArgumentOutOfRangeException : Cannot be negative.
Parameter name: length
at System.String.Substring (Int32 startIndex, Int32 length) [0x0007c] in /home/jazz/mono-3.12.0/mcs/class/corlib/System/String.cs:451
at System.Runtime.Serialization.KnownTypeCollection.GetSerializableQName (System.Type type) [0x00075] in /home/jazz/mono-3.12.0/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/KnownTypeCollection.cs:707
at System.Runtime.Serialization.KnownTypeCollection.RegisterSerializable (System.Type type) [0x00000] in /home/jazz/mono-3.12.0/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/KnownTypeCollection.cs:984
at System.Runtime.Serialization.KnownTypeCollection.DoTryRegister (System.Type type) [0x0007b] in /home/jazz/mono-3.12.0/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/KnownTypeCollection.cs:777
at System.Runtime.Serialization.KnownTypeCollection.TryRegister (System.Type type) [0x0000c] in /home/jazz/mono-3.12.0/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/KnownTypeCollection.cs:745
at System.Runtime.Serialization.KnownTypeCollection.InsertItem (Int32 index, System.Type type) [0x00019] in /home/jazz/mono-3.12.0/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/KnownTypeCollection.cs:482
at System.Collections.ObjectModel.Collection`1[System.Type].Add (System.Type item) [0x0000c] in /home/jazz/mono-3.12.0/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:81
at System.Runtime.Serialization.DataContractSerializer.RegisterTypeAsKnown (System.Type type) [0x00032] in /home/jazz/mono-3.12.0/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/DataContractSerializer.cs:257
at System.Runtime.Serialization.DataContractSerializer.RegisterTypeAsKnown (System.Type type) [0x0007c] in /home/jazz/mono-3.12.0/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/DataContractSerializer.cs:264
at System.Runtime.Serialization.DataContractSerializer.PopulateTypes (IEnumerable`1 knownTypes) [0x0007a] in /home/jazz/mono-3.12.0/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/DataContractSerializer.cs:243
at System.Runtime.Serialization.DataContractSerializer..ctor (System.Type type, IEnumerable`1 knownTypes) [0x0002f] in /home/jazz/mono-3.12.0/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/DataContractSerializer.cs:76
at System.Runtime.Serialization.DataContractSerializer..ctor (System.Type type) [0x00000] in /home/jazz/mono-3.12.0/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/DataContractSerializer.cs:65
at Tests.TestDataContractBug.TrySerialize () [0x00000] in <filename unknown>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00054] in /home/jazz/mono-3.12.0/mcs/class/corlib/System.Reflection/MonoMethod.cs:230
{stacktrace}


I analysed the KnownTypeCollection and it seems the type name parsing does not work correctly.
I am using mono snapshot 20150217075448 and I did the analysis by looking at https://github.com/mono/mono/blob/c1b43669320f96e4a2a482d993b7b36bb5e59496/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/KnownTypeCollection.cs

around line 697 the code says:
{code}
string xmlName = type.Name;
if (type.IsGenericType) {
  xmlName = xmlName.Substring (0, xmlName.IndexOf ('`')) + "Of";
  foreach (var t in type.GetGenericArguments ())
    xmlName += GetStaticQName (t).Name; // FIXME: check namespaces too
}
{code}

The bug is triggered when the serializer tries to resolve the types provided in the KnownTypes() method for MyOuterClass.
When it tries to resolve MyOuterClass<int>.MyInnerClass, line 697 in the KnownTypeCollection yields xmlName = "MyInnerClass". Since the type is generic we enter the if, try to find IndexOf("`") which is -1. The substring then tries to slice from (0, -1) which obviously fails.

I think the xmlName should also include the name of all outer classes, e.g. we should have MyOuterClass`1+MyInnerClass here.

Note You need to log in before you can comment on or make changes to this bug.