Skip to content

Commit

Permalink
Pass parent MinOccurs and MaxOccurs when creating properties (see #273)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ganss committed Jul 26, 2021
1 parent aaa3a12 commit c88b0d9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
23 changes: 13 additions & 10 deletions XmlSchemaClassGenerator/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private void CreateSubstitutes()
var cls = (ClassModel)prop.OwningType;
var schema = substitute.Element.GetSchema();
var source = CodeUtilities.CreateUri(schema.SourceUri);
var props = CreatePropertiesForElements(source, cls, prop.XmlParticle, new[] { prop.Particle }, substitute, order);
var props = CreatePropertiesForElements(source, cls, prop.Particle, new[] { prop.Particle }, substitute, order);

cls.Properties.AddRange(props);

Expand Down Expand Up @@ -379,8 +379,9 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaGroup group, NamespaceMod
Types[key] = interfaceModel;
}

var particle = group.Particle;
var items = GetElements(particle);
var xmlParticle = group.Particle;
var particle = new Particle(xmlParticle, group.Parent);
var items = GetElements(xmlParticle);
var properties = CreatePropertiesForElements(source, interfaceModel, particle, items.Where(i => !(i.XmlParticle is XmlSchemaGroupRef)));
interfaceModel.Properties.AddRange(properties);
var interfaces = items.Select(i => i.XmlParticle).OfType<XmlSchemaGroupRef>()
Expand Down Expand Up @@ -462,20 +463,20 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaComplexType complexType,
if (baseModel is ClassModel baseClassModel) { baseClassModel.DerivedTypes.Add(classModel); }
}

XmlSchemaParticle particle = null;
XmlSchemaParticle xmlParticle = null;
if (classModel.BaseClass != null)
{
if (complexType.ContentModel.Content is XmlSchemaComplexContentExtension complexContent)
{
particle = complexContent.Particle;
xmlParticle = complexContent.Particle;
}

// If it's a restriction, do not duplicate elements on the derived class, they're already in the base class.
// See https://msdn.microsoft.com/en-us/library/f3z3wh0y.aspx
}
else particle = complexType.Particle ?? complexType.ContentTypeParticle;
else xmlParticle = complexType.Particle ?? complexType.ContentTypeParticle;

var items = GetElements(particle, complexType).ToList();
var items = GetElements(xmlParticle, complexType).ToList();

if (_configuration.GenerateInterfaces)
{
Expand All @@ -485,6 +486,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaComplexType complexType,
classModel.AddInterfaces(interfaces);
}

var particle = new Particle(xmlParticle, xmlParticle?.Parent);
var properties = CreatePropertiesForElements(source, classModel, particle, items);
classModel.Properties.AddRange(properties);

Expand Down Expand Up @@ -763,10 +765,11 @@ private IEnumerable<PropertyModel> CreatePropertiesForAttributes(Uri source, Typ
return properties;
}

private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeModel typeModel, XmlSchemaParticle particle, IEnumerable<Particle> items,
private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeModel typeModel, Particle particle, IEnumerable<Particle> items,
Substitute substitute = null, int order = 0)
{
var properties = new List<PropertyModel>();
var xmlParticle = particle.XmlParticle;

foreach (var item in items)
{
Expand All @@ -784,7 +787,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
if (elementQualifiedName.IsEmpty)
{
// inner type, have to generate a type name
var typeModelName = particle is XmlSchemaGroupRef groupRef ? groupRef.RefName : typeModel.XmlSchemaName;
var typeModelName = xmlParticle is XmlSchemaGroupRef groupRef ? groupRef.RefName : typeModel.XmlSchemaName;
var typeName = _configuration.NamingProvider.PropertyNameFromElement(typeModelName.Name, element.QualifiedName.Name);
elementQualifiedName = new XmlQualifiedName(typeName, typeModel.XmlSchemaName.Namespace);
// try to avoid name clashes
Expand Down Expand Up @@ -861,7 +864,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
}

var groupItems = GetElements(group.Particle);
var groupProperties = CreatePropertiesForElements(source, typeModel, item.XmlParticle, groupItems, order: order).ToList();
var groupProperties = CreatePropertiesForElements(source, typeModel, item, groupItems, order: order).ToList();
if (_configuration.EmitOrder)
{
order += groupProperties.Count;
Expand Down
4 changes: 2 additions & 2 deletions XmlSchemaClassGenerator/Particle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public Particle(XmlSchemaParticle particle, XmlSchemaObject parent)
{
XmlParticle = particle;
XmlParent = parent;
MinOccurs = particle.MinOccurs;
MaxOccurs = particle.MaxOccurs;
MinOccurs = particle?.MinOccurs ?? 1;
MaxOccurs = particle?.MaxOccurs ?? 1;
}

public XmlSchemaParticle XmlParticle { get; set; }
Expand Down

0 comments on commit c88b0d9

Please sign in to comment.