Skip to content

Commit

Permalink
Merge pull request #85 from jokokko/master
Browse files Browse the repository at this point in the history
EditorBrowsableAttribute values should respect CodeTypeReferenceOptio…
  • Loading branch information
mganss authored Oct 4, 2018
2 parents c24c965 + b3c1867 commit 4ee90be
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
42 changes: 41 additions & 1 deletion XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -34,6 +35,7 @@ private IEnumerable<string> ConvertXml(string name, string xsd, Generator genera
EntityFramework = generatorPrototype.EntityFramework,
GenerateInterfaces = generatorPrototype.GenerateInterfaces,
MemberVisitor = generatorPrototype.MemberVisitor,
CodeTypeReferenceOptions = generatorPrototype.CodeTypeReferenceOptions
};

var set = new XmlSchemaSet();
Expand Down Expand Up @@ -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 = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
<xs:complexType name=""document"">
<xs:attribute name=""some-value"">
<xs:simpleType>
<xs:restriction base=""xs:string"">
<xs:enumeration value=""one""/>
<xs:enumeration value=""two""/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name=""system"" type=""xs:string""/>
</xs:complexType>
</xs:schema>";

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 = @"<?xml version=""1.0"" encoding=""UTF-8""?>
Expand Down
4 changes: 2 additions & 2 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
}
Expand Down

0 comments on commit 4ee90be

Please sign in to comment.