Skip to content

Commit

Permalink
Add (limited) support for unions (fixes #205)
Browse files Browse the repository at this point in the history
Generate enums only if no non-enum restrictions present
  • Loading branch information
Michael Ganss committed May 18, 2020
1 parent 21bedfd commit 5f0ae26
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
24 changes: 12 additions & 12 deletions XmlSchemaClassGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,18 +243,18 @@ public string PrivateMemberPrefix
public bool EnableUpaCheck
{
get { return _configuration.EnableUpaCheck; }
set { _configuration.EnableUpaCheck = value; }
}

public bool SeparateClasses
{
get { return _configuration.SeparateClasses; }
set { _configuration.SeparateClasses = value; }
}

public void Generate(IEnumerable<string> files)
{
var set = new XmlSchemaSet();
set { _configuration.EnableUpaCheck = value; }
}

public bool SeparateClasses
{
get { return _configuration.SeparateClasses; }
set { _configuration.SeparateClasses = value; }
}

public void Generate(IEnumerable<string> files)
{
var set = new XmlSchemaSet();
var settings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore };
var readers = files.Select(f => XmlReader.Create(f, settings));

Expand Down
15 changes: 11 additions & 4 deletions XmlSchemaClassGenerator/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,19 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaComplexType complexType,
private TypeModel CreateTypeModel(XmlSchemaSimpleType simpleType, NamespaceModel namespaceModel, XmlQualifiedName qualifiedName, List<DocumentationModel> docs)
{
var restrictions = new List<RestrictionModel>();
List<XmlSchemaFacet> facets = new List<XmlSchemaFacet>();

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();

if (facets.Any())
{
var enumFacets = typeRestriction.Facets.OfType<XmlSchemaEnumerationFacet>().ToList();
// If there's a pattern restriction mixed into the enumeration values, we'll generate a string to play it safe.
var isEnum = enumFacets.Count > 0 && !typeRestriction.Facets.OfType<XmlSchemaPatternFacet>().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;
if (isEnum)
{
// we got an enum
Expand Down Expand Up @@ -511,7 +518,7 @@ private TypeModel CreateTypeModel(XmlSchemaSimpleType simpleType, NamespaceModel
return enumModel;
}

restrictions = GetRestrictions(typeRestriction.Facets.Cast<XmlSchemaFacet>(), simpleType).Where(r => r != null).Sanitize().ToList();
restrictions = GetRestrictions(facets, simpleType).Where(r => r != null).Sanitize().ToList();
}

var simpleModelName = _configuration.NamingProvider.SimpleTypeNameFromQualifiedName(qualifiedName);
Expand Down

0 comments on commit 5f0ae26

Please sign in to comment.