Skip to content

Commit

Permalink
Merge pull request #368 from trygvelo/EnumAsString
Browse files Browse the repository at this point in the history
Implemented option to treat enumeration as string instead of creating enum
  • Loading branch information
mganss authored Jan 26, 2023
2 parents 45318ea + f9a78b0 commit b021402
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
3 changes: 3 additions & 0 deletions XmlSchemaClassGenerator.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ static void Main(string[] args)
var nullableReferenceAttributes = false;
var generateCommandLineArgs = true;
var useArrayItemAttribute = true;
var enumAsString = false;
var namespaceFiles = new List<string>();

var options = new OptionSet {
Expand Down Expand Up @@ -135,6 +136,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
{ "nc|netCore", "generate .NET Core specific code that might not work with .NET Framework (default is false)", v => netCoreSpecificCode = v != null },
{ "nr|nullableReferenceAttributes", "generate attributes for nullable reference types (default is false)", v => nullableReferenceAttributes = v != null },
{ "ar|useArrayItemAttribute", "use ArrayItemAttribute for sequences with single elements (default is true)", v => useArrayItemAttribute = v != null },
{ "es|enumAsString", "Use string instead of enum for enumeration", v => enumAsString = v != null },
{ "ca|commandArgs", "generate a comment with the exact command line arguments that were used to generate the source code (default is true)", v => generateCommandLineArgs = v != null },
};

Expand Down Expand Up @@ -216,6 +218,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
EnableNullableReferenceAttributes = nullableReferenceAttributes,
GenerateCommandLineArgumentsComment = generateCommandLineArgs,
UseArrayItemAttribute = useArrayItemAttribute,
EnumAsString = enumAsString,
};

generator.CommentLanguages.AddRange(commentLanguages);
Expand Down
1 change: 1 addition & 0 deletions XmlSchemaClassGenerator.Tests/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public static Assembly GenerateFiles(string name, IEnumerable<string> files, Gen
EnableNullableReferenceAttributes = generatorPrototype.EnableNullableReferenceAttributes,
NamingScheme = generatorPrototype.NamingScheme,
UseArrayItemAttribute = generatorPrototype.UseArrayItemAttribute,
EnumAsString = generatorPrototype.EnumAsString,
};

gen.CommentLanguages.Clear();
Expand Down
38 changes: 38 additions & 0 deletions XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private static IEnumerable<string> ConvertXml(string name, IEnumerable<string> x
CollectionImplementationType = generatorPrototype.CollectionImplementationType,
CollectionSettersMode = generatorPrototype.CollectionSettersMode,
UseArrayItemAttribute = generatorPrototype.UseArrayItemAttribute,
EnumAsString = generatorPrototype.EnumAsString,
};

gen.CommentLanguages.Clear();
Expand Down Expand Up @@ -975,6 +976,43 @@ public void CollidingElementAndComplexTypeNamesCanBeResolved()
Assert.Contains(@"public enum MyType2", generatedType);
}

[Fact]
public void EnumAsStringOption()
{
const string xsd = @"<?xml version=""1.0"" encoding = ""UTF-8""?>
<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" targetNamespace=""http://local.none"">
<xs:element name=""Authorisation"">
<xs:complexType>
<xs:sequence>
<xs:element name=""type"">
<xs:simpleType>
<xs:restriction base=""xs:string"">
<xs:enumeration value=""C019""/>
<xs:enumeration value=""C512""/>
<xs:enumeration value=""C513""/>
<xs:enumeration value=""C514""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
";
var generator = new Generator
{
NamespaceProvider = new NamespaceProvider
{
GenerateNamespace = key => "Test"
},
EnumAsString = true
};

var generatedType = ConvertXml(nameof(EnumAsStringOption), xsd, generator).First();

Assert.Contains(@"public string Type", generatedType);
}

[Fact]
public void ComplexTypeWithAttributeGroupExtension()
{
Expand Down
6 changes: 6 additions & 0 deletions XmlSchemaClassGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public OutputWriter OutputWriter
set { _configuration.OutputWriter = value; }
}

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

public bool GenerateComplexTypesForCollections
{
get { return _configuration.GenerateComplexTypesForCollections; }
Expand Down
2 changes: 2 additions & 0 deletions XmlSchemaClassGenerator/GeneratorConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public GeneratorConfiguration()
CommandLineArgumentsProvider = CommandLineArgumentsProvider.CreateFromEnvironment();
}

public bool EnumAsString { get; set; }

/// <summary>
/// The writer to be used to generate the code files
/// </summary>
Expand Down
5 changes: 3 additions & 2 deletions XmlSchemaClassGenerator/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace XmlSchemaClassGenerator
{
Expand Down Expand Up @@ -632,10 +633,10 @@ XmlSchemaSimpleTypeUnion typeUnion when AllMembersHaveFacets(typeUnion, out base
if (facets.Count > 0)
{
var enumFacets = facets.OfType<XmlSchemaEnumerationFacet>().ToList();

// If a union has enum restrictions, there must be an enum restriction in all parts of the union
// If there are other restrictions mixed into the enumeration values, we'll generate a string to play it safe.
if (enumFacets.Count > 0 && (baseFacets is null || baseFacets.All(fs => fs.OfType<XmlSchemaEnumerationFacet>().Any())))
if (enumFacets.Count > 0 && (baseFacets is null || baseFacets.All(fs => fs.OfType<XmlSchemaEnumerationFacet>().Any())) && !_configuration.EnumAsString)
return CreateEnumModel(simpleType, enumFacets);

restrictions = GetRestrictions(facets, simpleType).Where(r => r != null).Sanitize().ToList();
Expand Down

0 comments on commit b021402

Please sign in to comment.