Skip to content

Commit

Permalink
Include restrictions from inherited types (fixes #431)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ganss committed Oct 17, 2023
1 parent 8604846 commit 4539c1e
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions XmlSchemaClassGenerator/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -618,16 +618,16 @@ private TypeModel CreateTypeModel(XmlSchemaComplexType complexType)
return classModel;
}


private TypeModel CreateTypeModel(XmlSchemaSimpleType simpleType)
{
List<RestrictionModel> restrictions = null;
List<IEnumerable<XmlSchemaFacet>> baseFacets = null;

var facets = simpleType.Content switch
{
XmlSchemaSimpleTypeRestriction typeRestriction => typeRestriction.Facets.Cast<XmlSchemaFacet>().ToList(),
XmlSchemaSimpleTypeUnion typeUnion when AllMembersHaveFacets(typeUnion, out baseFacets) => baseFacets.SelectMany(f => f).ToList(),
_ => new(),
_ => MergeRestrictions(simpleType)
};

if (facets.Count > 0)
Expand All @@ -651,6 +651,27 @@ static bool AllMembersHaveFacets(XmlSchemaSimpleTypeUnion typeUnion, out List<IE
baseFacets = !retval ? null : members.Select(r => r.Facets.Cast<XmlSchemaFacet>()).ToList();
return retval;
}

static List<XmlSchemaFacet> MergeRestrictions(XmlSchemaSimpleType type)
{
if (type == null) return new();
var baseFacets = MergeRestrictions(type.BaseXmlSchemaType as XmlSchemaSimpleType);
if (type.Content is XmlSchemaSimpleTypeRestriction typeRestriction)
{
var facets = typeRestriction.Facets.Cast<XmlSchemaFacet>().ToList();
foreach (var facet in facets)
{
var baseFacet = baseFacets
.Where(f => f is not XmlSchemaEnumerationFacet
&& f.GetType() == facet.GetType())
.SingleOrDefault();
if (baseFacet != null)
baseFacets.Remove(baseFacet);
baseFacets.Add(facet);
}
}
return baseFacets;
}
}

private static List<EnumValueModel> EnsureEnumValuesUnique(List<EnumValueModel> enumModelValues)
Expand Down

0 comments on commit 4539c1e

Please sign in to comment.