diff --git a/XmlSchemaClassGenerator.Tests/XmlTests.cs b/XmlSchemaClassGenerator.Tests/XmlTests.cs index 11cffed3..1b3e70f0 100644 --- a/XmlSchemaClassGenerator.Tests/XmlTests.cs +++ b/XmlSchemaClassGenerator.Tests/XmlTests.cs @@ -29,7 +29,7 @@ public XmlTests(ITestOutputHelper output) Output = output; } - private IEnumerable ConvertXml(string name, string xsd, Generator generatorPrototype = null) + private IEnumerable ConvertXml(string name, IEnumerable xsds, Generator generatorPrototype = null) { if (name is null) { @@ -57,8 +57,9 @@ private IEnumerable 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); @@ -72,6 +73,11 @@ private IEnumerable ConvertXml(string name, string xsd, Generator genera return writer.Content; } + private IEnumerable 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"; @@ -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 = @" + + + + + + + + + + + + + + + + + + + + + + + + + + + "; + const string xsd2 = @" + + + + + + + + + + + + + + + + + + + + + + + + + + + "; + 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().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().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().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().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().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().FirstOrDefault(); + Assert.NotNull(xmlRootAttribute); + Assert.Equal("TestType2Property", xmlRootAttribute.ElementName); + Assert.Equal("Test_NS2", xmlRootAttribute.Namespace); + } } } diff --git a/XmlSchemaClassGenerator/CodeUtilities.cs b/XmlSchemaClassGenerator/CodeUtilities.cs index 8b48070b..8f1ceda2 100644 --- a/XmlSchemaClassGenerator/CodeUtilities.cs +++ b/XmlSchemaClassGenerator/CodeUtilities.cs @@ -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 {