From e56a63ac1af16258c064d3c9576f62945b38d3de Mon Sep 17 00:00:00 2001 From: Michael Ganss Date: Fri, 10 Mar 2023 18:05:08 +0100 Subject: [PATCH] Create separate properties for elements with identical name from different namespaces --- XmlSchemaClassGenerator/CodeUtilities.cs | 4 ++-- XmlSchemaClassGenerator/ModelBuilder.cs | 17 +++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/XmlSchemaClassGenerator/CodeUtilities.cs b/XmlSchemaClassGenerator/CodeUtilities.cs index 4719853f..c7c8ecb0 100644 --- a/XmlSchemaClassGenerator/CodeUtilities.cs +++ b/XmlSchemaClassGenerator/CodeUtilities.cs @@ -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 properties) { if (tm is not ClassModel cls) return name; @@ -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; diff --git a/XmlSchemaClassGenerator/ModelBuilder.cs b/XmlSchemaClassGenerator/ModelBuilder.cs index ec59348b..9d5fe194 100644 --- a/XmlSchemaClassGenerator/ModelBuilder.cs +++ b/XmlSchemaClassGenerator/ModelBuilder.cs @@ -814,7 +814,7 @@ private IEnumerable 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: @@ -854,7 +854,7 @@ private IEnumerable CreatePropertiesForAttributes(Uri source, Typ return properties; } - private PropertyModel PropertyFromAttribute(TypeModel owningTypeModel, XmlSchemaAttributeEx attribute) + private PropertyModel PropertyFromAttribute(TypeModel owningTypeModel, XmlSchemaAttributeEx attribute, IList properties) { var attributeQualifiedName = attribute.AttributeSchemaType.QualifiedName; var name = _configuration.NamingProvider.AttributeNameFromQualifiedName(attribute.QualifiedName, attribute); @@ -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) @@ -907,7 +907,7 @@ private PropertyModel PropertyFromAttribute(TypeModel owningTypeModel, XmlSchema } private IEnumerable CreatePropertiesForElements(Uri source, TypeModel owningTypeModel, Particle particle, IEnumerable items, - Substitute substitute = null, int order = 0) + Substitute substitute = null, int order = 0, bool passProperties = true) { var properties = new List(); @@ -919,7 +919,7 @@ private IEnumerable 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()); break; case XmlSchemaAny: SimpleModel typeModel = new(_configuration) @@ -937,7 +937,7 @@ private IEnumerable 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; @@ -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 properties) { PropertyModel property; XmlSchemaElementEx effectiveElement = substitute?.Element ?? element; @@ -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);