Fixing NullReferenceException in XmlSchemaAnyAttribute.Namespace (dotnet/corefx#41731)
authorMartin Frýdl <mfrydl@gmail.com>
Tue, 15 Oct 2019 17:45:35 +0000 (19:45 +0200)
committerKrzysztof Wicher <mordotymoja@gmail.com>
Tue, 15 Oct 2019 17:45:35 +0000 (10:45 -0700)
* Fixing NullReferenceException when XmlSchemaAnyAttribute.Namespace is not set (dotnet/corefx#41704)

* Fixing tests according to review

Commit migrated from https://github.com/dotnet/corefx/commit/75d19b61b20197f3e6348f5e444e9e3af0f0750a

src/libraries/System.Private.Xml/src/System/Xml/Schema/XmlSchemaAnyAttribute.cs
src/libraries/System.Private.Xml/tests/XmlSchema/XmlSchemaSet/TC_SchemaSet_AnyAttribute.cs

index 7fb2547..5601809 100644 (file)
@@ -17,7 +17,7 @@ namespace System.Xml.Schema
         [XmlAttribute("namespace")]
         public string Namespace
         {
-            get { return _ns ?? NamespaceList.ToString(); }
+            get { return _ns ?? NamespaceList?.ToString(); }
             set { _ns = value; }
         }
 
index 544318b..4ae4e3e 100644 (file)
@@ -240,6 +240,26 @@ namespace System.Xml.Tests
             CompareWildcardNamespaces(expectedNs, attributeWildcard.Namespace);
         }
 
+        [Fact]
+        public void NewInstanceReturnsNullNamespace()
+        {
+            var any = new XmlSchemaAnyAttribute();
+            Assert.Null(any.Namespace);
+        }
+
+        [Fact]
+        public void ReadFromFileWithoutNamespaceReturnsNull()
+        {
+            var xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='ns'>
+                        <xs:complexType name = 't'>
+                            <xs:anyAttribute/>
+                        </xs:complexType>
+                        </xs:schema>";
+            XmlSchema xs = XmlSchema.Read(new StringReader(xsd), null);
+            XmlSchemaAnyAttribute any = ((XmlSchemaComplexType)xs.Items[0]).AnyAttribute;
+            Assert.Null(any.Namespace);
+        }
+
         private static void CompareWildcardNamespaces(string expected, string actual)
         {
             var orderedExpected = string.Join(" ", expected.Split(' ').OrderBy(ns => ns));