Skip to content

Commit

Permalink
Do not duplicate attributes/elements on restricted derived classes
Browse files Browse the repository at this point in the history
Do not duplicate Text property for mixed elements which have a base class that is also mixed
Fix bug in IsArray
  • Loading branch information
mganss committed Oct 16, 2015
1 parent 36ad856 commit 5d1eb8a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Unsupported:
* Some restriction types
* Recursive choices and choices whose elements have minOccurs > 0 (see [below](#choice))
* Possible name clashes and invalid identifiers when names contain non-alphanumeric characters
* Groups with maxOccurs > 0

Usage
-----
Expand Down
25 changes: 17 additions & 8 deletions XmlSchemaClassGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,11 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaAnnotated type, XmlQualif
{
if (complexType.ContentModel.Content is XmlSchemaComplexContentExtension)
particle = ((XmlSchemaComplexContentExtension)complexType.ContentModel.Content).Particle;
else if (complexType.ContentModel.Content is XmlSchemaComplexContentRestriction)
particle = ((XmlSchemaComplexContentRestriction)complexType.ContentModel.Content).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 if (complexType.ContentModel.Content is XmlSchemaComplexContentRestriction)
// particle = ((XmlSchemaComplexContentRestriction)complexType.ContentModel.Content).Particle;
}
else particle = complexType.ContentTypeParticle;

Expand All @@ -520,15 +523,21 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaAnnotated type, XmlQualif
attributes = ((XmlSchemaComplexContentExtension)complexType.ContentModel.Content).Attributes;
else if (complexType.ContentModel.Content is XmlSchemaSimpleContentExtension)
attributes = ((XmlSchemaSimpleContentExtension)complexType.ContentModel.Content).Attributes;
else if (complexType.ContentModel.Content is XmlSchemaComplexContentRestriction)
attributes = ((XmlSchemaComplexContentRestriction)complexType.ContentModel.Content).Attributes;
else if (complexType.ContentModel.Content is XmlSchemaSimpleContentRestriction)
attributes = ((XmlSchemaSimpleContentRestriction)complexType.ContentModel.Content).Attributes;

// If it's a restriction, do not duplicate attributes on the derived class, they're already in the base class.
// See https://msdn.microsoft.com/en-us/library/f3z3wh0y.aspx
//else if (complexType.ContentModel.Content is XmlSchemaComplexContentRestriction)
// attributes = ((XmlSchemaComplexContentRestriction)complexType.ContentModel.Content).Attributes;
//else if (complexType.ContentModel.Content is XmlSchemaSimpleContentRestriction)
// attributes = ((XmlSchemaSimpleContentRestriction)complexType.ContentModel.Content).Attributes;
}
else attributes = complexType.Attributes;

var attributeProperties = CreatePropertiesForAttributes(source, classModel, attributes.Cast<XmlSchemaObject>());
classModel.Properties.AddRange(attributeProperties);
if (attributes != null)
{
var attributeProperties = CreatePropertiesForAttributes(source, classModel, attributes.Cast<XmlSchemaObject>());
classModel.Properties.AddRange(attributeProperties);
}

if (GenerateInterfaces)
{
Expand Down
22 changes: 18 additions & 4 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@ public ClassModel(GeneratorConfiguration configuration)
DerivedTypes = new List<ClassModel>();
}

public IEnumerable<ClassModel> AllBaseClasses
{
get
{
var baseClass = BaseClass as ClassModel;
while (baseClass != null)
{
yield return baseClass;
baseClass = baseClass.BaseClass as ClassModel;
}
}
}

public override CodeTypeDeclaration Generate()
{
var classDeclaration = base.Generate();
Expand Down Expand Up @@ -326,7 +339,7 @@ public override CodeTypeDeclaration Generate()
foreach (var property in Properties)
property.AddMembersTo(classDeclaration, EnableDataBinding);

if (IsMixed && (BaseClass == null || BaseClass is ClassModel))
if (IsMixed && (BaseClass == null || (BaseClass is ClassModel && !AllBaseClasses.Any(b => b.IsMixed))))
{
var text = new CodeMemberField(typeof(string), "Text");
// hack to generate automatic property
Expand Down Expand Up @@ -517,10 +530,11 @@ private bool IsArray
{
get
{
return !IsCollection && !IsAttribute && TypeClassModel != null && TypeClassModel.TotalProperties == 1
return !IsCollection && !IsAttribute && TypeClassModel != null
&& TypeClassModel.BaseClass == null
&& TypeClassModel.Properties.Count == 1
&& !TypeClassModel.Properties[0].IsAttribute && !TypeClassModel.Properties[0].IsAny
&& TypeClassModel.Properties[0].IsCollection
&& TypeClassModel.BaseClass == null;
&& TypeClassModel.Properties[0].IsCollection;
}
}

Expand Down

0 comments on commit 5d1eb8a

Please sign in to comment.