Skip to content

Commit

Permalink
Fix #50, #52
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ganss committed Apr 23, 2020
1 parent 7b3bdc9 commit acb5719
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ public void CollidingElementAndComplexTypeNamesCanBeResolved()
var generatedType = ConvertXml(nameof(CollidingElementAndComplexTypeNamesCanBeResolved), xsd, generator).First();

Assert.Contains(@"public partial class MyType", generatedType);
Assert.Contains(@"public enum MyType", generatedType);
Assert.Contains(@"public enum MyType2", generatedType);
}

[Fact]
Expand Down
26 changes: 16 additions & 10 deletions XmlSchemaClassGenerator/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ internal class ModelBuilder

private static readonly XmlQualifiedName AnyType = new XmlQualifiedName("anyType", XmlSchema.Namespace);

private string BuildKey(XmlSchemaAnnotated annotated, XmlQualifiedName name)
=> $"{annotated.GetType()}:{annotated.SourceUri}:{annotated.LineNumber}:{annotated.LinePosition}:{name}";

public ModelBuilder(GeneratorConfiguration configuration, XmlSchemaSet set)
{
_configuration = configuration;
Expand All @@ -35,7 +38,7 @@ public ModelBuilder(GeneratorConfiguration configuration, XmlSchemaSet set)
UseDataTypeAttribute = false
};

var key = typeof(XmlSchemaComplexType) + "#" + AnyType;
var key = BuildKey(new XmlSchemaComplexType(), AnyType);
Types[key] = objectModel;

AttributeGroups = set.Schemas().Cast<XmlSchema>().SelectMany(s => s.AttributeGroups.Values.Cast<XmlSchemaAttributeGroup>())
Expand Down Expand Up @@ -124,7 +127,7 @@ private void CreateElements(IEnumerable<XmlSchemaElement> elements)
derivedClassModel.Namespace.Types[derivedClassModel.Name] = derivedClassModel;
}

var key = rootElement.GetType() + "#" + rootElement.QualifiedName;
var key = BuildKey(rootElement, rootElement.QualifiedName);
Types[key] = derivedClassModel;

derivedClassModel.BaseClass = classModel;
Expand All @@ -134,7 +137,7 @@ private void CreateElements(IEnumerable<XmlSchemaElement> elements)
}
else
{
var key = rootElement.GetType() + "#" + rootElement.QualifiedName;
var key = BuildKey(rootElement, rootElement.QualifiedName);
Types[key] = type;
}
}
Expand All @@ -157,7 +160,7 @@ private void CreateElements(IEnumerable<XmlSchemaElement> elements)

private TypeModel CreateTypeModel(Uri source, XmlSchemaAnnotated type, XmlQualifiedName qualifiedName)
{
var key = type.GetType() + "#" + qualifiedName;
var key = BuildKey(type, qualifiedName);
if (!qualifiedName.IsEmpty && Types.TryGetValue(key, out TypeModel typeModel))
{
return typeModel;
Expand Down Expand Up @@ -204,7 +207,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaGroup group, NamespaceMod

if (!qualifiedName.IsEmpty)
{
var key = group.GetType() + "#" + qualifiedName;
var key = BuildKey(group, qualifiedName);
Types[key] = interfaceModel;
}

Expand Down Expand Up @@ -237,7 +240,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaAttributeGroup attributeG

if (!qualifiedName.IsEmpty)
{
var key = attributeGroup.GetType() + "#" + qualifiedName;
var key = BuildKey(attributeGroup, qualifiedName);
Types[key] = interfaceModel;
}

Expand Down Expand Up @@ -280,7 +283,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaComplexType complexType,

if (!qualifiedName.IsEmpty)
{
var key = complexType.GetType() + "#" + qualifiedName;
var key = BuildKey(complexType, qualifiedName);
Types[key] = classModel;
}

Expand Down Expand Up @@ -417,7 +420,7 @@ private TypeModel CreateTypeModel(XmlSchemaSimpleType simpleType, NamespaceModel

if (!qualifiedName.IsEmpty)
{
var key = simpleType.GetType() + "#" + qualifiedName;
var key = BuildKey(simpleType, qualifiedName);
Types[key] = enumModel;
}

Expand Down Expand Up @@ -449,7 +452,7 @@ private TypeModel CreateTypeModel(XmlSchemaSimpleType simpleType, NamespaceModel

if (!qualifiedName.IsEmpty)
{
var key = simpleType.GetType() + "#" + qualifiedName;
var key = BuildKey(simpleType, qualifiedName);
Types[key] = simpleModel;
}

Expand All @@ -469,7 +472,10 @@ private IEnumerable<PropertyModel> CreatePropertiesForAttributes(Uri source, Typ
var attributeQualifiedName = attribute.AttributeSchemaType.QualifiedName;
var attributeName = _configuration.NamingProvider.AttributeNameFromQualifiedName(attribute.QualifiedName);

if (attribute.Parent is XmlSchemaAttributeGroup attributeGroup && attributeGroup.QualifiedName != typeModel.XmlSchemaName && Types.TryGetValue(attributeGroup.GetType() + "#" + attributeGroup.QualifiedName, out var typeModelValue) && typeModelValue is InterfaceModel interfaceTypeModel)
if (attribute.Parent is XmlSchemaAttributeGroup attributeGroup
&& attributeGroup.QualifiedName != typeModel.XmlSchemaName
&& Types.TryGetValue(BuildKey(attributeGroup, attributeGroup.QualifiedName), out var typeModelValue)
&& typeModelValue is InterfaceModel interfaceTypeModel)
{
var interfaceProperty = interfaceTypeModel.Properties.Single(p => p.XmlSchemaName == attribute.QualifiedName);
attributeQualifiedName = interfaceProperty.Type.XmlSchemaName;
Expand Down

0 comments on commit acb5719

Please sign in to comment.