From bb898896800046a77c8635374b97578f327a2c52 Mon Sep 17 00:00:00 2001 From: Alexey Katargin Date: Mon, 6 Sep 2021 15:08:02 +0200 Subject: [PATCH] any attribute is not rendered if complex type extends other complex type --- XmlSchemaClassGenerator.Tests/Compiler.cs | 3 +- .../XmlSchemaClassGenerator.Tests.csproj | 2 +- XmlSchemaClassGenerator.Tests/XmlTests.cs | 27 +++++++++++--- .../xsd/simple/any.xsd | 37 +++++++++++++++++++ .../xsd/simple/fields_ambiguity.xsd | 29 +++++++++++++++ .../xsd/simple/nested_type.xsd | 24 ++++++++++++ XmlSchemaClassGenerator/CodeUtilities.cs | 20 +++++++--- XmlSchemaClassGenerator/ModelBuilder.cs | 25 ++++++++++++- XmlSchemaClassGenerator/TypeModel.cs | 25 +++++++++++-- 9 files changed, 173 insertions(+), 19 deletions(-) create mode 100644 XmlSchemaClassGenerator.Tests/xsd/simple/any.xsd create mode 100644 XmlSchemaClassGenerator.Tests/xsd/simple/fields_ambiguity.xsd create mode 100644 XmlSchemaClassGenerator.Tests/xsd/simple/nested_type.xsd diff --git a/XmlSchemaClassGenerator.Tests/Compiler.cs b/XmlSchemaClassGenerator.Tests/Compiler.cs index 4db9c633..f7925a03 100644 --- a/XmlSchemaClassGenerator.Tests/Compiler.cs +++ b/XmlSchemaClassGenerator.Tests/Compiler.cs @@ -112,7 +112,8 @@ public static Assembly GenerateFiles(string name, IEnumerable files, Gen CompactTypeNames = generatorPrototype.CompactTypeNames, UniqueTypeNamesAcrossNamespaces = generatorPrototype.UniqueTypeNamesAcrossNamespaces, CreateGeneratedCodeAttributeVersion = generatorPrototype.CreateGeneratedCodeAttributeVersion, - NetCoreSpecificCode = generatorPrototype.NetCoreSpecificCode + NetCoreSpecificCode = generatorPrototype.NetCoreSpecificCode, + NamingScheme = generatorPrototype.NamingScheme }; gen.CommentLanguages.Clear(); diff --git a/XmlSchemaClassGenerator.Tests/XmlSchemaClassGenerator.Tests.csproj b/XmlSchemaClassGenerator.Tests/XmlSchemaClassGenerator.Tests.csproj index 080203d4..c8c4891a 100644 --- a/XmlSchemaClassGenerator.Tests/XmlSchemaClassGenerator.Tests.csproj +++ b/XmlSchemaClassGenerator.Tests/XmlSchemaClassGenerator.Tests.csproj @@ -24,7 +24,7 @@ runtime; build; native; contentfiles; analyzers - + diff --git a/XmlSchemaClassGenerator.Tests/XmlTests.cs b/XmlSchemaClassGenerator.Tests/XmlTests.cs index 326589fd..665eef0b 100644 --- a/XmlSchemaClassGenerator.Tests/XmlTests.cs +++ b/XmlSchemaClassGenerator.Tests/XmlTests.cs @@ -300,12 +300,24 @@ public void TestListWithPublicPropertySettersWithoutConstructorsSpecified() } } + public static IEnumerable TestSimpleData() { + foreach (var referenceMode in new[] + { CodeTypeReferenceOptions.GlobalReference, /*CodeTypeReferenceOptions.GenericTypeParameter*/ }) + foreach (var namingScheme in new[] { NamingScheme.Direct, NamingScheme.PascalCase }) + foreach (var collectionType in new[] { typeof(Collection<>), /*typeof(Array)*/ }) + { + yield return new object[] { referenceMode, namingScheme, collectionType }; + } + } - [Fact, TestPriority(1)] + + [Theory, TestPriority(1)] + [MemberData(nameof(TestSimpleData))] [UseCulture("en-US")] - public void TestSimple() + public void TestSimple(CodeTypeReferenceOptions referenceMode, NamingScheme namingScheme, Type collectionType) { - Compiler.Generate("Simple", SimplePattern, new Generator + var name = $"Simple_{referenceMode}_{namingScheme}_{collectionType}"; + Compiler.Generate(name, SimplePattern, new Generator { GenerateNullables = true, IntegerDataType = typeof(int), @@ -316,10 +328,13 @@ public void TestSimple() GenerateInterfaces = true, NamespacePrefix = "Simple", GenerateDescriptionAttribute = true, - CodeTypeReferenceOptions = CodeTypeReferenceOptions.GlobalReference, - NetCoreSpecificCode = true + CodeTypeReferenceOptions = referenceMode, + NetCoreSpecificCode = true, + NamingScheme = namingScheme, + CollectionType = collectionType, + CollectionSettersMode = CollectionSettersMode.Public }); - TestSamples("Simple", SimplePattern); + TestSamples(name, SimplePattern); } [Fact, TestPriority(1)] diff --git a/XmlSchemaClassGenerator.Tests/xsd/simple/any.xsd b/XmlSchemaClassGenerator.Tests/xsd/simple/any.xsd new file mode 100644 index 00000000..7b0ae60a --- /dev/null +++ b/XmlSchemaClassGenerator.Tests/xsd/simple/any.xsd @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/XmlSchemaClassGenerator.Tests/xsd/simple/fields_ambiguity.xsd b/XmlSchemaClassGenerator.Tests/xsd/simple/fields_ambiguity.xsd new file mode 100644 index 00000000..e93e175e --- /dev/null +++ b/XmlSchemaClassGenerator.Tests/xsd/simple/fields_ambiguity.xsd @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/XmlSchemaClassGenerator.Tests/xsd/simple/nested_type.xsd b/XmlSchemaClassGenerator.Tests/xsd/simple/nested_type.xsd new file mode 100644 index 00000000..537403ea --- /dev/null +++ b/XmlSchemaClassGenerator.Tests/xsd/simple/nested_type.xsd @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + diff --git a/XmlSchemaClassGenerator/CodeUtilities.cs b/XmlSchemaClassGenerator/CodeUtilities.cs index e88a597f..a267ed2f 100644 --- a/XmlSchemaClassGenerator/CodeUtilities.cs +++ b/XmlSchemaClassGenerator/CodeUtilities.cs @@ -269,11 +269,6 @@ public static string GetUniqueFieldName(this TypeModel typeModel, PropertyModel var i = 0; foreach (var prop in classModel.Properties) { - if (!classModel.Configuration.EnableDataBinding && !(prop.Type is SimpleModel)) - { - continue; - } - if (propertyModel == prop) { i += 1; @@ -399,7 +394,20 @@ public static CodeTypeReference CreateTypeReference(Type t, GeneratorConfigurati return typeRef; } else - return new CodeTypeReference(t, conf.CodeTypeReferenceOptions); + { + var typeRef = new CodeTypeReference(t, conf.CodeTypeReferenceOptions); + + foreach (var typeArg in typeRef.TypeArguments) + { + if (typeArg is CodeTypeReference typeArgRef) + { + typeArgRef.Options = conf.CodeTypeReferenceOptions; + } + } + + return typeRef; + } + } public static CodeTypeReference CreateTypeReference(string namespaceName, string typeName, GeneratorConfiguration conf) diff --git a/XmlSchemaClassGenerator/ModelBuilder.cs b/XmlSchemaClassGenerator/ModelBuilder.cs index 23cd1435..06ce6e39 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 { @@ -577,7 +578,29 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaComplexType complexType, } } + XmlSchemaAnyAttribute anyAttribute = null; if (complexType.AnyAttribute != null) + anyAttribute = complexType.AnyAttribute; + else if (complexType.AttributeWildcard != null) + { + var hasAnyAttribute = true; + for (var baseType = complexType.BaseXmlSchemaType; baseType != null; baseType = baseType.BaseXmlSchemaType) + { + if (baseType is not XmlSchemaComplexType baseComplexType) + continue; + + if (baseComplexType.AttributeWildcard != null) + { + hasAnyAttribute = false; + break; + } + } + + if (hasAnyAttribute) + anyAttribute = complexType.AttributeWildcard; + } + + if (anyAttribute != null) { var property = new PropertyModel(_configuration) { @@ -589,7 +612,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaComplexType complexType, IsAny = true }; - var attributeDocs = GetDocumentation(complexType.AnyAttribute); + var attributeDocs = GetDocumentation(anyAttribute); property.Documentation.AddRange(attributeDocs); classModel.Properties.Add(property); diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index b4307cf1..6f579eb1 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -1,7 +1,6 @@ using System; using System.CodeDom; using System.CodeDom.Compiler; -using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; @@ -172,17 +171,32 @@ protected void GenerateSerializableAttribute(CodeTypeDeclaration typeDeclaration public virtual CodeTypeReference GetReferenceFor(NamespaceModel referencingNamespace, bool collection = false, bool forInit = false, bool attribute = false) { string name; + var referencingOptions = Configuration.CodeTypeReferenceOptions; if (referencingNamespace == Namespace) + { name = Name; + referencingOptions = CodeTypeReferenceOptions.GenericTypeParameter; + } + else if ((referencingNamespace ?? Namespace).IsAmbiguous) + { + name = string.Format("global::{0}.{1}", Namespace.Name, Name); + referencingOptions = CodeTypeReferenceOptions.GenericTypeParameter; + } else - name = string.Format("{2}{0}.{1}", Namespace.Name, Name, ((referencingNamespace ?? Namespace).IsAmbiguous ? "global::" : string.Empty)); + { + name = string.Format("{0}.{1}", Namespace.Name, Name); + } if (collection) { name = forInit ? SimpleModel.GetCollectionImplementationName(name, Configuration) : SimpleModel.GetCollectionDefinitionName(name, Configuration); + if (Configuration.CollectionType == typeof(System.Array)) + referencingOptions = CodeTypeReferenceOptions.GenericTypeParameter; + else + referencingOptions = Configuration.CodeTypeReferenceOptions; } - return new CodeTypeReference(name, Configuration.CodeTypeReferenceOptions); + return new CodeTypeReference(name, referencingOptions); } public virtual CodeExpression GetDefaultValueFor(string defaultString, bool attribute) @@ -1411,7 +1425,10 @@ private static string GetFullTypeName(string typeName, CodeTypeReference typeRef typeRef.ArrayElementType = new CodeTypeReference(typeName); typeRef.ArrayRank = 1; } - var typeOfExpr = new CodeTypeOfExpression(typeRef); + var typeOfExpr = new CodeTypeOfExpression(typeRef) + { + Type = { Options = CodeTypeReferenceOptions.GenericTypeParameter } + }; var writer = new System.IO.StringWriter(); CSharpProvider.GenerateCodeFromExpression(typeOfExpr, writer, new CodeGeneratorOptions()); var fullTypeName = writer.ToString();