Skip to content

Commit

Permalink
Relax requirements for enums (fixes #294)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ganss committed Nov 29, 2021
1 parent 3f2e0a8 commit 9c6f1dc
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions XmlSchemaClassGenerator/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -624,19 +624,28 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaComplexType complexType,
private TypeModel CreateTypeModel(XmlSchemaSimpleType simpleType, NamespaceModel namespaceModel, XmlQualifiedName qualifiedName, List<DocumentationModel> docs)
{
var restrictions = new List<RestrictionModel>();
var allBasesHaveEnums = true;
List<XmlSchemaFacet> facets = new();

if (simpleType.Content is XmlSchemaSimpleTypeRestriction typeRestriction)
{
facets = typeRestriction.Facets.Cast<XmlSchemaFacet>().ToList();
}
else if (simpleType.Content is XmlSchemaSimpleTypeUnion typeUnion
&& typeUnion.BaseMemberTypes.All(b => b.Content is XmlSchemaSimpleTypeRestriction r && r.Facets.Count > 0))
facets = typeUnion.BaseMemberTypes.SelectMany(b => ((XmlSchemaSimpleTypeRestriction)b.Content).Facets.Cast<XmlSchemaFacet>()).ToList();
{
var baseFacets = typeUnion.BaseMemberTypes.Select(b => ((XmlSchemaSimpleTypeRestriction)b.Content).Facets.Cast<XmlSchemaFacet>()).ToList();
// if a union has enum restrictions, there must be an enum restriction in all parts of the union
allBasesHaveEnums = baseFacets.All(fs => fs.OfType<XmlSchemaEnumerationFacet>().Any());
facets = baseFacets.SelectMany(f => f).ToList();
}

if (facets.Any())
{
var enumFacets = facets.OfType<XmlSchemaEnumerationFacet>().ToList();
// If there are other restrictions mixed into the enumeration values, we'll generate a string to play it safe.
var isEnum = enumFacets.Count > 0 && enumFacets.Count == facets.Count;
var isEnum = enumFacets.Any() && allBasesHaveEnums;

if (isEnum)
{
// we got an enum
Expand Down

0 comments on commit 9c6f1dc

Please sign in to comment.