From 689dff71423caa7437002ba3396d114cb60e57ce Mon Sep 17 00:00:00 2001 From: Mathew Jones Date: Mon, 22 Aug 2022 14:11:13 +0100 Subject: [PATCH 1/2] Deficient model checking updated to allow data member checks --- Meraki.ApiChecker/Extensions/TypeExtension.cs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/Meraki.ApiChecker/Extensions/TypeExtension.cs b/Meraki.ApiChecker/Extensions/TypeExtension.cs index f0b13934d..0a1126122 100644 --- a/Meraki.ApiChecker/Extensions/TypeExtension.cs +++ b/Meraki.ApiChecker/Extensions/TypeExtension.cs @@ -27,7 +27,7 @@ public static class TypeExtension public static bool IsGenericList(this Type type) => type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>); - public static List GetDeficientDataModels(this Type type, bool isRoot = true) + public static List GetDeficientDataModels(this Type type, bool isRoot = true, bool isParentClassReadOnly = false) { // If we're at the root and this is a generic list then deal with the underlying generic type // as attributes won't be assigned to the generic list @@ -43,17 +43,14 @@ public static List GetDeficientDataModels(this Type type, bool isRoot = return new(); } - if (type.GetCustomAttribute() is not null) - { - // It's a read-only class, so return here - return new List(); - } + var isClassReadOnly = isParentClassReadOnly || type.GetCustomAttribute() is not null; var deficientDataModels = new List(); foreach (var property in type.GetProperties()) { // Is ApiAccessAttribute or ApiKeyAttribute present on the property? - if (property.GetCustomAttribute() is null + if (!isClassReadOnly + && property.GetCustomAttribute() is null && property.GetCustomAttribute() is null) { // NO - ApiAccess is not fully denoted for the type @@ -68,7 +65,7 @@ public static List GetDeficientDataModels(this Type type, bool isRoot = // Is it a class? AND ensure it's a class we didn't discover yet if (propertyType?.IsClass == true && !deficientDataModels.Contains(propertyType.Name)) { - deficientDataModels.AddRange(GetDeficientDataModels(propertyType, false)); + deficientDataModels.AddRange(GetDeficientDataModels(propertyType, false, isClassReadOnly)); } } From c037d591b0907b5100ef14d6d90e3b2e1804a026 Mon Sep 17 00:00:00 2001 From: Mathew Jones Date: Mon, 22 Aug 2022 14:41:52 +0100 Subject: [PATCH 2/2] Now checks for duplicate properties --- Meraki.ApiChecker/Extensions/TypeExtension.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Meraki.ApiChecker/Extensions/TypeExtension.cs b/Meraki.ApiChecker/Extensions/TypeExtension.cs index 0a1126122..20b7e3399 100644 --- a/Meraki.ApiChecker/Extensions/TypeExtension.cs +++ b/Meraki.ApiChecker/Extensions/TypeExtension.cs @@ -1,5 +1,6 @@ using Meraki.Api.Attributes; using System.Reflection; +using System.Runtime.Serialization; namespace Meraki.ApiChecker.Extensions; public static class TypeExtension @@ -46,8 +47,24 @@ public static List GetDeficientDataModels(this Type type, bool isRoot = var isClassReadOnly = isParentClassReadOnly || type.GetCustomAttribute() is not null; var deficientDataModels = new List(); + var dataMemberNames = new Dictionary(); + foreach (var property in type.GetProperties()) { + var dataMemberAttribute = property.GetCustomAttribute(); + if (dataMemberAttribute != null) + { + var dataMemberName = dataMemberAttribute.Name ?? "Not set"; + if (dataMemberNames.TryGetValue(dataMemberName, out var existingPropertyName)) + { + deficientDataModels.Add($"Found {existingPropertyName} already has DataMemberName '{dataMemberName}' when processing {property.Name} property on {type}"); + } + else + { + dataMemberNames[dataMemberName] = property.Name; + } + } + // Is ApiAccessAttribute or ApiKeyAttribute present on the property? if (!isClassReadOnly && property.GetCustomAttribute() is null @@ -56,7 +73,7 @@ public static List GetDeficientDataModels(this Type type, bool isRoot = // NO - ApiAccess is not fully denoted for the type if (!deficientDataModels.Contains(type.Name)) { - deficientDataModels.Add(type.Name); + deficientDataModels.Add($"{type.Name} is missing ApiAccessAttribute"); } }