From 59c49c62335a3a1ad3abb8249416cf9d66b0e1da Mon Sep 17 00:00:00 2001 From: jmatss Date: Tue, 17 May 2022 22:40:53 +0200 Subject: [PATCH 1/2] Fix invalid interface generated for attribute group Fixes some instances when a invalid interface was generated for attribute groups containing a attribute with a `list` type. Correct logic was already implemented in the function used to generate interfaces for "normal" groups. This logic was moved to a property so that it can be used for both attribute groups and "normal" groups. fixes #325 --- XmlSchemaClassGenerator/TypeModel.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index 3bac6eef..5620a7d4 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -781,6 +781,15 @@ private bool IsList } } + private bool IsPrivateSetter + { + get + { + return Configuration.CollectionSettersMode == CollectionSettersMode.Private + && (IsCollection || IsArray || (IsList && IsAttribute)); + } + } + private CodeTypeReference TypeReference { get @@ -825,9 +834,9 @@ public void AddInterfaceMembersTo(CodeTypeDeclaration typeDeclaration) { CodeTypeMember member; - var isArray = IsArray; var propertyType = PropertyType; var isNullableValueType = IsNullableValueType; + var isPrivateSetter = IsPrivateSetter; var typeReference = TypeReference; if (isNullableValueType && Configuration.GenerateNullables) @@ -842,7 +851,7 @@ public void AddInterfaceMembersTo(CodeTypeDeclaration typeDeclaration) Name = Name, Type = typeReference, HasGet = true, - HasSet = !IsCollection && !isArray + HasSet = !isPrivateSetter }; if (DefaultValue != null && IsNullable) @@ -872,6 +881,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi var propertyType = PropertyType; var isNullableValueType = IsNullableValueType; var isNullableReferenceType = IsNullableReferenceType; + var isPrivateSetter = IsPrivateSetter; var typeReference = TypeReference; var requiresBackingField = withDataBinding || DefaultValue != null || IsCollection || isArray; @@ -935,8 +945,6 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi else member = new CodeMemberField(typeReference, propertyName); - var isPrivateSetter = (IsCollection || isArray || (IsList && IsAttribute)) && Configuration.CollectionSettersMode == CollectionSettersMode.Private; - if (requiresBackingField) { member.Name += GetAccessors(member.Name, backingField.Name, From 4fc5714db690c96ee539779703770523ee063f86 Mon Sep 17 00:00:00 2001 From: jmatss Date: Tue, 17 May 2022 22:49:31 +0200 Subject: [PATCH 2/2] Add unit tests for invalid interface generated --- XmlSchemaClassGenerator.Tests/XmlTests.cs | 86 +++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/XmlSchemaClassGenerator.Tests/XmlTests.cs b/XmlSchemaClassGenerator.Tests/XmlTests.cs index 43162982..0216e8d5 100644 --- a/XmlSchemaClassGenerator.Tests/XmlTests.cs +++ b/XmlSchemaClassGenerator.Tests/XmlTests.cs @@ -2578,5 +2578,91 @@ public void TestArrayItemAttribute() var optionList = applicationType.GetProperty("OptionList"); Assert.Equal("Test_Generation_Namespace.T_OptionList", optionList.PropertyType.FullName); } + + [Fact] + public void CollectionSetterInAttributeGroupInterfaceIsPrivateIfCollectionSetterModeIsPrivate() + { + const string xsd = @" + + + + + + + + + + + + + + +"; + + var generator = new Generator + { + NamespaceProvider = new NamespaceProvider + { + GenerateNamespace = key => "Test", + }, + GenerateInterfaces = true, + CollectionSettersMode = CollectionSettersMode.Private + }; + var contents = ConvertXml(nameof(CollectionSetterInAttributeGroupInterfaceIsPrivateIfCollectionSetterModeIsPrivate), xsd, generator).ToArray(); + var assembly = Compiler.Compile(nameof(CollectionSetterInAttributeGroupInterfaceIsPrivateIfCollectionSetterModeIsPrivate), contents); + + var interfaceProperty = assembly.GetType("Test.IAttrGroup")?.GetProperty("Attr"); + var implementerProperty = assembly.GetType("Test.Element")?.GetProperty("Attr"); + Assert.NotNull(interfaceProperty); + Assert.NotNull(implementerProperty); + + var interfaceHasPublicSetter = interfaceProperty.GetSetMethod() != null; + var implementerHasPublicSetter = implementerProperty.GetSetMethod() != null; + Assert.False(interfaceHasPublicSetter); + Assert.False(implementerHasPublicSetter); + } + + [Fact] + public void CollectionSetterInAttributeGroupInterfaceIsPublicIfCollectionSetterModeIsPublic() + { + const string xsd = @" + + + + + + + + + + + + + + +"; + + var generator = new Generator + { + NamespaceProvider = new NamespaceProvider + { + GenerateNamespace = key => "Test", + }, + GenerateInterfaces = true, + CollectionSettersMode = CollectionSettersMode.Public + }; + var contents = ConvertXml(nameof(CollectionSetterInAttributeGroupInterfaceIsPublicIfCollectionSetterModeIsPublic), xsd, generator).ToArray(); + var assembly = Compiler.Compile(nameof(CollectionSetterInAttributeGroupInterfaceIsPublicIfCollectionSetterModeIsPublic), contents); + + var interfaceProperty = assembly.GetType("Test.IAttrGroup")?.GetProperty("Attr"); + var implementerProperty = assembly.GetType("Test.Element")?.GetProperty("Attr"); + Assert.NotNull(interfaceProperty); + Assert.NotNull(implementerProperty); + + var interfaceHasPublicSetter = interfaceProperty.GetSetMethod() != null; + var implementerHasPublicSetter = implementerProperty.GetSetMethod() != null; + Assert.True(interfaceHasPublicSetter); + Assert.True(implementerHasPublicSetter); + } } }