Skip to content

Commit

Permalink
Promote interface properties to collections (fixes #305)
Browse files Browse the repository at this point in the history
Create unique property names for elements and attributes with same name
  • Loading branch information
Michael Ganss committed Feb 7, 2022
1 parent 5cb6f30 commit 7fb5352
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
4 changes: 2 additions & 2 deletions XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ public void TestWfs()
{
OutputWriter = output,
EmitOrder = true,
GenerateInterfaces = false
GenerateInterfaces = true
});
TestSamples("wfs", WfsPattern);
}
Expand Down Expand Up @@ -992,7 +992,7 @@ public void EditorBrowsableAttributeRespectsCodeTypeReferenceOptions(CodeTypeRef
{
CodeTypeReferenceOptions = codeTypeReferenceOptions,
GenerateNullables = true,
GenerateInterfaces = false,
GenerateInterfaces = true,
NamespaceProvider = new NamespaceProvider
{
GenerateNamespace = key => "Test"
Expand Down
7 changes: 6 additions & 1 deletion XmlSchemaClassGenerator/CodeUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,14 @@ public static string GetUniquePropertyName(this TypeModel tm, string name)
var i = 0;
var n = name;
var baseClasses = cls.AllBaseClasses.ToList();
var props = cls.Properties.ToList();

while (baseClasses.SelectMany(b => b.Properties).Any(p => p.Name == n))
while (baseClasses.SelectMany(b => b.Properties)
.Concat(props)
.Any(p => p.Name == n))
{
n = name + (++i);
}

return n;
}
Expand Down
28 changes: 27 additions & 1 deletion XmlSchemaClassGenerator/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public ModelBuilder(GeneratorConfiguration configuration, XmlSchemaSet set)

if (configuration.GenerateInterfaces)
{
PromoteInterfacePropertiesToCollection();
RenameInterfacePropertiesIfRenamedInDerivedClasses();
RemoveDuplicateInterfaceProperties();
}
Expand Down Expand Up @@ -184,7 +185,8 @@ private void RenameInterfacePropertiesIfRenamedInDerivedClasses()
{
if (implementationClassProperty.Name != implementationClassProperty.OriginalPropertyName
&& implementationClassProperty.OriginalPropertyName == interfaceProperty.Name
&& implementationClassProperty.XmlSchemaName == interfaceProperty.XmlSchemaName)
&& implementationClassProperty.XmlSchemaName == interfaceProperty.XmlSchemaName
&& implementationClassProperty.IsAttribute == interfaceProperty.IsAttribute)
{
RenameInterfacePropertyInBaseClasses(interfaceModel, implementationClass, interfaceProperty, implementationClassProperty.Name);
interfaceProperty.Name = implementationClassProperty.Name;
Expand All @@ -195,6 +197,28 @@ private void RenameInterfacePropertiesIfRenamedInDerivedClasses()
}
}

private void PromoteInterfacePropertiesToCollection()
{
foreach (var interfaceModel in Types.Values.OfType<InterfaceModel>())
{
foreach (var interfaceProperty in interfaceModel.Properties)
{
var derivedProperties = interfaceModel.AllDerivedReferenceTypes().SelectMany(t => t.Properties)
.Where(p => p.Name == interfaceProperty.Name || p.OriginalPropertyName == interfaceProperty.Name).ToList();

if (derivedProperties.Any(p => p.IsCollection))
{
foreach (var derivedProperty in derivedProperties.Where(p => !p.IsCollection))
{
derivedProperty.IsCollection = true;
}

interfaceProperty.IsCollection = true;
}
}
}
}

private static void RenameInterfacePropertyInBaseClasses(InterfaceModel interfaceModel, ReferenceTypeModel implementationClass,
PropertyModel interfaceProperty, string newName)
{
Expand Down Expand Up @@ -752,6 +776,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForAttributes(Uri source, Typ
{
var attributeQualifiedName = attribute.AttributeSchemaType.QualifiedName;
var attributeName = _configuration.NamingProvider.AttributeNameFromQualifiedName(attribute.QualifiedName, attribute);
var originalAttributeName = attributeName;

if (attribute.Parent is XmlSchemaAttributeGroup attributeGroup
&& attributeGroup.QualifiedName != typeModel.XmlSchemaName
Expand Down Expand Up @@ -795,6 +820,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForAttributes(Uri source, Typ
{
OwningType = typeModel,
Name = attributeName,
OriginalPropertyName = originalAttributeName,
XmlSchemaName = attribute.QualifiedName,
Type = CreateTypeModel(attribute.AttributeSchemaType, attributeQualifiedName),
IsAttribute = true,
Expand Down

0 comments on commit 7fb5352

Please sign in to comment.