support import of xs:anyType, as well as make couple of minor fixes.
authorAtsushi Eno <atsushi@ximian.com>
Tue, 10 May 2011 12:45:45 +0000 (21:45 +0900)
committerAtsushi Eno <atsushi@ximian.com>
Tue, 10 May 2011 12:45:45 +0000 (21:45 +0900)
mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/XsdDataContractImporter.cs
mcs/class/System.Runtime.Serialization/Test/System.Runtime.Serialization/XsdDataContractImporterTest.cs

index 90654b04dd56ff91fb7223c91f24bec48fcbc280..bb416dcef777c4f4446737064cd63afcffd82b61 100644 (file)
@@ -78,7 +78,8 @@ namespace System.Runtime.Serialization
                        var cns = GetCodeNamespace (qname.Namespace);
                        var td = new CodeTypeDeclaration () {
                                Name = GetUniqueName (CodeIdentifier.MakeValid (qname.Name), cns),
-                               TypeAttributes = GenerateInternal ? TypeAttributes.NotPublic : TypeAttributes.Public };
+                               TypeAttributes = GenerateInternal ? TypeAttributes.NotPublic : TypeAttributes.Public,
+                               IsPartial = true };
                        cns.Types.Add (td);
                        td.BaseTypes.Add (new CodeTypeReference (typeof (IXmlSerializable)));
 
@@ -193,7 +194,13 @@ namespace System.Runtime.Serialization
                        if (!schemas.IsCompiled)
                                schemas.Compile ();
 
-                       return CanImport (schemas, element.ElementSchemaType as XmlSchemaType);
+                       if (element.ElementSchemaType != null)
+                               return CanImport (schemas, element.ElementSchemaType as XmlSchemaType);
+                       else if (element.SchemaTypeName != null && !element.SchemaTypeName.Equals (QName.Empty))
+                               return CanImport (schemas, element.SchemaTypeName);
+                       else
+                               // anyType
+                               return true;
                }
 
 
@@ -410,6 +417,9 @@ namespace System.Runtime.Serialization
                        if (typeName == null)
                                throw new ArgumentNullException ("typeName");
 
+                       if (!schemas.IsCompiled)
+                               schemas.Compile ();
+
                        if (IsPredefinedType (typeName))
                                return;
 
@@ -428,6 +438,9 @@ namespace System.Runtime.Serialization
 
                        var elname = element.QualifiedName;
 
+                       if (IsPredefinedType (element.SchemaTypeName))
+                               return elname;
+
                        switch (elname.Namespace) {
                        case KnownTypeCollection.MSSimpleNamespace:
                                switch (elname.Name) {
@@ -446,8 +459,19 @@ namespace System.Runtime.Serialization
                        }
 
                        // FIXME: use element to fill nillable and arrays.
-                       var qname = element.SchemaType != null ? element.QualifiedName : element.ElementSchemaType.QualifiedName;
-                       Import (schemas, element.ElementSchemaType, qname);
+                       var qname =
+                               elname != null && !elname.Equals (QName.Empty) ? elname :
+                               element.ElementSchemaType != null ? element.ElementSchemaType.QualifiedName :
+                               qname_anytype;
+
+                       if (element.ElementSchemaType != null)
+                               Import (schemas, element.ElementSchemaType, qname);
+                       else if (element.SchemaTypeName != null && !element.SchemaTypeName.Equals (QName.Empty))
+                               Import (schemas, schemas.GlobalTypes [element.SchemaTypeName] as XmlSchemaType, qname);
+                       // otherwise it is typeless == anyType.
+                       else
+                               Import (schemas, XmlSchemaType.GetBuiltInComplexType (qname_anytype), qname);
+
                        return qname;
                }
 
@@ -491,7 +515,8 @@ namespace System.Runtime.Serialization
 
                        var td = new CodeTypeDeclaration () {
                                Name = GetUniqueName (CodeIdentifier.MakeValid (qname.Name), cns),
-                               TypeAttributes = GenerateInternal ? TypeAttributes.NotPublic : TypeAttributes.Public };
+                               TypeAttributes = GenerateInternal ? TypeAttributes.NotPublic : TypeAttributes.Public,
+                               IsPartial = true };
                        cns.Types.Add (td);
 
                        var info = new TypeImportInfo () { ClrType = clrRef, XsdType = type,  XsdTypeName = qname };
@@ -789,6 +814,8 @@ namespace System.Runtime.Serialization
 
                bool IsPredefinedType (XmlQualifiedName qname)
                {
+                       if (qname == null)
+                               return false;
                        switch (qname.Namespace) {
                        case KnownTypeCollection.MSSimpleNamespace:
                                return KnownTypeCollection.GetPrimitiveTypeFromName (qname) != null;
index e5423fc08261eaa07b772db7e50b891529e4105b..f3147b4d51646616fead41564218c60e4ffa94da 100644 (file)
@@ -37,6 +37,7 @@ using System.CodeDom;
 using System.CodeDom.Compiler;
 using System.Collections.Generic;
 using System.IO;
+using System.Linq;
 using System.Runtime.Serialization;
 using System.ServiceModel.Description;
 using System.Xml;
@@ -70,6 +71,7 @@ namespace MonoTests.System.Runtime.Serialization
                                if (section.Metadata is XmlSchema)
                                        xss.Add (section.Metadata as XmlSchema);
 
+                       Assert.AreEqual (3, xss.Schemas ().Count, "#1");
                        return xss;
                }
 
@@ -94,7 +96,6 @@ namespace MonoTests.System.Runtime.Serialization
 
                [Test]
                [ExpectedException (typeof (InvalidOperationException))]
-               [Category ("NotWorking")]
                public void GetCodeTypeReferenceTest ()
                {
                        XsdDataContractImporter xsdi = GetImporter ();
@@ -102,7 +103,6 @@ namespace MonoTests.System.Runtime.Serialization
                }
 
                [Test]
-               [Category ("NotWorking")]
                public void GetCodeTypeReferenceTest2 ()
                {
                        NewXmlSchemaSet ();
@@ -258,7 +258,6 @@ namespace MonoTests.System.Runtime.Serialization
                }
 
                [Test]
-               [Category ("NotWorking")]
                public void ImportDataContract1 ()
                {
                        NewXmlSchemaSet ();
@@ -286,7 +285,6 @@ namespace MonoTests.System.Runtime.Serialization
                }
 
                [Test]
-               [Category ("NotWorking")]
                public void ImportDataContract2 ()
                {
                        NewXmlSchemaSet ();
@@ -315,7 +313,6 @@ namespace MonoTests.System.Runtime.Serialization
 
                [Test]
                [ExpectedException (typeof (InvalidDataContractException))]
-               [Category ("NotWorking")]
                public void ImportMessageEcho ()
                {
                        XsdDataContractImporter xsdi = GetImporter ();
@@ -740,16 +737,19 @@ namespace MonoTests.System.Runtime.Serialization
 
                private void CheckDataContractAttribute (CodeTypeDeclaration type, string msg)
                {
-                       Assert.AreEqual (3, type.CustomAttributes.Count, msg + "a");
+                       // DebuggerStepThrouAttribute is insignificant. So, no reason to check the attribute count.
+                       // Assert.AreEqual (3, type.CustomAttributes.Count, msg + "a");
 
                        // DebuggerStepThroughAttribute - skip it
 
                        //GeneratedCodeAttribute
-                       CodeAttributeDeclaration ca = type.CustomAttributes [1];
-                       Assert.AreEqual ("System.CodeDom.Compiler.GeneratedCodeAttribute", ca.Name, msg + "b");
+                       var l = new List<CodeAttributeDeclaration> ();
+                       foreach (CodeAttributeDeclaration a in type.CustomAttributes)
+                               l.Add (a);
+                       Assert.IsTrue (l.Any (a => a.Name == "System.CodeDom.Compiler.GeneratedCodeAttribute"), msg + "b");
 
-                       ca = type.CustomAttributes [2];
-                       Assert.AreEqual ("System.Runtime.Serialization.DataContractAttribute", ca.Name, msg + "c");
+                       var ca = l.FirstOrDefault (a => a.Name == "System.Runtime.Serialization.DataContractAttribute");
+                       Assert.IsNotNull (ca, msg + "b");
                        Assert.AreEqual (2, ca.Arguments.Count, msg + "d");
                }