From eb532e6209dfa5ba1284816d0c8a986c6fcb79d8 Mon Sep 17 00:00:00 2001 From: Michael Ganss Date: Tue, 14 Jul 2020 12:23:23 +0200 Subject: [PATCH] Fix #214 --- XmlSchemaClassGenerator.Tests/XmlTests.cs | 57 +++++++++++++++++++++++ XmlSchemaClassGenerator/TypeModel.cs | 2 + 2 files changed, 59 insertions(+) diff --git a/XmlSchemaClassGenerator.Tests/XmlTests.cs b/XmlSchemaClassGenerator.Tests/XmlTests.cs index 5238d180..3f8c5002 100644 --- a/XmlSchemaClassGenerator.Tests/XmlTests.cs +++ b/XmlSchemaClassGenerator.Tests/XmlTests.cs @@ -2063,5 +2063,62 @@ public void TestForceIsNullableGeneration() var xmlElementNullableAttribute2 = prop.GetCustomAttribute(); Assert.True(xmlElementNullableAttribute2.IsNullable); } + + [Fact] + public void TestArrayOfMsTypeGeneration() + { + // see https://github.com/mganss/XmlSchemaClassGenerator/issues/214 + + var xsd0 = + @" + + + + + + + + "; + var xsd1 = + @" + + + + + + + + + + + + + + + "; + var validXml = + @" + + String + String + String + + + "; + var generator = new Generator + { + IntegerDataType = typeof(int), + NamespacePrefix = "Test_NS1", + GenerateNullables = true, + CollectionType = typeof(System.Collections.Generic.List<>) + }; + var contents = ConvertXml(nameof(TestArrayOfMsTypeGeneration), new[] { xsd0, xsd1 }, generator).ToArray(); + var assembly = Compiler.Compile(nameof(TestForceIsNullableGeneration), contents); + var testType = assembly.GetType("Test_NS1.C_Ai"); + var serializer = new XmlSerializer(testType); + Assert.NotNull(serializer); + dynamic deserialized = serializer.Deserialize(new StringReader(validXml)); + Assert.NotEmpty((System.Collections.IEnumerable)deserialized.D); //<== oops + } } } diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index f8681a97..da096c87 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -1129,6 +1129,8 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi { var arrayItemAttribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(XmlArrayItemAttribute), Configuration.CodeTypeReferenceOptions), propertyAttribute.Arguments.Cast().Where(x => !string.Equals(x.Name, "Order", StringComparison.Ordinal)).ToArray()); + if (!arrayItemProperty.XmlSchemaName.IsEmpty && !string.IsNullOrEmpty(arrayItemProperty.XmlSchemaName.Namespace)) + arrayItemAttribute.Arguments.Add(new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(arrayItemProperty.XmlSchemaName.Namespace))); member.CustomAttributes.Add(arrayItemAttribute); } }