2010-04-28 Atsushi Enomoto <atsushi@ximian.com>
authorAtsushi Eno <atsushieno@gmail.com>
Wed, 28 Apr 2010 01:08:01 +0000 (01:08 -0000)
committerAtsushi Eno <atsushieno@gmail.com>
Wed, 28 Apr 2010 01:08:01 +0000 (01:08 -0000)
* KnownTypeCollection.cs : support DataContractNamespaceAttribute.
  Fixed bug #599889.

* XmlObjectSerializerTest.cs : add test for bug #599899.

svn path=/trunk/mcs/; revision=156277

mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/ChangeLog
mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/KnownTypeCollection.cs
mcs/class/System.Runtime.Serialization/Test/System.Runtime.Serialization/ChangeLog
mcs/class/System.Runtime.Serialization/Test/System.Runtime.Serialization/XmlObjectSerializerTest.cs

index 7f4f2d8e3a044823ffe012930b93d363057867a0..7670edc14290b1ee12ad561a24a0808fa2219efc 100755 (executable)
@@ -1,3 +1,8 @@
+2010-04-28  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * KnownTypeCollection.cs : support DataContractNamespaceAttribute.
+         Fixed bug #599889.
+
 2010-04-05  Atsushi Enomoto  <atsushi@ximian.com>
 
        * DataContractExporter-new.cs : do not expect contract attribute on
index 62ace1948be4ee6dd1d749b8232b2f0627d93490..c6501cacaef9b6081d45f99e6999d2247948cadb 100644 (file)
@@ -510,7 +510,7 @@ namespace System.Runtime.Serialization
                                }
                        }
                        if (ns == null)
-                               ns = DefaultClrNamespaceBase + type.Namespace;
+                               ns = GetDefaultNamespace (type);
                        return new QName (name, ns);
                }
 
@@ -529,7 +529,7 @@ namespace System.Runtime.Serialization
                        }
 
                        if (ns == null)
-                               ns = DefaultClrNamespaceBase + type.Namespace;
+                               ns = GetDefaultNamespace (type);
 
                        if (name == null)
                                name = type.Namespace == null ? type.Name : type.FullName.Substring (type.Namespace.Length + 1).Replace ('+', '.');
@@ -537,6 +537,14 @@ namespace System.Runtime.Serialization
                        return new QName (name, ns);
                }
 
+               static string GetDefaultNamespace (Type type)
+               {
+                       foreach (ContractNamespaceAttribute a in type.Assembly.GetCustomAttributes (typeof (ContractNamespaceAttribute), true))
+                               if (a.ClrNamespace == type.Namespace)
+                                       return a.ContractNamespace;
+                       return DefaultClrNamespaceBase + type.Namespace;
+               }
+
                static QName GetCollectionQName (Type element)
                {
                        QName eqname = GetStaticQName (element);
@@ -559,7 +567,7 @@ namespace System.Runtime.Serialization
                                foreach (var t in type.GetGenericArguments ())
                                        xmlName += GetStaticQName (t).Name; // FIXME: check namespaces too
                        }
-                       string xmlNamespace = DefaultClrNamespaceBase + type.Namespace;
+                       string xmlNamespace = GetDefaultNamespace (type);
                        var x = GetAttribute<XmlRootAttribute> (type);
                        if (x != null) {
                                xmlName = x.ElementName;
@@ -633,9 +641,9 @@ namespace System.Runtime.Serialization
                        return null;
                }
 
-               internal static T GetAttribute<T> (MemberInfo mi) where T : Attribute
+               internal static T GetAttribute<T> (ICustomAttributeProvider ap) where T : Attribute
                {
-                       object [] atts = mi.GetCustomAttributes (typeof (T), false);
+                       object [] atts = ap.GetCustomAttributes (typeof (T), false);
                        return atts.Length == 0 ? null : (T) atts [0];
                }
 
index b266853def1485fdc6a89adf533e92fb8f6bb4bc..f3394f5bf4352d7863d658b09473150ed64e98ce 100644 (file)
@@ -1,3 +1,7 @@
+2010-04-28  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * XmlObjectSerializerTest.cs : add test for bug #599899.
+
 2010-03-04  Atsushi Enomoto  <atsushi@ximian.com>
 
        * XmlObjectSerializerTest.cs : test for empty array deserialization.
index d4bf7c032b50d6f0d564742f233c5b979b824f3e..49abbf5ae1a0211397984be5afb17a0a93aa6192 100644 (file)
@@ -46,6 +46,8 @@ using NUnit.Framework;
 using System.Xml.Serialization;
 using System.Xml.Schema;
 
+[assembly: ContractNamespace ("http://www.u2u.be/samples/wcf/2009", ClrNamespace = "U2U.DataContracts")] // bug #599889
+
 namespace MonoTests.System.Runtime.Serialization
 {
        [TestFixture]
@@ -1370,6 +1372,21 @@ namespace MonoTests.System.Runtime.Serialization
                        var ret = ds.ReadObject (xr);
                        Assert.AreEqual (typeof (string []), ret.GetType (), "#1");
                }
+               
+               [Test]
+               public void ContractNamespaceAttribute ()
+               {
+                       var ds = new DataContractSerializer (typeof (U2U.DataContracts.Person));
+                       string xml = "<?xml version='1.0' encoding='utf-16'?><Person xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://www.u2u.be/samples/wcf/2009'><Name>Rupert</Name><Occupation><Description>Monkey</Description></Occupation></Person>";
+                       var person = new U2U.DataContracts.Person () {
+                               Name = "Rupert",
+                               Occupation = new U2U.DataContracts.Job () { Description = "Monkey" }
+                               };
+                       var sw = new StringWriter ();
+                       using (var xw = XmlWriter.Create (sw))
+                               ds.WriteObject (xw, person);
+                       Assert.AreEqual (xml, sw.ToString ().Replace ('"', '\''), "#1");
+               }
        }
 
        [DataContract]
@@ -1751,3 +1768,24 @@ public class Person
                return string.Format ("name={0},id={1}", name, Id);
        }
 }
+
+// bug #599889
+namespace U2U.DataContracts
+{
+       [DataContract]
+       public class Person
+       {
+               [DataMember]
+               public string Name { get; set; }
+
+               [DataMember]
+               public Job Occupation { get; set; }
+       }
+
+       [DataContract]
+       public class Job
+       {
+               [DataMember]
+               public string Description { get; set; }
+       }
+}