Skip to content

Commit

Permalink
Fix null reference exception when referenced group is empty
Browse files Browse the repository at this point in the history
Do not add namespace argument if it's already present
  • Loading branch information
Michael Ganss committed Jul 20, 2020
1 parent eb532e6 commit bdc1c51
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
13 changes: 8 additions & 5 deletions XmlSchemaClassGenerator/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -860,13 +860,16 @@ private IEnumerable<RestrictionModel> GetRestrictions(IEnumerable<XmlSchemaFacet

public IEnumerable<Particle> GetElements(XmlSchemaGroupBase groupBase)
{
foreach (var item in groupBase.Items)
if (groupBase?.Items != null)
{
foreach (var element in GetElements(item, groupBase))
foreach (var item in groupBase.Items)
{
element.MaxOccurs = Math.Max(element.MaxOccurs, groupBase.MaxOccurs);
element.MinOccurs = Math.Min(element.MinOccurs, groupBase.MinOccurs);
yield return element;
foreach (var element in GetElements(item, groupBase))
{
element.MaxOccurs = Math.Max(element.MaxOccurs, groupBase.MaxOccurs);
element.MinOccurs = Math.Min(element.MinOccurs, groupBase.MinOccurs);
yield return element;
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,8 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
{
var arrayItemAttribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(XmlArrayItemAttribute), Configuration.CodeTypeReferenceOptions),
propertyAttribute.Arguments.Cast<CodeAttributeArgument>().Where(x => !string.Equals(x.Name, "Order", StringComparison.Ordinal)).ToArray());
if (!arrayItemProperty.XmlSchemaName.IsEmpty && !string.IsNullOrEmpty(arrayItemProperty.XmlSchemaName.Namespace))
var namespacePresent = arrayItemAttribute.Arguments.OfType<CodeAttributeArgument>().Any(a => a.Name == "Namespace");
if (!namespacePresent && !arrayItemProperty.XmlSchemaName.IsEmpty && !string.IsNullOrEmpty(arrayItemProperty.XmlSchemaName.Namespace))
arrayItemAttribute.Arguments.Add(new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(arrayItemProperty.XmlSchemaName.Namespace)));
member.CustomAttributes.Add(arrayItemAttribute);
}
Expand Down

0 comments on commit bdc1c51

Please sign in to comment.