From b3c1867e7e63b10abfedf9156c05dbb4b8c3e40e Mon Sep 17 00:00:00 2001 From: jokokko Date: Thu, 4 Oct 2018 16:17:21 +0300 Subject: [PATCH] EditorBrowsableAttribute values should respect CodeTypeReferenceOptions to mitigate/avoid collisions. --- XmlSchemaClassGenerator.Tests/XmlTests.cs | 42 ++++++++++++++++++++++- XmlSchemaClassGenerator/TypeModel.cs | 4 +-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/XmlSchemaClassGenerator.Tests/XmlTests.cs b/XmlSchemaClassGenerator.Tests/XmlTests.cs index 3278447e..216ff11d 100644 --- a/XmlSchemaClassGenerator.Tests/XmlTests.cs +++ b/XmlSchemaClassGenerator.Tests/XmlTests.cs @@ -2,6 +2,7 @@ using Microsoft.CodeAnalysis; using Microsoft.Xml.XMLGen; using System; +using System.CodeDom; using System.Collections.Generic; using System.IO; using System.Linq; @@ -34,6 +35,7 @@ private IEnumerable ConvertXml(string name, string xsd, Generator genera EntityFramework = generatorPrototype.EntityFramework, GenerateInterfaces = generatorPrototype.GenerateInterfaces, MemberVisitor = generatorPrototype.MemberVisitor, + CodeTypeReferenceOptions = generatorPrototype.CodeTypeReferenceOptions }; var set = new XmlSchemaSet(); @@ -338,7 +340,45 @@ public void DontGenerateElementForEmptyCollectionInChoice() Assert.DoesNotContain("tags", xml, StringComparison.OrdinalIgnoreCase); } - [Fact] + + [Theory] + [InlineData(CodeTypeReferenceOptions.GlobalReference, "[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]")] + [InlineData((CodeTypeReferenceOptions)0, "[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]")] + public void EditorBrowsableAttributeRespectsCodeTypeReferenceOptions(CodeTypeReferenceOptions codeTypeReferenceOptions, string expectedLine) + { + const string xsd = @" + + + + + + + + + + + + +"; + + var generatedType = ConvertXml(nameof(EditorBrowsableAttributeRespectsCodeTypeReferenceOptions), xsd, new Generator + { + CodeTypeReferenceOptions = codeTypeReferenceOptions, + GenerateNullables = true, + GenerateInterfaces = false, + NamespaceProvider = new NamespaceProvider + { + GenerateNamespace = key => "Test" + } + }); + + Assert.Contains( + expectedLine, + generatedType.First()); + } + + + [Fact] public void ComplexTypeWithAttributeGroupExtension() { const string xsd = @" diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index 67b3526b..a71fa388 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -850,8 +850,8 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi typeDeclaration.Members.Add(nullableMember); var editorBrowsableAttribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(EditorBrowsableAttribute), Configuration.CodeTypeReferenceOptions)); - editorBrowsableAttribute.Arguments.Add(new CodeAttributeArgument(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(EditorBrowsableState)), "Never"))); - specifiedMember.CustomAttributes.Add(editorBrowsableAttribute); + editorBrowsableAttribute.Arguments.Add(new CodeAttributeArgument(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(new CodeTypeReference(typeof(EditorBrowsableState), Configuration.CodeTypeReferenceOptions)), "Never"))); + specifiedMember.CustomAttributes.Add(editorBrowsableAttribute); member.CustomAttributes.Add(editorBrowsableAttribute); if (Configuration.EntityFramework) { member.CustomAttributes.Add(notMappedAttribute); } }