Skip to content

Commit

Permalink
Merge branch 'DuplicatePropertyNames'
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ganss committed Mar 16, 2023
2 parents bd858b6 + e56a63a commit 2d9854b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions XmlSchemaClassGenerator/CodeUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public static string GetUniqueFieldName(this TypeModel typeModel, PropertyModel
return i <= 1 ? propBackingFieldName : $"{propBackingFieldName}{i}";
}

public static string GetUniquePropertyName(this TypeModel tm, string name)
public static string GetUniquePropertyName(this TypeModel tm, string name, IList<PropertyModel> properties)
{
if (tm is not ClassModel cls) return name;

Expand All @@ -205,7 +205,7 @@ public static string GetUniquePropertyName(this TypeModel tm, string name)
var baseProps = cls.AllBaseClasses.SelectMany(b => b.Properties).ToList();
var props = cls.Properties.ToList();

while (baseProps.Concat(props).Any(p => p.Name == n))
while (baseProps.Concat(props).Concat(properties).Any(p => p.Name == n))
n = name + (++i);

return n;
Expand Down
17 changes: 9 additions & 8 deletions XmlSchemaClassGenerator/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForAttributes(Uri source, Typ
{
case XmlSchemaAttribute attribute when attribute.Use != XmlSchemaUse.Prohibited:

properties.Add(PropertyFromAttribute(owningTypeModel, attribute));
properties.Add(PropertyFromAttribute(owningTypeModel, attribute, properties));
break;

case XmlSchemaAttributeGroupRef attributeGroupRef:
Expand Down Expand Up @@ -854,7 +854,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForAttributes(Uri source, Typ
return properties;
}

private PropertyModel PropertyFromAttribute(TypeModel owningTypeModel, XmlSchemaAttributeEx attribute)
private PropertyModel PropertyFromAttribute(TypeModel owningTypeModel, XmlSchemaAttributeEx attribute, IList<PropertyModel> properties)
{
var attributeQualifiedName = attribute.AttributeSchemaType.QualifiedName;
var name = _configuration.NamingProvider.AttributeNameFromQualifiedName(attribute.QualifiedName, attribute);
Expand Down Expand Up @@ -890,7 +890,7 @@ private PropertyModel PropertyFromAttribute(TypeModel owningTypeModel, XmlSchema
name += "Property";
}

name = owningTypeModel.GetUniquePropertyName(name);
name = owningTypeModel.GetUniquePropertyName(name, properties);

var typeModel = CreateTypeModel(attributeQualifiedName, attribute.AttributeSchemaType);
var property = new PropertyModel(_configuration, name, typeModel, owningTypeModel)
Expand All @@ -907,7 +907,7 @@ private PropertyModel PropertyFromAttribute(TypeModel owningTypeModel, XmlSchema
}

private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeModel owningTypeModel, Particle particle, IEnumerable<Particle> items,
Substitute substitute = null, int order = 0)
Substitute substitute = null, int order = 0, bool passProperties = true)
{
var properties = new List<PropertyModel>();

Expand All @@ -919,7 +919,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
{
// ElementSchemaType must be non-null. This is not the case when maxOccurs="0".
case XmlSchemaElement element when element.ElementSchemaType != null:
property = PropertyFromElement(owningTypeModel, element, particle, item, substitute);
property = PropertyFromElement(owningTypeModel, element, particle, item, substitute, passProperties ? properties : new List<PropertyModel>());
break;
case XmlSchemaAny:
SimpleModel typeModel = new(_configuration)
Expand All @@ -937,7 +937,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
CreateTypeModel(groupRef.RefName, group.First());

var groupItems = GetElements(groupRef.Particle).ToList();
var groupProperties = CreatePropertiesForElements(source, owningTypeModel, item, groupItems, order: order).ToList();
var groupProperties = CreatePropertiesForElements(source, owningTypeModel, item, groupItems, order: order, passProperties: false).ToList();
if (_configuration.EmitOrder)
order += groupProperties.Count;

Expand Down Expand Up @@ -986,7 +986,8 @@ private static bool IsNullableByChoice(XmlSchemaObject parent)
return false;
}

private PropertyModel PropertyFromElement(TypeModel owningTypeModel, XmlSchemaElementEx element, Particle particle, Particle item, Substitute substitute)
private PropertyModel PropertyFromElement(TypeModel owningTypeModel, XmlSchemaElementEx element, Particle particle, Particle item, Substitute substitute,
IList<PropertyModel> properties)
{
PropertyModel property;
XmlSchemaElementEx effectiveElement = substitute?.Element ?? element;
Expand All @@ -995,7 +996,7 @@ private PropertyModel PropertyFromElement(TypeModel owningTypeModel, XmlSchemaEl
if (name == owningTypeModel.Name)
name += "Property"; // member names cannot be the same as their enclosing type

name = owningTypeModel.GetUniquePropertyName(name);
name = owningTypeModel.GetUniquePropertyName(name, properties);

var typeModel = substitute?.Type ?? CreateTypeModel(GetQualifiedName(owningTypeModel, particle.XmlParticle, element), element.ElementSchemaType);

Expand Down

0 comments on commit 2d9854b

Please sign in to comment.