Skip to content

Commit dd4c63f

Browse files
committed
Add tests for nested non-nullable choice members
1 parent c72e203 commit dd4c63f

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

XmlSchemaClassGenerator.Tests/XmlTests.cs

+83
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,89 @@ public void ChoiceMembersAreNullable()
11001100
Assert.Contains("Opt4Specified", content);
11011101
}
11021102

1103+
[Fact]
1104+
public void NestedElementInChoiceIsNullable()
1105+
{
1106+
// Because nullability isn't directly exposed in the generated C#, we use "XXXSpecified" on a value type
1107+
// as a proxy.
1108+
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
1109+
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
1110+
<xs:element name=""Root"">
1111+
<xs:complexType>
1112+
<xs:choice>
1113+
<xs:sequence>
1114+
<xs:element name=""ElementA"" type=""xs:int""/>
1115+
</xs:sequence>
1116+
<xs:group ref=""Group""/>
1117+
</xs:choice>
1118+
</xs:complexType>
1119+
</xs:element>
1120+
1121+
<xs:group name=""Group"">
1122+
<xs:sequence>
1123+
<xs:element name=""ElementB"" type=""xs:int""/>
1124+
</xs:sequence>
1125+
</xs:group>
1126+
</xs:schema>";
1127+
1128+
var generator = new Generator
1129+
{
1130+
NamespaceProvider = new NamespaceProvider
1131+
{
1132+
GenerateNamespace = key => "Test"
1133+
}
1134+
};
1135+
var contents = ConvertXml(nameof(NestedElementInChoiceIsNullable), xsd, generator);
1136+
var content = Assert.Single(contents);
1137+
1138+
Assert.Contains("ElementASpecified", content);
1139+
Assert.Contains("ElementBSpecified", content);
1140+
}
1141+
1142+
[Fact]
1143+
public void OnlyFirstElementOfNestedElementsIsForcedToNullableInChoice()
1144+
{
1145+
// Because nullability isn't directly exposed in the generated C#, we use the "RequiredAttribute"
1146+
// as a proxy.
1147+
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
1148+
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
1149+
<xs:element name=""Root"">
1150+
<xs:complexType>
1151+
<xs:choice>
1152+
<xs:element name=""ElementWithChild"">
1153+
<xs:complexType>
1154+
<xs:sequence>
1155+
<xs:element name=""NestedChild"" type=""xs:int""/>
1156+
</xs:sequence>
1157+
</xs:complexType>
1158+
</xs:element>
1159+
</xs:choice>
1160+
</xs:complexType>
1161+
</xs:element>
1162+
</xs:schema>";
1163+
1164+
var generator = new Generator
1165+
{
1166+
NamespaceProvider = new NamespaceProvider
1167+
{
1168+
GenerateNamespace = key => "Test"
1169+
}
1170+
};
1171+
var contents = ConvertXml(nameof(OnlyFirstElementOfNestedElementsIsForcedToNullableInChoice), xsd, generator).ToArray();
1172+
var assembly = Compiler.Compile(nameof(OnlyFirstElementOfNestedElementsIsForcedToNullableInChoice), contents);
1173+
1174+
var elementWithChildProperty = assembly.GetType("Test.Root")?.GetProperty("ElementWithChild");
1175+
var nestedChildProperty = assembly.GetType("Test.RootElementWithChild")?.GetProperty("NestedChild");
1176+
Assert.NotNull(elementWithChildProperty);
1177+
Assert.NotNull(nestedChildProperty);
1178+
1179+
Type requiredType = typeof(System.ComponentModel.DataAnnotations.RequiredAttribute);
1180+
bool elementWithChildIsRequired = Attribute.GetCustomAttribute(elementWithChildProperty, requiredType) != null;
1181+
bool nestedChildIsRequired = Attribute.GetCustomAttribute(nestedChildProperty, requiredType) != null;
1182+
Assert.False(elementWithChildIsRequired);
1183+
Assert.True(nestedChildIsRequired);
1184+
}
1185+
11031186
[Fact]
11041187
public void AssemblyVisibleIsInternalClass()
11051188
{

0 commit comments

Comments
 (0)