Skip to content

Commit

Permalink
Merge pull request #196 from AVTit/master
Browse files Browse the repository at this point in the history
Fix CodeUtilities.QualifiedName for anonymous types
  • Loading branch information
mganss authored May 4, 2020
2 parents 4918caa + 2e5e522 commit 63f20de
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 3 deletions.
123 changes: 121 additions & 2 deletions XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public XmlTests(ITestOutputHelper output)
Output = output;
}

private IEnumerable<string> ConvertXml(string name, string xsd, Generator generatorPrototype = null)
private IEnumerable<string> ConvertXml(string name, IEnumerable<string> xsds, Generator generatorPrototype = null)
{
if (name is null)
{
Expand Down Expand Up @@ -57,8 +57,9 @@ private IEnumerable<string> ConvertXml(string name, string xsd, Generator genera

var set = new XmlSchemaSet();

using (var stringReader = new StringReader(xsd))
foreach (var xsd in xsds)
{
using var stringReader = new StringReader(xsd);
var schema = XmlSchema.Read(stringReader, (s, e) =>
{
throw new InvalidOperationException($"{e.Severity}: {e.Message}",e.Exception);
Expand All @@ -72,6 +73,11 @@ private IEnumerable<string> ConvertXml(string name, string xsd, Generator genera
return writer.Content;
}

private IEnumerable<string> ConvertXml(string name, string xsd, Generator generatorPrototype = null)
{
return ConvertXml(name, new[] {xsd}, generatorPrototype);
}

const string IS24Pattern = @"xsd\is24\*\*.xsd";
const string IS24ImmoTransferPattern = @"xsd\is24immotransfer\is24immotransfer.xsd";
const string WadlPattern = @"xsd\wadl\*.xsd";
Expand Down Expand Up @@ -1723,5 +1729,118 @@ public void AmbiguousTypesTest()
Assert.Equal("EnumTestType", xmlRootAttribute.ElementName);
Assert.Equal("Test_NS2", xmlRootAttribute.Namespace);
}

[Fact]
public void AmbiguousAnonymousTypesTest()
{
const string xsd1 = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"" targetNamespace=""Test_NS1""
elementFormDefault=""qualified"" attributeFormDefault=""unqualified"">
<xs:complexType name=""TestType"">
<xs:sequence>
<xs:element name=""Property"">
<xs:simpleType>
<xs:restriction base=""xs:string"">
<xs:enumeration value=""EnumValue""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name=""TestType2"">
<xs:sequence>
<xs:element name=""Property"">
<xs:complexType>
<xs:sequence>
<xs:element name=""Property"" type=""xs:string""/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>";
const string xsd2 = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"" targetNamespace=""Test_NS2""
elementFormDefault=""qualified"" attributeFormDefault=""unqualified"">
<xs:complexType name=""TestType"">
<xs:sequence>
<xs:element name=""Property"">
<xs:simpleType>
<xs:restriction base=""xs:string"">
<xs:enumeration value=""EnumValue""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name=""TestType2"">
<xs:sequence>
<xs:element name=""Property"">
<xs:complexType>
<xs:sequence>
<xs:element name=""Property"" type=""xs:string""/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>";
var generator = new Generator
{
NamespaceProvider = new NamespaceProvider
{
GenerateNamespace = key =>key.XmlSchemaNamespace
}
, };
var contents = ConvertXml(nameof(GenerateXmlRootAttributeForEnumTest), new[]{xsd1, xsd2}, generator).ToArray();
Assert.Equal(2, contents.Length);

var assembly = Compiler.Compile(nameof(GenerateXmlRootAttributeForEnumTest), contents);

var testType = assembly.GetType("Test_NS1.TestType");
Assert.NotNull(testType);
var xmlRootAttribute = testType.GetCustomAttributes<XmlRootAttribute>().FirstOrDefault();
Assert.NotNull(xmlRootAttribute);
Assert.Equal("TestType", xmlRootAttribute.ElementName);
Assert.Equal("Test_NS1", xmlRootAttribute.Namespace);
testType = assembly.GetType("Test_NS1.TestTypeProperty");
Assert.NotNull(testType);
xmlRootAttribute = testType.GetCustomAttributes<XmlRootAttribute>().FirstOrDefault();
Assert.NotNull(xmlRootAttribute);
Assert.Equal("TestTypeProperty", xmlRootAttribute.ElementName);
Assert.Equal("Test_NS1", xmlRootAttribute.Namespace);
testType = assembly.GetType("Test_NS1.TestType2Property");
Assert.NotNull(testType);
xmlRootAttribute = testType.GetCustomAttributes<XmlRootAttribute>().FirstOrDefault();
Assert.NotNull(xmlRootAttribute);
Assert.Equal("TestType2Property", xmlRootAttribute.ElementName);
Assert.Equal("Test_NS1", xmlRootAttribute.Namespace);


testType = assembly.GetType("Test_NS2.TestType");
Assert.NotNull(testType);
xmlRootAttribute = testType.GetCustomAttributes<XmlRootAttribute>().FirstOrDefault();
Assert.NotNull(xmlRootAttribute);
Assert.Equal("TestType", xmlRootAttribute.ElementName);
Assert.Equal("Test_NS2", xmlRootAttribute.Namespace);
testType = assembly.GetType("Test_NS2.TestTypeProperty");
Assert.NotNull(testType);
xmlRootAttribute = testType.GetCustomAttributes<XmlRootAttribute>().FirstOrDefault();
Assert.NotNull(xmlRootAttribute);
Assert.Equal("TestTypeProperty", xmlRootAttribute.ElementName);
Assert.Equal("Test_NS2", xmlRootAttribute.Namespace);
testType = assembly.GetType("Test_NS2.TestType2Property");
Assert.NotNull(testType);
xmlRootAttribute = testType.GetCustomAttributes<XmlRootAttribute>().FirstOrDefault();
Assert.NotNull(xmlRootAttribute);
Assert.Equal("TestType2Property", xmlRootAttribute.ElementName);
Assert.Equal("Test_NS2", xmlRootAttribute.Namespace);
}
}
}
9 changes: 8 additions & 1 deletion XmlSchemaClassGenerator/CodeUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,14 @@ public static XmlQualifiedName GetQualifiedName(this TypeModel typeModel)
XmlQualifiedName qualifiedName;
if (!(typeModel is SimpleModel simpleTypeModel))
{
qualifiedName = typeModel.XmlSchemaType.GetQualifiedName();
if (typeModel.IsAnonymous)
{
qualifiedName = typeModel.XmlSchemaName;
}
else
{
qualifiedName = typeModel.XmlSchemaType.GetQualifiedName();
}
}
else
{
Expand Down

0 comments on commit 63f20de

Please sign in to comment.