Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion XmlSchemaClassGenerator/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ private IEnumerable<Substitute> GetSubstitutedElements(XmlQualifiedName name)
{
if (SubstitutionGroups.TryGetValue(name, out var substitutes))
{
foreach (var substitute in substitutes.Where(s => s.Element.QualifiedName != name))
foreach (var substitute in substitutes.Where(s => !s.Element.IsAbstract && s.Element.QualifiedName != name))
{
yield return substitute;
foreach (var recursiveSubstitute in GetSubstitutedElements(substitute.Element.QualifiedName))
Expand Down
60 changes: 60 additions & 0 deletions XmlSchemaClassGenerator/Models/PropertyModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ string checkEquality()

private bool IsPrivateSetter => IsEnumerable && Configuration.CollectionSettersMode == CollectionSettersMode.Private;

private bool IsPrimitiveType => Type is SimpleModel simpleModel
&& (simpleModel.ValueType.IsPrimitive
|| simpleModel.ValueType == typeof(string)
|| simpleModel.ValueType == typeof(decimal));

private CodeTypeReference TypeReference => PropertyType.GetReferenceFor(OwningType.Namespace, collection: IsEnumerable, attribute: IsAttribute);

private void AddDocs(CodeTypeMember member)
Expand Down Expand Up @@ -473,6 +478,12 @@ [new CodeMethodReturnStatement(valueExpression)],
var attributes = GetAttributes(isArray).ToArray();
member.CustomAttributes.AddRange(attributes);

CodeStatement choiceIdentifierInitStatement = null;
if (!IsPrimitiveType && Substitutes.Any(sub => sub.Type == Type))
{
choiceIdentifierInitStatement = AddChoiceIdentifier(typeDeclaration, member, isEnumerable);
}

// initialize List<>
if (isEnumerable && (Configuration.CollectionSettersMode != CollectionSettersMode.PublicWithoutConstructorInitialization)
&& (Configuration.CollectionSettersMode != CollectionSettersMode.InitWithoutConstructorInitialization))
Expand Down Expand Up @@ -509,6 +520,11 @@ [new CodeMethodReturnStatement(valueExpression)],
}

constructor.Statements.Add(new CodeAssignStatement(listReference, initExpression));

if (choiceIdentifierInitStatement != null)
{
constructor.Statements.Add(choiceIdentifierInitStatement);
}
}

if (isArray)
Expand Down Expand Up @@ -648,4 +664,48 @@ private IEnumerable<CodeAttributeDeclaration> GetAttributes(bool isArray, TypeMo

return attributes;
}

private CodeStatement AddChoiceIdentifier(CodeTypeDeclaration typeDeclaration, CodeMemberField memberField, bool isArray)
{
var enumName = Name + "Enum";
var propertyName = enumName + "Identifier";

var xmlElementNames = memberField.CustomAttributes
.OfType<CodeAttributeDeclaration>()
.Where(attribute => attribute.AttributeType.BaseType == typeof(XmlElementAttribute).FullName)
.Select(attribute => attribute.Arguments
.OfType<CodeAttributeArgument>()
.Where(argument => string.IsNullOrEmpty(argument.Name))
.Select(argument => argument.Value)
.OfType<CodePrimitiveExpression>()
.Select(primitive => primitive.Value?.ToString())
.FirstOrDefault(name => name != null))
.Where(name => name != null);

var choiceEnum = new CodeTypeDeclaration { Name = enumName, IsEnum = true };

foreach (var elementName in xmlElementNames)
choiceEnum.Members.Add(new CodeMemberField(enumName, elementName));

var choiceProperty = new CodeMemberField
{
Name = propertyName + " { get; set; }",
Type = new CodeTypeReference(enumName, isArray ? 1 : 0),
Attributes = MemberAttributes.Public | MemberAttributes.Final
};
choiceProperty.CustomAttributes.Add(AttributeDecl<XmlIgnoreAttribute>());

memberField.CustomAttributes.Add(AttributeDecl<XmlChoiceIdentifierAttribute>(
new CodeAttributeArgument(new CodePrimitiveExpression(propertyName))));

typeDeclaration.Members.Add(choiceEnum);
typeDeclaration.Members.Add(choiceProperty);

if (!isArray)
return null;

var choiceReference = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), propertyName);
var emptyArray = new CodeMethodInvokeExpression(new(TypeRefExpr<Array>(), nameof(Array.Empty), new CodeTypeReference(enumName)));
return new CodeAssignStatement(choiceReference, emptyArray);
}
}