From fc1028aee59bfdb82dc122a21dd80c816003fdda Mon Sep 17 00:00:00 2001 From: Ming Yau Lee Date: Fri, 16 Aug 2024 13:03:02 +0100 Subject: [PATCH] Allow disabling of merging restrictions with base in configuration --- XmlSchemaClassGenerator.Console/Program.cs | 3 +++ XmlSchemaClassGenerator/Generator.cs | 6 ++++++ XmlSchemaClassGenerator/GeneratorConfiguration.cs | 3 +++ XmlSchemaClassGenerator/ModelBuilder.cs | 3 ++- 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/XmlSchemaClassGenerator.Console/Program.cs b/XmlSchemaClassGenerator.Console/Program.cs index de83521..d098f73 100644 --- a/XmlSchemaClassGenerator.Console/Program.cs +++ b/XmlSchemaClassGenerator.Console/Program.cs @@ -61,6 +61,7 @@ static int Main(string[] args) var generateCommandLineArgs = true; var useArrayItemAttribute = true; var enumAsString = false; + var disableMergeRestrictionsWithBase = false; var namespaceFiles = new List(); var nameSubstituteFiles = new List(); var unionCommonType = false; @@ -159,6 +160,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l { "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 }, + { "dmb|disableMergeRestrictionsWithBase", "Disable merging of simple type restrictions with base type restrictions", v => disableMergeRestrictionsWithBase = 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 }, { "uc|unionCommonType", "generate a common type for unions if possible (default is false)", v => unionCommonType = v != null }, { "ec|serializeEmptyCollections", "serialize empty collections (default is false)", v => serializeEmptyCollections = v != null }, @@ -259,6 +261,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l GenerateCommandLineArgumentsComment = generateCommandLineArgs, UseArrayItemAttribute = useArrayItemAttribute, EnumAsString = enumAsString, + MergeRestrictionsWithBase = !disableMergeRestrictionsWithBase, MapUnionToWidestCommonType = unionCommonType, SeparateNamespaceHierarchy = separateNamespaceHierarchy, SerializeEmptyCollections = serializeEmptyCollections, diff --git a/XmlSchemaClassGenerator/Generator.cs b/XmlSchemaClassGenerator/Generator.cs index 7e584d8..36bfc26 100644 --- a/XmlSchemaClassGenerator/Generator.cs +++ b/XmlSchemaClassGenerator/Generator.cs @@ -39,6 +39,12 @@ public bool EnumAsString set { _configuration.EnumAsString = value; } } + public bool MergeRestrictionsWithBase + { + get { return _configuration.MergeRestrictionsWithBase; } + set { _configuration.MergeRestrictionsWithBase = value; } + } + public bool GenerateComplexTypesForCollections { get { return _configuration.GenerateComplexTypesForCollections; } diff --git a/XmlSchemaClassGenerator/GeneratorConfiguration.cs b/XmlSchemaClassGenerator/GeneratorConfiguration.cs index 4541bc0..78c5ffd 100644 --- a/XmlSchemaClassGenerator/GeneratorConfiguration.cs +++ b/XmlSchemaClassGenerator/GeneratorConfiguration.cs @@ -39,10 +39,13 @@ public GeneratorConfiguration() Version = VersionProvider.CreateFromAssembly(); EnableUpaCheck = true; CommandLineArgumentsProvider = CommandLineArgumentsProvider.CreateFromEnvironment(); + MergeRestrictionsWithBase = true; } public bool EnumAsString { get; set; } + public bool MergeRestrictionsWithBase { get; set; } + /// /// The writer to be used to generate the code files /// diff --git a/XmlSchemaClassGenerator/ModelBuilder.cs b/XmlSchemaClassGenerator/ModelBuilder.cs index af223bf..d186fa9 100644 --- a/XmlSchemaClassGenerator/ModelBuilder.cs +++ b/XmlSchemaClassGenerator/ModelBuilder.cs @@ -635,6 +635,7 @@ private TypeModel CreateTypeModel(XmlSchemaSimpleType simpleType) var facets = simpleType.Content switch { + XmlSchemaSimpleTypeRestriction typeRestriction when !_configuration.MergeRestrictionsWithBase => typeRestriction.Facets.Cast().ToList(), XmlSchemaSimpleTypeUnion typeUnion when AllMembersHaveFacets(typeUnion, out baseFacets) => baseFacets.SelectMany(f => f).ToList(), _ => MergeRestrictions(simpleType) }; @@ -1108,4 +1109,4 @@ private string BuildNamespace(Uri source, string xmlNamespace) : throw new ArgumentException(string.Format("Namespace {0} not provided through map or generator.", xmlNamespace)); } } -} \ No newline at end of file +}