diff --git a/src/OpenRiaServices.Tools/Framework/DataContractProxyGenerator.cs b/src/OpenRiaServices.Tools/Framework/DataContractProxyGenerator.cs index fcf97843..51c78d62 100644 --- a/src/OpenRiaServices.Tools/Framework/DataContractProxyGenerator.cs +++ b/src/OpenRiaServices.Tools/Framework/DataContractProxyGenerator.cs @@ -22,7 +22,6 @@ internal abstract class DataContractProxyGenerator : ProxyGenerator { private readonly bool _isRoundtripType; private readonly IDictionary _typeMapping; - private readonly List _attributeTypesToFilter; /// /// Initializes a new instance of the class. @@ -37,19 +36,6 @@ protected DataContractProxyGenerator(CodeDomClientCodeGenerator proxyGenerator, _typeMapping = typeMapping; NotificationMethodGen = new NotificationMethodGenerator(proxyGenerator); _isRoundtripType = type.Attributes()[typeof(RoundtripOriginalAttribute)] != null; - - // Add attributes which should be used to filter the attributes on type to be generated - _attributeTypesToFilter = new() - { - // DataContractAttribute and KnownTypeAttribute are handled seperatly - typeof(DataContractAttribute), - typeof(KnownTypeAttribute), -#if NET - // Filter out NullableAttribute and NullableContextAttribute, should only be used by compiler - Type.GetType("System.Runtime.CompilerServices.NullableAttribute"), - Type.GetType("System.Runtime.CompilerServices.NullableContextAttribute"), -#endif - }; } /// @@ -399,7 +385,8 @@ protected virtual void GenerateProperty(PropertyDescriptor propertyDescriptor) property.Name = propertyName; property.Type = propTypeReference; property.Attributes = MemberAttributes.Public | MemberAttributes.Final; // final needed, else becomes virtual - List propertyAttributes = propertyDescriptor.ExplicitAttributes().Cast().ToList(); + var propertyAttributes = propertyDescriptor.ExplicitAttributes().Cast() + .Where(a => a.GetType().Namespace != "System.Runtime.CompilerServices").ToList(); //Do not use attributes only used by the compiler // Generate for property string comment = string.Format(CultureInfo.CurrentCulture, Resource.CodeGen_Entity_Property_Summary_Comment, propertyName); @@ -549,8 +536,11 @@ private IEnumerable FilterTypeAttributes(AttributeCollection typeAttr } } - // Filter out attributes in filteredAttributes and attributeTypesToFilter - return typeAttributes.Cast().Where(a => !_attributeTypesToFilter.Contains(a.GetType()) && !filteredAttributes.Contains(a)); + // Filter out attributes in filteredAttributes, attributes which should only be used by the compiler, + // DataContractAttribute and KnownTypeAttribute since they are handled seperatly + return typeAttributes.Cast().Where(a => a.GetType().Namespace != "System.Runtime.CompilerServices" + && a.GetType() != typeof(DataContractAttribute) && a.GetType() != typeof(KnownTypeAttribute) + && !filteredAttributes.Contains(a)); } /// diff --git a/src/OpenRiaServices.Tools/Test/CodeGenRequiredModifierTests.cs b/src/OpenRiaServices.Tools/Test/CodeGenRequiredModifierTests.cs new file mode 100644 index 00000000..23c52da9 --- /dev/null +++ b/src/OpenRiaServices.Tools/Test/CodeGenRequiredModifierTests.cs @@ -0,0 +1,31 @@ +#if NET7_0_OR_GREATER //required modifier does not work before net7 +using System.ComponentModel.DataAnnotations; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; + +namespace OpenRiaServices.Tools.Test; + +/// +/// CodeGen tests for the required modifier +/// +[TestClass] +public class CodeGenRequiredModifierTests +{ + [TestMethod] + [Description("CodeGen does not apply compiler exclusive attribute for required")] + public void CodeGen_Required_Modifier_Dont_Use_Compiler_Attributes() + { + MockSharedCodeService sts = TestHelper.CreateCommonMockSharedCodeService(); + string generatedCode = TestHelper.GenerateCodeAssertSuccess("C#", new Type[] {typeof(Mock_CG_Required_Entity_DomainService) }, null, sts); + TestHelper.AssertGeneratedCodeDoesNotContain(generatedCode, "RequiredMember"); + } +} + +public class Mock_CG_Required_Entity_DomainService : GenericDomainService { } + +public class Mock_CG_Required_Entity +{ + [Key] + public required string RequiredProperty { get; set; } +} +#endif