Skip to content

Commit

Permalink
Fix #214
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ganss committed Jul 14, 2020
1 parent d3c3509 commit eb532e6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
57 changes: 57 additions & 0 deletions XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2063,5 +2063,62 @@ public void TestForceIsNullableGeneration()
var xmlElementNullableAttribute2 = prop.GetCustomAttribute<XmlElementAttribute>();
Assert.True(xmlElementNullableAttribute2.IsNullable);
}

[Fact]
public void TestArrayOfMsTypeGeneration()
{
// see https://github.com/mganss/XmlSchemaClassGenerator/issues/214

var xsd0 =
@"<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:tns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"" targetNamespace=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"" elementFormDefault=""qualified"">
<xs:complexType name=""ArrayOfstring"">
<xs:sequence>
<xs:element name=""string"" type=""xs:string"" nillable=""true"" minOccurs=""0"" maxOccurs=""unbounded""/>
</xs:sequence>
</xs:complexType>
<xs:element name=""ArrayOfstring"" type=""tns:ArrayOfstring"" nillable=""true""/>
</xs:schema>
";
var xsd1 =
@"<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:q1=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"" elementFormDefault=""qualified"">
<xs:import namespace=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""/>
<xs:complexType name=""c_ai"">
<xs:sequence>
<xs:element name=""d"" type=""q1:ArrayOfstring"" nillable=""true"" minOccurs=""0"">
<xs:annotation>
<xs:appinfo>
<DefaultValue EmitDefaultValue=""false"" xmlns=""http://schemas.microsoft.com/2003/10/Serialization/""/>
</xs:appinfo>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name=""c_ai"" type=""c_ai"" nillable=""true""/>
</xs:schema>
";
var validXml =
@"<c_ai xmlns:tns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"">
<d>
<tns:string>String</tns:string>
<tns:string>String</tns:string>
<tns:string>String</tns:string>
</d>
</c_ai>
";
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
}
}
}
2 changes: 2 additions & 0 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,8 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
{
var arrayItemAttribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(XmlArrayItemAttribute), Configuration.CodeTypeReferenceOptions),
propertyAttribute.Arguments.Cast<CodeAttributeArgument>().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);
}
}
Expand Down

0 comments on commit eb532e6

Please sign in to comment.