From f9a78b07e3f9fa593d1aa267933f8011aa0c9c50 Mon Sep 17 00:00:00 2001 From: Trygve Lorentzen Date: Tue, 17 Jan 2023 20:08:09 +0100 Subject: [PATCH] Implemented option to treat enumeration as string instead of creating enum. --- XmlSchemaClassGenerator.Console/Program.cs | 3 ++ XmlSchemaClassGenerator.Tests/Compiler.cs | 1 + XmlSchemaClassGenerator.Tests/XmlTests.cs | 38 +++++++++++++++++++ XmlSchemaClassGenerator/Generator.cs | 6 +++ .../GeneratorConfiguration.cs | 2 + XmlSchemaClassGenerator/ModelBuilder.cs | 5 ++- 6 files changed, 53 insertions(+), 2 deletions(-) diff --git a/XmlSchemaClassGenerator.Console/Program.cs b/XmlSchemaClassGenerator.Console/Program.cs index d6e7459d..ec7d8c2b 100644 --- a/XmlSchemaClassGenerator.Console/Program.cs +++ b/XmlSchemaClassGenerator.Console/Program.cs @@ -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(); var options = new OptionSet { @@ -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 }, }; @@ -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); diff --git a/XmlSchemaClassGenerator.Tests/Compiler.cs b/XmlSchemaClassGenerator.Tests/Compiler.cs index fc0dd3f7..3566cee7 100644 --- a/XmlSchemaClassGenerator.Tests/Compiler.cs +++ b/XmlSchemaClassGenerator.Tests/Compiler.cs @@ -116,6 +116,7 @@ public static Assembly GenerateFiles(string name, IEnumerable files, Gen EnableNullableReferenceAttributes = generatorPrototype.EnableNullableReferenceAttributes, NamingScheme = generatorPrototype.NamingScheme, UseArrayItemAttribute = generatorPrototype.UseArrayItemAttribute, + EnumAsString = generatorPrototype.EnumAsString, }; gen.CommentLanguages.Clear(); diff --git a/XmlSchemaClassGenerator.Tests/XmlTests.cs b/XmlSchemaClassGenerator.Tests/XmlTests.cs index 2b7cfe8b..59c1e4e9 100644 --- a/XmlSchemaClassGenerator.Tests/XmlTests.cs +++ b/XmlSchemaClassGenerator.Tests/XmlTests.cs @@ -65,6 +65,7 @@ private static IEnumerable ConvertXml(string name, IEnumerable x CollectionImplementationType = generatorPrototype.CollectionImplementationType, CollectionSettersMode = generatorPrototype.CollectionSettersMode, UseArrayItemAttribute = generatorPrototype.UseArrayItemAttribute, + EnumAsString = generatorPrototype.EnumAsString, }; gen.CommentLanguages.Clear(); @@ -975,6 +976,43 @@ public void CollidingElementAndComplexTypeNamesCanBeResolved() Assert.Contains(@"public enum MyType2", generatedType); } + [Fact] + public void EnumAsStringOption() + { + const string xsd = @" + + + + + + + + + + + + + + + + + + +"; + 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() { diff --git a/XmlSchemaClassGenerator/Generator.cs b/XmlSchemaClassGenerator/Generator.cs index bd621e23..54e4146f 100644 --- a/XmlSchemaClassGenerator/Generator.cs +++ b/XmlSchemaClassGenerator/Generator.cs @@ -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; } diff --git a/XmlSchemaClassGenerator/GeneratorConfiguration.cs b/XmlSchemaClassGenerator/GeneratorConfiguration.cs index e147a7c4..7167d884 100644 --- a/XmlSchemaClassGenerator/GeneratorConfiguration.cs +++ b/XmlSchemaClassGenerator/GeneratorConfiguration.cs @@ -41,6 +41,8 @@ public GeneratorConfiguration() CommandLineArgumentsProvider = CommandLineArgumentsProvider.CreateFromEnvironment(); } + public bool EnumAsString { get; set; } + /// /// The writer to be used to generate the code files /// diff --git a/XmlSchemaClassGenerator/ModelBuilder.cs b/XmlSchemaClassGenerator/ModelBuilder.cs index cc21e696..2d94dd12 100644 --- a/XmlSchemaClassGenerator/ModelBuilder.cs +++ b/XmlSchemaClassGenerator/ModelBuilder.cs @@ -5,6 +5,7 @@ using System.Xml; using System.Xml.Linq; using System.Xml.Schema; +using System.Xml.Serialization; namespace XmlSchemaClassGenerator { @@ -632,10 +633,10 @@ XmlSchemaSimpleTypeUnion typeUnion when AllMembersHaveFacets(typeUnion, out base if (facets.Count > 0) { var enumFacets = facets.OfType().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().Any()))) + if (enumFacets.Count > 0 && (baseFacets is null || baseFacets.All(fs => fs.OfType().Any())) && !_configuration.EnumAsString) return CreateEnumModel(simpleType, enumFacets); restrictions = GetRestrictions(facets, simpleType).Where(r => r != null).Sanitize().ToList();