From c72e203fca57469e453958a49b5060df2e04f1f2 Mon Sep 17 00:00:00 2001 From: jmatss Date: Wed, 15 Jun 2022 21:55:24 +0200 Subject: [PATCH] Add support for nested non-nullable choice members --- XmlSchemaClassGenerator/ModelBuilder.cs | 28 ++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/XmlSchemaClassGenerator/ModelBuilder.cs b/XmlSchemaClassGenerator/ModelBuilder.cs index b6b81073..7202c7c8 100644 --- a/XmlSchemaClassGenerator/ModelBuilder.cs +++ b/XmlSchemaClassGenerator/ModelBuilder.cs @@ -917,6 +917,7 @@ private IEnumerable CreatePropertiesForElements(Uri source, TypeM } var effectiveElement = substitute?.Element ?? element; + var isNullableByChoice = IsNullableByChoice(item.XmlParent); var propertyName = _configuration.NamingProvider.ElementNameFromQualifiedName(effectiveElement.QualifiedName, effectiveElement); var originalPropertyName = propertyName; if (propertyName == typeModel.Name) @@ -934,9 +935,9 @@ private IEnumerable CreatePropertiesForElements(Uri source, TypeM OriginalPropertyName = originalPropertyName, Type = substitute?.Type ?? CreateTypeModel(element.ElementSchemaType, elementQualifiedName), IsNillable = element.IsNillable, - IsNullable = item.MinOccurs < 1.0m || (item.XmlParent is XmlSchemaChoice), + IsNullable = item.MinOccurs < 1.0m || isNullableByChoice, IsCollection = item.MaxOccurs > 1.0m || particle.MaxOccurs > 1.0m, // http://msdn.microsoft.com/en-us/library/vstudio/d3hx2s7e(v=vs.100).aspx - DefaultValue = element.DefaultValue ?? ((item.MinOccurs >= 1.0m && item.XmlParent is not XmlSchemaChoice) ? element.FixedValue : null), + DefaultValue = element.DefaultValue ?? ((item.MinOccurs >= 1.0m && !isNullableByChoice) ? element.FixedValue : null), FixedValue = element.FixedValue, XmlNamespace = !string.IsNullOrEmpty(effectiveElement.QualifiedName.Namespace) && effectiveElement.QualifiedName.Namespace != typeModel.XmlSchemaName.Namespace ? effectiveElement.QualifiedName.Namespace : null, @@ -969,7 +970,7 @@ private IEnumerable CreatePropertiesForElements(Uri source, TypeM OwningType = typeModel, Name = "Any", Type = new SimpleModel(_configuration) { ValueType = (_configuration.UseXElementForAny ? typeof(XElement) : typeof(XmlElement)), UseDataTypeAttribute = false }, - IsNullable = item.MinOccurs < 1.0m || (item.XmlParent is XmlSchemaChoice), + IsNullable = item.MinOccurs < 1.0m || IsNullableByChoice(item.XmlParent), IsCollection = item.MaxOccurs > 1.0m || particle.MaxOccurs > 1.0m, // http://msdn.microsoft.com/en-us/library/vstudio/d3hx2s7e(v=vs.100).aspx IsAny = true, XmlParticle = item.XmlParticle, @@ -1020,6 +1021,27 @@ private IEnumerable CreatePropertiesForElements(Uri source, TypeM return properties; } + private static bool IsNullableByChoice(XmlSchemaObject parent) + { + while (parent != null) + { + switch (parent) + { + case XmlSchemaChoice: + return true; + // Any ancestor element between the current item and the + // choice would already have been forced to nullable. + case XmlSchemaElement: + case XmlSchemaParticle p when p.MinOccurs < 1.0m: + return false; + default: + break; + } + parent = parent.Parent; + } + return false; + } + private NamespaceModel CreateNamespaceModel(Uri source, XmlQualifiedName qualifiedName) { NamespaceModel namespaceModel = null;