Skip to content

Commit

Permalink
Fix sonarcloud issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mganss committed Apr 17, 2024
1 parent 7294c72 commit 4ce4394
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
6 changes: 5 additions & 1 deletion XmlSchemaClassGenerator/CodeUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,13 @@ public static XmlQualifiedName GetQualifiedName(this TypeModel typeModel)
public static string GetUniqueTypeName(this NamespaceModel model, string name)
{
var n = name;
var i = 2;

for (var i = 2; model.Types.ContainsKey(n) && model.Types[n] is not SimpleModel; i++)
while (model.Types.ContainsKey(n) && model.Types[n] is not SimpleModel)
{
n = name + i;
i++;
}

return n;
}
Expand Down
11 changes: 7 additions & 4 deletions XmlSchemaClassGenerator/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ namespace XmlSchemaClassGenerator
{
internal class ModelBuilder
{
private const string ItemName = "Item";
private const string PropertyName = "Property";
private const string ElementName = "Element";
private readonly GeneratorConfiguration _configuration;
private readonly XmlSchemaSet _set;
private readonly Dictionary<XmlQualifiedName, HashSet<XmlSchemaAttributeGroup>> AttributeGroups = new();
Expand Down Expand Up @@ -868,12 +871,12 @@ private PropertyModel PropertyFromAttribute(TypeModel owningTypeModel, XmlSchema
attributeQualifiedName = new XmlQualifiedName(typeName, owningTypeModel.XmlSchemaName.Namespace);
// try to avoid name clashes
if (NameExists(attributeQualifiedName))
attributeQualifiedName = new[] { "Item", "Property", "Element" }.Select(s => new XmlQualifiedName(attributeQualifiedName.Name + s, attributeQualifiedName.Namespace)).First(n => !NameExists(n));
attributeQualifiedName = new[] { ItemName, PropertyName, ElementName }.Select(s => new XmlQualifiedName(attributeQualifiedName.Name + s, attributeQualifiedName.Namespace)).First(n => !NameExists(n));
}
}

if (name == owningTypeModel.Name)
name += "Property";
name += PropertyName;
}

name = owningTypeModel.GetUniquePropertyName(name, properties);
Expand Down Expand Up @@ -989,7 +992,7 @@ private PropertyModel PropertyFromElement(TypeModel owningTypeModel, XmlSchemaEl
var name = _configuration.NamingProvider.ElementNameFromQualifiedName(effectiveElement.QualifiedName, effectiveElement);
var originalName = name;
if (name == owningTypeModel.Name)
name += "Property"; // member names cannot be the same as their enclosing type
name += PropertyName; // member names cannot be the same as their enclosing type

name = owningTypeModel.GetUniquePropertyName(name, properties);

Expand Down Expand Up @@ -1023,7 +1026,7 @@ private XmlQualifiedName GetQualifiedName(TypeModel typeModel, XmlSchemaParticle
elementQualifiedName = new XmlQualifiedName(typeName, typeModel.XmlSchemaName.Namespace);
// try to avoid name clashes
if (NameExists(elementQualifiedName))
elementQualifiedName = new[] { "Item", "Property", "Element" }.Select(s => new XmlQualifiedName(elementQualifiedName.Name + s, elementQualifiedName.Namespace)).First(n => !NameExists(n));
elementQualifiedName = new[] { ItemName, PropertyName, ElementName }.Select(s => new XmlQualifiedName(elementQualifiedName.Name + s, elementQualifiedName.Namespace)).First(n => !NameExists(n));
}
}

Expand Down
4 changes: 3 additions & 1 deletion XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -385,11 +385,13 @@ public override CodeTypeDeclaration Generate()
if (IsMixed && (BaseClass == null || (BaseClass is ClassModel && !AllBaseClasses.Any(b => b.IsMixed))))
{
var propName = "Text";
var propertyIndex = 1;

// To not collide with any existing members
for (var propertyIndex = 1; Properties.Exists(x => x.Name.Equals(propName, StringComparison.Ordinal)) || propName.Equals(classDeclaration.Name, StringComparison.Ordinal); propertyIndex++)
while (Properties.Exists(x => x.Name.Equals(propName, StringComparison.Ordinal)) || propName.Equals(classDeclaration.Name, StringComparison.Ordinal))
{
propName = $"Text_{propertyIndex}";
propertyIndex++;
}
// hack to generate automatic property
var text = new CodeMemberField(typeof(string[]), propName + PropertyModel.GetAccessors()) { Attributes = MemberAttributes.Public };
Expand Down

0 comments on commit 4ce4394

Please sign in to comment.