Skip to content

Commit 9c6f1dc

Browse files
author
Michael Ganss
committed
Relax requirements for enums (fixes #294)
1 parent 3f2e0a8 commit 9c6f1dc

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

XmlSchemaClassGenerator/ModelBuilder.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -624,19 +624,28 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaComplexType complexType,
624624
private TypeModel CreateTypeModel(XmlSchemaSimpleType simpleType, NamespaceModel namespaceModel, XmlQualifiedName qualifiedName, List<DocumentationModel> docs)
625625
{
626626
var restrictions = new List<RestrictionModel>();
627+
var allBasesHaveEnums = true;
627628
List<XmlSchemaFacet> facets = new();
628629

629630
if (simpleType.Content is XmlSchemaSimpleTypeRestriction typeRestriction)
631+
{
630632
facets = typeRestriction.Facets.Cast<XmlSchemaFacet>().ToList();
633+
}
631634
else if (simpleType.Content is XmlSchemaSimpleTypeUnion typeUnion
632635
&& typeUnion.BaseMemberTypes.All(b => b.Content is XmlSchemaSimpleTypeRestriction r && r.Facets.Count > 0))
633-
facets = typeUnion.BaseMemberTypes.SelectMany(b => ((XmlSchemaSimpleTypeRestriction)b.Content).Facets.Cast<XmlSchemaFacet>()).ToList();
636+
{
637+
var baseFacets = typeUnion.BaseMemberTypes.Select(b => ((XmlSchemaSimpleTypeRestriction)b.Content).Facets.Cast<XmlSchemaFacet>()).ToList();
638+
// if a union has enum restrictions, there must be an enum restriction in all parts of the union
639+
allBasesHaveEnums = baseFacets.All(fs => fs.OfType<XmlSchemaEnumerationFacet>().Any());
640+
facets = baseFacets.SelectMany(f => f).ToList();
641+
}
634642

635643
if (facets.Any())
636644
{
637645
var enumFacets = facets.OfType<XmlSchemaEnumerationFacet>().ToList();
638646
// If there are other restrictions mixed into the enumeration values, we'll generate a string to play it safe.
639-
var isEnum = enumFacets.Count > 0 && enumFacets.Count == facets.Count;
647+
var isEnum = enumFacets.Any() && allBasesHaveEnums;
648+
640649
if (isEnum)
641650
{
642651
// we got an enum

0 commit comments

Comments
 (0)