From b1e88365b0f71b0ff40c6a054dd70c3297616f3e Mon Sep 17 00:00:00 2001 From: Fritz Brandhuber Date: Mon, 20 Sep 2021 14:27:48 +0200 Subject: [PATCH 1/3] fix bug where a ArgumentNullException could prevent the architecture from building Signed-off-by: Fritz Brandhuber --- ArchUnitNET/Loader/MonoCecilAttributeExtensions.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ArchUnitNET/Loader/MonoCecilAttributeExtensions.cs b/ArchUnitNET/Loader/MonoCecilAttributeExtensions.cs index 7bbf2c44d..d0c92ae14 100644 --- a/ArchUnitNET/Loader/MonoCecilAttributeExtensions.cs +++ b/ArchUnitNET/Loader/MonoCecilAttributeExtensions.cs @@ -53,12 +53,12 @@ private static void HandleAttributeArgument(CustomAttributeArgument argument, Ty type = typeFactory.GetOrCreateStubTypeInstanceFromTypeReference(argument.Type); - if (type.IsArray) + if (argument.Value is IEnumerable attArgEnumerable) { - value = (from arrayMember in (CustomAttributeArgument[]) argument.Value - select arrayMember.Value is TypeReference tr + value = (from attArg in attArgEnumerable + select attArg.Value is TypeReference tr ? typeFactory.GetOrCreateStubTypeInstanceFromTypeReference(tr) - : arrayMember.Value) + : attArg.Value) .ToArray(); } else From 1ea237df0bf0aed31791a77ffeb848abe6786adc Mon Sep 17 00:00:00 2001 From: Fritz Brandhuber Date: Mon, 20 Sep 2021 15:03:57 +0200 Subject: [PATCH 2/3] handle null in fluent attribute argument methods (the "wrong" overload with IEnumerable as parameter gets used, which can lead to ArgumentNullExceptions) Signed-off-by: Fritz Brandhuber --- .../Elements/ObjectConditionsDefinition.cs | 16 ++++++++-------- .../Elements/ObjectPredicatesDefinition.cs | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/ArchUnitNET/Fluent/Syntax/Elements/ObjectConditionsDefinition.cs b/ArchUnitNET/Fluent/Syntax/Elements/ObjectConditionsDefinition.cs index f2c148c50..ebbe09683 100644 --- a/ArchUnitNET/Fluent/Syntax/Elements/ObjectConditionsDefinition.cs +++ b/ArchUnitNET/Fluent/Syntax/Elements/ObjectConditionsDefinition.cs @@ -1059,7 +1059,7 @@ public static ICondition HaveAttributeWithNamedArguments(Type attribu public static ICondition HaveAnyAttributesWithArguments(IEnumerable argumentValues) { - var argumentValueList = argumentValues.ToList(); + var argumentValueList = argumentValues?.ToList() ?? new List {null}; string description; Func failDescription; if (argumentValueList.IsNullOrEmpty()) @@ -1124,7 +1124,7 @@ public static ICondition HaveAttributeWithArguments([NotNull] string IEnumerable argumentValues) { string description, failDescription; - var argumentValueList = argumentValues.ToList(); + var argumentValueList = argumentValues?.ToList() ?? new List {null}; if (argumentValueList.IsNullOrEmpty()) { description = "have attribute \"" + attribute + "\""; @@ -1182,7 +1182,7 @@ public static ICondition HaveAttributeWithArguments([NotNull] Attribu IEnumerable argumentValues) { string description, failDescription; - var argumentValueList = argumentValues.ToList(); + var argumentValueList = argumentValues?.ToList() ?? new List {null}; if (argumentValueList.IsNullOrEmpty()) { description = "have attribute \"" + attribute.FullName + "\""; @@ -1240,7 +1240,7 @@ public static ICondition HaveAttributeWithArguments([NotNull] Type at IEnumerable argumentValues) { string description, failDescription; - var argumentValueList = argumentValues.ToList(); + var argumentValueList = argumentValues?.ToList() ?? new List {null}; if (argumentValueList.IsNullOrEmpty()) { description = "have attribute \"" + attribute.FullName + "\""; @@ -2371,7 +2371,7 @@ public static ICondition NotHaveAttributeWithNamedArguments(Type attr public static ICondition NotHaveAnyAttributesWithArguments(IEnumerable argumentValues) { - var argumentValueList = argumentValues.ToList(); + var argumentValueList = argumentValues?.ToList() ?? new List {null}; string description; Func failDescription; if (argumentValueList.IsNullOrEmpty()) @@ -2435,7 +2435,7 @@ public static ICondition NotHaveAttributeWithArguments([NotNull] stri IEnumerable argumentValues) { string description, failDescription; - var argumentValueList = argumentValues.ToList(); + var argumentValueList = argumentValues?.ToList() ?? new List {null}; if (argumentValueList.IsNullOrEmpty()) { description = "not have attribute \"" + attribute + "\""; @@ -2493,7 +2493,7 @@ public static ICondition NotHaveAttributeWithArguments([NotNull] Attr IEnumerable argumentValues) { string description, failDescription; - var argumentValueList = argumentValues.ToList(); + var argumentValueList = argumentValues?.ToList() ?? new List {null}; if (argumentValueList.IsNullOrEmpty()) { description = "not have attribute \"" + attribute.FullName + "\""; @@ -2551,7 +2551,7 @@ public static ICondition NotHaveAttributeWithArguments([NotNull] Type IEnumerable argumentValues) { string description, failDescription; - var argumentValueList = argumentValues.ToList(); + var argumentValueList = argumentValues?.ToList() ?? new List {null}; if (argumentValueList.IsNullOrEmpty()) { description = "not have attribute \"" + attribute.FullName + "\""; diff --git a/ArchUnitNET/Fluent/Syntax/Elements/ObjectPredicatesDefinition.cs b/ArchUnitNET/Fluent/Syntax/Elements/ObjectPredicatesDefinition.cs index a38ff407c..7fce67027 100644 --- a/ArchUnitNET/Fluent/Syntax/Elements/ObjectPredicatesDefinition.cs +++ b/ArchUnitNET/Fluent/Syntax/Elements/ObjectPredicatesDefinition.cs @@ -673,7 +673,7 @@ public static IPredicate HaveAttributeWithNamedArguments(Type attribute, public static IPredicate HaveAnyAttributesWithArguments(IEnumerable argumentValues) { - var argumentValueList = argumentValues.ToList(); + var argumentValueList = argumentValues?.ToList() ?? new List {null}; string description; if (argumentValueList.IsNullOrEmpty()) { @@ -718,7 +718,7 @@ public static IPredicate HaveAttributeWithArguments([NotNull] string attribut IEnumerable argumentValues) { string description; - var argumentValueList = argumentValues.ToList(); + var argumentValueList = argumentValues?.ToList() ?? new List {null}; if (argumentValueList.IsNullOrEmpty()) { description = "have attribute \"" + attribute + "\""; @@ -772,7 +772,7 @@ public static IPredicate HaveAttributeWithArguments([NotNull] Attribute attri IEnumerable argumentValues) { string description; - var argumentValueList = argumentValues.ToList(); + var argumentValueList = argumentValues?.ToList() ?? new List {null}; if (argumentValueList.IsNullOrEmpty()) { description = "have attribute \"" + attribute.FullName + "\""; @@ -826,7 +826,7 @@ public static IPredicate HaveAttributeWithArguments([NotNull] Type attribute, IEnumerable argumentValues) { string description; - var argumentValueList = argumentValues.ToList(); + var argumentValueList = argumentValues?.ToList() ?? new List {null}; if (argumentValueList.IsNullOrEmpty()) { description = "have attribute \"" + attribute.FullName + "\""; @@ -1619,7 +1619,7 @@ public static IPredicate DoNotHaveAttributeWithNamedArguments(Type attribute, public static IPredicate DoNotHaveAnyAttributesWithArguments(IEnumerable argumentValues) { - var argumentValueList = argumentValues.ToList(); + var argumentValueList = argumentValues?.ToList() ?? new List {null}; string description; if (argumentValueList.IsNullOrEmpty()) { @@ -1664,7 +1664,7 @@ public static IPredicate DoNotHaveAttributeWithArguments([NotNull] string att IEnumerable argumentValues) { string description; - var argumentValueList = argumentValues.ToList(); + var argumentValueList = argumentValues?.ToList() ?? new List {null}; if (argumentValueList.IsNullOrEmpty()) { description = "do not have attribute \"" + attribute + "\""; @@ -1718,7 +1718,7 @@ public static IPredicate DoNotHaveAttributeWithArguments([NotNull] Attribute IEnumerable argumentValues) { string description; - var argumentValueList = argumentValues.ToList(); + var argumentValueList = argumentValues?.ToList() ?? new List {null}; if (argumentValueList.IsNullOrEmpty()) { description = "do not have attribute \"" + attribute.FullName + "\""; @@ -1772,7 +1772,7 @@ public static IPredicate DoNotHaveAttributeWithArguments([NotNull] Type attri IEnumerable argumentValues) { string description; - var argumentValueList = argumentValues.ToList(); + var argumentValueList = argumentValues?.ToList() ?? new List {null}; if (argumentValueList.IsNullOrEmpty()) { description = "do not have attribute \"" + attribute.FullName + "\""; From 296d5123eba0726f4e4d1fe81662963d6f163071 Mon Sep 17 00:00:00 2001 From: Fritz Brandhuber Date: Mon, 20 Sep 2021 15:04:43 +0200 Subject: [PATCH 3/3] implement testcase from PR #110 Signed-off-by: Fritz Brandhuber --- .../Domain/AttributeArgumentTests.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/ArchUnitNETTests/Domain/AttributeArgumentTests.cs b/ArchUnitNETTests/Domain/AttributeArgumentTests.cs index 646ca7f29..5102a0026 100644 --- a/ArchUnitNETTests/Domain/AttributeArgumentTests.cs +++ b/ArchUnitNETTests/Domain/AttributeArgumentTests.cs @@ -112,7 +112,7 @@ public void FluentPredicatesTest() .HaveAnyAttributesWithNamedArguments(("Parameter2", "param2_1"), ("Parameter3", "param3_2")).Should() .Exist().Check(Architecture); Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).And() - .HaveAnyAttributesWithArguments(1).Should() + .HaveAnyAttributesWithArguments(null).Should() .Exist().Check(Architecture); Types().That().Are(typeof(ClassWithTypeParameterAttribute)).And() .HaveAnyAttributesWithArguments(typeof(ClassWithArrayParameterAttribute)).Should() @@ -151,7 +151,7 @@ public void FluentPredicatesTest() .Should() .NotExist().Check(Architecture); Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).And() - .DoNotHaveAnyAttributesWithArguments(1).Should() + .DoNotHaveAnyAttributesWithArguments(null).Should() .NotExist().Check(Architecture); Types().That().Are(typeof(ClassWithTypeParameterAttribute)).And() .DoNotHaveAnyAttributesWithArguments(typeof(ClassWithArrayParameterAttribute)).Should() @@ -188,7 +188,7 @@ public void FluentConditionsTest() .HaveAnyAttributesWithNamedArguments(("Parameter2", "param2_1"), ("Parameter3", "param3_2")) .Check(Architecture); Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).Should() - .HaveAnyAttributesWithArguments(1).Check(Architecture); + .HaveAnyAttributesWithArguments(null).Check(Architecture); Types().That().Are(typeof(ClassWithTypeParameterAttribute)).Should() .HaveAnyAttributesWithArguments(typeof(ClassWithArrayParameterAttribute)).Check(Architecture); Types().That().Are(typeof(ClassWithTypeParameterAttribute)).Should() @@ -227,7 +227,7 @@ public void FluentConditionsTest() .Check(Architecture)); Assert.Throws(() => Types().That() .Are(typeof(ClassWithMultipleAttributesWithParameters)).Should() - .NotHaveAnyAttributesWithArguments(1).Check(Architecture)); + .NotHaveAnyAttributesWithArguments(null).Check(Architecture)); Assert.Throws(() => Types().That().Are(typeof(ClassWithTypeParameterAttribute)) .Should() .NotHaveAnyAttributesWithArguments(typeof(ClassWithArrayParameterAttribute)).Check(Architecture)); @@ -270,9 +270,12 @@ public AttributeWithStringParameters(string parameter1, string parameter2) internal class AttributeWithObjectParameter : Attribute { - public AttributeWithObjectParameter(object type) + public AttributeWithObjectParameter(params object[] arguments) + { + } + + public AttributeWithObjectParameter(object arg) { - Type = type; } public object Type { get; } @@ -283,7 +286,7 @@ public AttributeWithObjectParameter(object type) [AttributeWithStringParameters("param1_0")] [AttributeWithStringParameters("param1_1", Parameter2 = "param2_1")] [AttributeWithStringParameters("param1_2", "param2_2", Parameter3 = "param3_2")] - [AttributeWithObjectParameter(1)] + [AttributeWithObjectParameter(null)] internal class ClassWithMultipleAttributesWithParameters { }