Skip to content

Commit 7fb5352

Browse files
author
Michael Ganss
committed
Promote interface properties to collections (fixes #305)
Create unique property names for elements and attributes with same name
1 parent 5cb6f30 commit 7fb5352

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

XmlSchemaClassGenerator.Tests/XmlTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ public void TestWfs()
482482
{
483483
OutputWriter = output,
484484
EmitOrder = true,
485-
GenerateInterfaces = false
485+
GenerateInterfaces = true
486486
});
487487
TestSamples("wfs", WfsPattern);
488488
}
@@ -992,7 +992,7 @@ public void EditorBrowsableAttributeRespectsCodeTypeReferenceOptions(CodeTypeRef
992992
{
993993
CodeTypeReferenceOptions = codeTypeReferenceOptions,
994994
GenerateNullables = true,
995-
GenerateInterfaces = false,
995+
GenerateInterfaces = true,
996996
NamespaceProvider = new NamespaceProvider
997997
{
998998
GenerateNamespace = key => "Test"

XmlSchemaClassGenerator/CodeUtilities.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,14 @@ public static string GetUniquePropertyName(this TypeModel tm, string name)
298298
var i = 0;
299299
var n = name;
300300
var baseClasses = cls.AllBaseClasses.ToList();
301+
var props = cls.Properties.ToList();
301302

302-
while (baseClasses.SelectMany(b => b.Properties).Any(p => p.Name == n))
303+
while (baseClasses.SelectMany(b => b.Properties)
304+
.Concat(props)
305+
.Any(p => p.Name == n))
306+
{
303307
n = name + (++i);
308+
}
304309

305310
return n;
306311
}

XmlSchemaClassGenerator/ModelBuilder.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public ModelBuilder(GeneratorConfiguration configuration, XmlSchemaSet set)
6969

7070
if (configuration.GenerateInterfaces)
7171
{
72+
PromoteInterfacePropertiesToCollection();
7273
RenameInterfacePropertiesIfRenamedInDerivedClasses();
7374
RemoveDuplicateInterfaceProperties();
7475
}
@@ -184,7 +185,8 @@ private void RenameInterfacePropertiesIfRenamedInDerivedClasses()
184185
{
185186
if (implementationClassProperty.Name != implementationClassProperty.OriginalPropertyName
186187
&& implementationClassProperty.OriginalPropertyName == interfaceProperty.Name
187-
&& implementationClassProperty.XmlSchemaName == interfaceProperty.XmlSchemaName)
188+
&& implementationClassProperty.XmlSchemaName == interfaceProperty.XmlSchemaName
189+
&& implementationClassProperty.IsAttribute == interfaceProperty.IsAttribute)
188190
{
189191
RenameInterfacePropertyInBaseClasses(interfaceModel, implementationClass, interfaceProperty, implementationClassProperty.Name);
190192
interfaceProperty.Name = implementationClassProperty.Name;
@@ -195,6 +197,28 @@ private void RenameInterfacePropertiesIfRenamedInDerivedClasses()
195197
}
196198
}
197199

200+
private void PromoteInterfacePropertiesToCollection()
201+
{
202+
foreach (var interfaceModel in Types.Values.OfType<InterfaceModel>())
203+
{
204+
foreach (var interfaceProperty in interfaceModel.Properties)
205+
{
206+
var derivedProperties = interfaceModel.AllDerivedReferenceTypes().SelectMany(t => t.Properties)
207+
.Where(p => p.Name == interfaceProperty.Name || p.OriginalPropertyName == interfaceProperty.Name).ToList();
208+
209+
if (derivedProperties.Any(p => p.IsCollection))
210+
{
211+
foreach (var derivedProperty in derivedProperties.Where(p => !p.IsCollection))
212+
{
213+
derivedProperty.IsCollection = true;
214+
}
215+
216+
interfaceProperty.IsCollection = true;
217+
}
218+
}
219+
}
220+
}
221+
198222
private static void RenameInterfacePropertyInBaseClasses(InterfaceModel interfaceModel, ReferenceTypeModel implementationClass,
199223
PropertyModel interfaceProperty, string newName)
200224
{
@@ -752,6 +776,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForAttributes(Uri source, Typ
752776
{
753777
var attributeQualifiedName = attribute.AttributeSchemaType.QualifiedName;
754778
var attributeName = _configuration.NamingProvider.AttributeNameFromQualifiedName(attribute.QualifiedName, attribute);
779+
var originalAttributeName = attributeName;
755780

756781
if (attribute.Parent is XmlSchemaAttributeGroup attributeGroup
757782
&& attributeGroup.QualifiedName != typeModel.XmlSchemaName
@@ -795,6 +820,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForAttributes(Uri source, Typ
795820
{
796821
OwningType = typeModel,
797822
Name = attributeName,
823+
OriginalPropertyName = originalAttributeName,
798824
XmlSchemaName = attribute.QualifiedName,
799825
Type = CreateTypeModel(attribute.AttributeSchemaType, attributeQualifiedName),
800826
IsAttribute = true,

0 commit comments

Comments
 (0)