From 0b5f3fa1ff4148ca00f8ea5af028ee9f26c8d988 Mon Sep 17 00:00:00 2001 From: Michael Ganss Date: Mon, 26 Jul 2021 17:09:34 +0200 Subject: [PATCH] Fix sonarcloud issue --- XmlSchemaClassGenerator/TypeModel.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index 338f527c..dc482be0 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -118,6 +118,7 @@ public abstract class TypeModel public List Documentation { get; private set; } public bool IsAnonymous { get; set; } public GeneratorConfiguration Configuration { get; private set; } + public virtual bool IsSubtype => false; protected TypeModel(GeneratorConfiguration configuration) { @@ -148,7 +149,7 @@ protected void GenerateTypeAttribute(CodeTypeDeclaration typeDeclaration) var typeAttribute = new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference(typeof(XmlTypeAttribute), Configuration), new CodeAttributeArgument(new CodePrimitiveExpression(XmlSchemaName.Name)), new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(XmlSchemaName.Namespace))); - if (IsAnonymous && (this is not ClassModel classModel || classModel.BaseClass == null)) + if (IsAnonymous && !IsSubtype) { // don't generate AnonymousType if it's derived class, otherwise XmlSerializer will // complain with "InvalidOperationException: Cannot include anonymous type '...'" @@ -259,6 +260,8 @@ public class ClassModel : ReferenceTypeModel public bool IsSubstitution { get; set; } public TypeModel BaseClass { get; set; } public List DerivedTypes { get; set; } + public override bool IsSubtype => BaseClass != null; + public ClassModel(GeneratorConfiguration configuration) : base(configuration) {