MakXsdDataContractExporter refactoring to generally use SerializationMap.XmlName.
authorAtsushi Eno <atsushi@ximian.com>
Wed, 16 Feb 2011 15:55:15 +0000 (00:55 +0900)
committerAtsushi Eno <atsushi@ximian.com>
Wed, 16 Feb 2011 15:55:15 +0000 (00:55 +0900)
Fixed bug #670539.

mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/KnownTypeCollection.cs
mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/SerializationMap.XsdExporter.cs
mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/SerializationMap.cs
mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/XsdDataContractExporter.cs
mcs/class/System.Runtime.Serialization/Test/System.Runtime.Serialization/XsdDataContractExporterTest.cs

index d915a7850d1ac937a3451e52f3ac9758cb4df656..c1d60d3e4c6f3626a32512887eba96110616ba14 100755 (executable)
@@ -81,6 +81,14 @@ namespace System.Runtime.Serialization
                        var arr = type.GetCustomAttributes (typeof (T), inherit);
                        return arr != null && arr.Length == 1 ? (T) arr [0] : default (T);
                }
+
+               public static IEnumerable<Type> GetInterfacesOrSelfInterface (this Type type)
+               {
+                       if (type.IsInterface)
+                               yield return type;
+                       foreach (var t in type.GetInterfaces ())
+                               yield return t;
+               }
        }
 
        internal sealed class KnownTypeCollection : Collection<Type>
@@ -431,6 +439,8 @@ namespace System.Runtime.Serialization
 
                internal Type GetSerializedType (Type type)
                {
+                       if (IsPrimitiveNotEnum (type))
+                               return type;
                        Type element = GetCollectionElementType (type);
                        if (element == null)
                                return type;
@@ -661,10 +671,9 @@ namespace System.Runtime.Serialization
                {
                        if (type.IsArray)
                                return type.GetElementType ();
-
-                       Type [] ifaces = type.GetInterfaces ();
+                       var ifaces = type.GetInterfacesOrSelfInterface ();
                        foreach (Type i in ifaces)
-                               if (i.IsGenericType && i.GetGenericTypeDefinition ().Equals (typeof (ICollection<>)))
+                               if (i.IsGenericType && i.GetGenericTypeDefinition ().Equals (typeof (IEnumerable<>)))
                                        return i.GetGenericArguments () [0];
                        foreach (Type i in ifaces)
                                if (i == typeof (IList))
@@ -730,7 +739,7 @@ namespace System.Runtime.Serialization
 
                static bool TypeImplementsIDictionary (Type type)
                {
-                       foreach (var iface in type.GetInterfaces ())
+                       foreach (var iface in type.GetInterfacesOrSelfInterface ())
                                if (iface == typeof (IDictionary) || (iface.IsGenericType && iface.GetGenericTypeDefinition () == typeof (IDictionary<,>)))
                                        return true;
 
index ce950f0b1e0d0f9af2b1fb2a9e0646d980790e8b..47fc17c477e66fedd0fb4aa96053e93c4919c3fc 100644 (file)
@@ -62,7 +62,7 @@ namespace System.Runtime.Serialization
        {
                public override void ExportSchemaType (XsdDataContractExporter exporter)
                {
-                       exporter.ExportStandardComplexType (RuntimeType.GetCustomAttribute<DataContractAttribute> (false), RuntimeType, Members);
+                       exporter.ExportStandardComplexType (RuntimeType.GetCustomAttribute<DataContractAttribute> (false), this, Members);
                }
        }
        
@@ -70,7 +70,7 @@ namespace System.Runtime.Serialization
        {
                public override void ExportSchemaType (XsdDataContractExporter exporter)
                {
-                       exporter.ExportStandardComplexType (null, RuntimeType, Members);
+                       exporter.ExportStandardComplexType (null, this, Members);
                }
        }
        
@@ -78,7 +78,7 @@ namespace System.Runtime.Serialization
        {
                public override void ExportSchemaType (XsdDataContractExporter exporter)
                {
-                       exporter.ExportListContractType (a, RuntimeType);
+                       exporter.ExportListContractType (a, this);
                }
        }
        
@@ -86,7 +86,7 @@ namespace System.Runtime.Serialization
        {
                public override void ExportSchemaType (XsdDataContractExporter exporter)
                {
-                       exporter.ExportListContractType (null, RuntimeType);
+                       exporter.ExportListContractType (null, this);
                }
        }
        
@@ -94,7 +94,7 @@ namespace System.Runtime.Serialization
        {
                public override void ExportSchemaType (XsdDataContractExporter exporter)
                {
-                       exporter.ExportDictionaryContractType (a, RuntimeType, GetGenericDictionaryInterface (RuntimeType));
+                       exporter.ExportDictionaryContractType (a, this, GetGenericDictionaryInterface (RuntimeType));
                }
        }
        
@@ -102,7 +102,7 @@ namespace System.Runtime.Serialization
        {
                public override void ExportSchemaType (XsdDataContractExporter exporter)
                {
-                       exporter.ExportStandardComplexType (null, RuntimeType, Members);
+                       exporter.ExportStandardComplexType (null, this, Members);
                }
        }
        
@@ -110,7 +110,7 @@ namespace System.Runtime.Serialization
        {
                public override void ExportSchemaType (XsdDataContractExporter exporter)
                {
-                       exporter.ExportEnumContractType (RuntimeType.GetCustomAttribute<DataContractAttribute> (false), RuntimeType);
+                       exporter.ExportEnumContractType (RuntimeType.GetCustomAttribute<DataContractAttribute> (false), this);
                }
        }
 }
index 2542424156f20199248e951f08c5a6c07c9e9a24..e4d082ff086070be608f1a4afe865330faa08f29 100644 (file)
@@ -590,8 +590,8 @@ namespace System.Runtime.Serialization
 
                static Type GetGenericCollectionInterface (Type type)
                {
-                       foreach (var iface in type.GetInterfaces ())
-                               if (iface.IsGenericType && iface.GetGenericTypeDefinition () == typeof (ICollection<>))
+                       foreach (var iface in type.GetInterfacesOrSelfInterface ())
+                               if (iface.IsGenericType && iface.GetGenericTypeDefinition () == typeof (IEnumerable<>))
                                        return iface;
 
                        return null;
@@ -731,7 +731,7 @@ namespace System.Runtime.Serialization
 
                static Type GetGenericDictionaryInterface (Type type)
                {
-                       foreach (var iface in type.GetInterfaces ())
+                       foreach (var iface in type.GetInterfacesOrSelfInterface ())
                                if (iface.IsGenericType && iface.GetGenericTypeDefinition () == typeof (IDictionary<,>))
                                        return iface;
 
index a99d4469ed72892ff6aa3e6ca38707c15bd1278d..e2294edbbd5c7f81163048ae0e22c1e82fc2be4a 100644 (file)
@@ -217,9 +217,10 @@ namespace System.Runtime.Serialization
                        return true;
                }
                
-               internal void ExportDictionaryContractType (CollectionDataContractAttribute attr, Type type, Type dicType)
+               internal void ExportDictionaryContractType (CollectionDataContractAttribute attr, SerializationMap map, Type dicType)
                {
-                       var qname = GetSchemaTypeName (type);
+                       var type = map.RuntimeType;
+                       var qname = map.XmlName;
 
                        var typeArgs = dicType.IsGenericType ? dicType.GetGenericArguments () : null;
                        var keyType = typeArgs != null ? typeArgs [0] : typeof (object);
@@ -255,9 +256,10 @@ namespace System.Runtime.Serialization
                        dictSeq.Items.Add (new XmlSchemaElement () { Name = valueName, SchemaTypeName = GetSchemaTypeName (valueType), IsNillable = true });
                }
                
-               internal void ExportListContractType (CollectionDataContractAttribute attr, Type type)
+               internal void ExportListContractType (CollectionDataContractAttribute attr, CollectionTypeMap map)
                {
-                       var qname = attr != null && attr.Name != null ? new QName (attr.Name, attr.Namespace ?? GetXmlNamespace (type)) : GetSchemaTypeName (type);
+                       Type type = map.RuntimeType;
+                       var qname = map.XmlName;
 
                        var typeArgs = type.IsGenericType ? type.GetGenericArguments () : null;
                        if (typeArgs != null && typeArgs.Length != 1)
@@ -289,9 +291,10 @@ namespace System.Runtime.Serialization
                        */
                }
 
-               internal void ExportEnumContractType (DataContractAttribute attr, Type type)
+               internal void ExportEnumContractType (DataContractAttribute attr, SerializationMap map)
                {
-                       var qname = attr != null && attr.Name != null ? new QName (attr.Name, attr.Namespace ?? GetXmlNamespace (type)) : GetSchemaTypeName (type);
+                       Type type = map.RuntimeType;
+                       var qname = map.XmlName;
                        var st = CreateSimpleType (qname, type);
                        if (type.GetCustomAttribute<FlagsAttribute> (false) != null) {
                                var list = new XmlSchemaSimpleTypeList ();
@@ -317,9 +320,10 @@ namespace System.Runtime.Serialization
                        return r;
                }
 
-               internal void ExportStandardComplexType (DataContractAttribute attr, Type type, List<DataMemberInfo> members)
+               internal void ExportStandardComplexType (DataContractAttribute attr, SerializationMap map, List<DataMemberInfo> members)
                {
-                       var qname = attr != null && attr.Name != null ? new QName (attr.Name, attr.Namespace ?? GetXmlNamespace (type)) : GetSchemaTypeName (type);
+                       Type type = map.RuntimeType;
+                       var qname = map.XmlName;
                        var ct = CreateComplexType (qname, type);
 
                        if (type.BaseType != null && type.BaseType != typeof (object)) {
@@ -423,7 +427,11 @@ namespace System.Runtime.Serialization
                string GetXmlTypeName (Type type)
                {
                        var qname = KnownTypeCollection.GetPrimitiveTypeName (type);
-                       return qname.Equals (QName.Empty) ? type.Name : qname.Name;
+                       if (!qname.Equals (QName.Empty))
+                               return qname.Name;
+                       var ret = type.Name;
+                       int idx = ret.IndexOf ('`');
+                       return idx < 0 ? ret : ret.Substring (0, idx);
                }
 
                string GetXmlNamespace (Type type)
@@ -469,6 +477,13 @@ namespace System.Runtime.Serialization
                        if (info != null && info.SchemaTypeName != null)
                                return info.SchemaTypeName;
 
+                       known_types.Add (type);
+                       var map = known_types.FindUserMap (type);
+                       if (map != null)
+                               return map.XmlName;
+
+                       // The following lines would be mostly redundant (legacy code now that it widely uses SerializationMap.XmlName for consistency...)
+
                        var cdca = type.GetCustomAttribute<CollectionDataContractAttribute> (false);
                        if (cdca != null)
                                return new QName (cdca.Name ?? GetXmlTypeName (type), cdca.Namespace ?? GetXmlNamespace (type));
index a8fcf9846261343cc30eda305b24b3eb494b52ea..21dadac1d955656001713bdb84fb523956c0b49b 100644 (file)
@@ -45,6 +45,13 @@ namespace MonoTests.System.Runtime.Serialization
        [TestFixture]
        public class XsdDataContractExporterTest
        {
+               internal const string MSSimpleNamespace =
+                       "http://schemas.microsoft.com/2003/10/Serialization/";
+               internal const string MSArraysNamespace =
+                       "http://schemas.microsoft.com/2003/10/Serialization/Arrays";
+               internal const string DefaultClrNamespaceBase =
+                       "http://schemas.datacontract.org/2004/07/";
+
                [Test]
                public void Ctor1 ()
                {
@@ -153,6 +160,14 @@ namespace MonoTests.System.Runtime.Serialization
                        CheckDcFull (xdce.Schemas);
                }
 
+               [Test]
+               public void GetSchemaTypeName ()
+               {
+                       var xdce = new XsdDataContractExporter ();
+                       // bug #670539
+                       Assert.AreEqual (new XmlQualifiedName ("ArrayOfstring", MSArraysNamespace), xdce.GetSchemaTypeName (typeof (IEnumerable<string>)), "#1");
+               }
+
                //Helper methods
 
                XmlSchemas GetSchemas (XmlSchemaSet set)