Skip to content

Commit f18798b

Browse files
authored
Merge pull request #326 from jmatss/fix-attribute-group-interface
Fix invalid interface generated for attribute group containing attribute with list type
2 parents f5c2837 + 4fc5714 commit f18798b

File tree

2 files changed

+98
-4
lines changed

2 files changed

+98
-4
lines changed

XmlSchemaClassGenerator.Tests/XmlTests.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2578,5 +2578,91 @@ public void TestArrayItemAttribute()
25782578
var optionList = applicationType.GetProperty("OptionList");
25792579
Assert.Equal("Test_Generation_Namespace.T_OptionList", optionList.PropertyType.FullName);
25802580
}
2581+
2582+
[Fact]
2583+
public void CollectionSetterInAttributeGroupInterfaceIsPrivateIfCollectionSetterModeIsPrivate()
2584+
{
2585+
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
2586+
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
2587+
<xs:element name=""Element"">
2588+
<xs:complexType>
2589+
<xs:attributeGroup ref=""AttrGroup""/>
2590+
</xs:complexType>
2591+
</xs:element>
2592+
2593+
<xs:attributeGroup name=""AttrGroup"">
2594+
<xs:attribute name=""Attr"">
2595+
<xs:simpleType>
2596+
<xs:list itemType=""xs:int""/>
2597+
</xs:simpleType>
2598+
</xs:attribute>
2599+
</xs:attributeGroup>
2600+
</xs:schema>";
2601+
2602+
var generator = new Generator
2603+
{
2604+
NamespaceProvider = new NamespaceProvider
2605+
{
2606+
GenerateNamespace = key => "Test",
2607+
},
2608+
GenerateInterfaces = true,
2609+
CollectionSettersMode = CollectionSettersMode.Private
2610+
};
2611+
var contents = ConvertXml(nameof(CollectionSetterInAttributeGroupInterfaceIsPrivateIfCollectionSetterModeIsPrivate), xsd, generator).ToArray();
2612+
var assembly = Compiler.Compile(nameof(CollectionSetterInAttributeGroupInterfaceIsPrivateIfCollectionSetterModeIsPrivate), contents);
2613+
2614+
var interfaceProperty = assembly.GetType("Test.IAttrGroup")?.GetProperty("Attr");
2615+
var implementerProperty = assembly.GetType("Test.Element")?.GetProperty("Attr");
2616+
Assert.NotNull(interfaceProperty);
2617+
Assert.NotNull(implementerProperty);
2618+
2619+
var interfaceHasPublicSetter = interfaceProperty.GetSetMethod() != null;
2620+
var implementerHasPublicSetter = implementerProperty.GetSetMethod() != null;
2621+
Assert.False(interfaceHasPublicSetter);
2622+
Assert.False(implementerHasPublicSetter);
2623+
}
2624+
2625+
[Fact]
2626+
public void CollectionSetterInAttributeGroupInterfaceIsPublicIfCollectionSetterModeIsPublic()
2627+
{
2628+
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
2629+
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
2630+
<xs:element name=""Element"">
2631+
<xs:complexType>
2632+
<xs:attributeGroup ref=""AttrGroup""/>
2633+
</xs:complexType>
2634+
</xs:element>
2635+
2636+
<xs:attributeGroup name=""AttrGroup"">
2637+
<xs:attribute name=""Attr"">
2638+
<xs:simpleType>
2639+
<xs:list itemType=""xs:int""/>
2640+
</xs:simpleType>
2641+
</xs:attribute>
2642+
</xs:attributeGroup>
2643+
</xs:schema>";
2644+
2645+
var generator = new Generator
2646+
{
2647+
NamespaceProvider = new NamespaceProvider
2648+
{
2649+
GenerateNamespace = key => "Test",
2650+
},
2651+
GenerateInterfaces = true,
2652+
CollectionSettersMode = CollectionSettersMode.Public
2653+
};
2654+
var contents = ConvertXml(nameof(CollectionSetterInAttributeGroupInterfaceIsPublicIfCollectionSetterModeIsPublic), xsd, generator).ToArray();
2655+
var assembly = Compiler.Compile(nameof(CollectionSetterInAttributeGroupInterfaceIsPublicIfCollectionSetterModeIsPublic), contents);
2656+
2657+
var interfaceProperty = assembly.GetType("Test.IAttrGroup")?.GetProperty("Attr");
2658+
var implementerProperty = assembly.GetType("Test.Element")?.GetProperty("Attr");
2659+
Assert.NotNull(interfaceProperty);
2660+
Assert.NotNull(implementerProperty);
2661+
2662+
var interfaceHasPublicSetter = interfaceProperty.GetSetMethod() != null;
2663+
var implementerHasPublicSetter = implementerProperty.GetSetMethod() != null;
2664+
Assert.True(interfaceHasPublicSetter);
2665+
Assert.True(implementerHasPublicSetter);
2666+
}
25812667
}
25822668
}

XmlSchemaClassGenerator/TypeModel.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,15 @@ private bool IsList
781781
}
782782
}
783783

784+
private bool IsPrivateSetter
785+
{
786+
get
787+
{
788+
return Configuration.CollectionSettersMode == CollectionSettersMode.Private
789+
&& (IsCollection || IsArray || (IsList && IsAttribute));
790+
}
791+
}
792+
784793
private CodeTypeReference TypeReference
785794
{
786795
get
@@ -825,9 +834,9 @@ public void AddInterfaceMembersTo(CodeTypeDeclaration typeDeclaration)
825834
{
826835
CodeTypeMember member;
827836

828-
var isArray = IsArray;
829837
var propertyType = PropertyType;
830838
var isNullableValueType = IsNullableValueType;
839+
var isPrivateSetter = IsPrivateSetter;
831840
var typeReference = TypeReference;
832841

833842
if (isNullableValueType && Configuration.GenerateNullables)
@@ -842,7 +851,7 @@ public void AddInterfaceMembersTo(CodeTypeDeclaration typeDeclaration)
842851
Name = Name,
843852
Type = typeReference,
844853
HasGet = true,
845-
HasSet = !IsCollection && !isArray
854+
HasSet = !isPrivateSetter
846855
};
847856

848857
if (DefaultValue != null && IsNullable)
@@ -872,6 +881,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
872881
var propertyType = PropertyType;
873882
var isNullableValueType = IsNullableValueType;
874883
var isNullableReferenceType = IsNullableReferenceType;
884+
var isPrivateSetter = IsPrivateSetter;
875885
var typeReference = TypeReference;
876886

877887
var requiresBackingField = withDataBinding || DefaultValue != null || IsCollection || isArray;
@@ -935,8 +945,6 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
935945
else
936946
member = new CodeMemberField(typeReference, propertyName);
937947

938-
var isPrivateSetter = (IsCollection || isArray || (IsList && IsAttribute)) && Configuration.CollectionSettersMode == CollectionSettersMode.Private;
939-
940948
if (requiresBackingField)
941949
{
942950
member.Name += GetAccessors(member.Name, backingField.Name,

0 commit comments

Comments
 (0)