From bdc1c515ce528364a5fce525d1a132bb1bed55df Mon Sep 17 00:00:00 2001 From: Michael Ganss Date: Mon, 20 Jul 2020 16:17:22 +0200 Subject: [PATCH] Fix null reference exception when referenced group is empty Do not add namespace argument if it's already present --- XmlSchemaClassGenerator/ModelBuilder.cs | 13 ++++++++----- XmlSchemaClassGenerator/TypeModel.cs | 3 ++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/XmlSchemaClassGenerator/ModelBuilder.cs b/XmlSchemaClassGenerator/ModelBuilder.cs index ae16ef31..d092d368 100644 --- a/XmlSchemaClassGenerator/ModelBuilder.cs +++ b/XmlSchemaClassGenerator/ModelBuilder.cs @@ -860,13 +860,16 @@ private IEnumerable GetRestrictions(IEnumerable 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; + } } } } diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index da096c87..2a7fdc56 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -1129,7 +1129,8 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi { var arrayItemAttribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(XmlArrayItemAttribute), Configuration.CodeTypeReferenceOptions), propertyAttribute.Arguments.Cast().Where(x => !string.Equals(x.Name, "Order", StringComparison.Ordinal)).ToArray()); - if (!arrayItemProperty.XmlSchemaName.IsEmpty && !string.IsNullOrEmpty(arrayItemProperty.XmlSchemaName.Namespace)) + var namespacePresent = arrayItemAttribute.Arguments.OfType().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); }