From 638d825fa753d929f07cb2459882f20463184373 Mon Sep 17 00:00:00 2001 From: James Cracknell Date: Sun, 14 Apr 2024 15:55:25 -0600 Subject: [PATCH] Removed extension PropertyInfo.HasAttribute in favor of builtin IsDefined --- .../SchemaGenerator/JsonSerializerDataContractResolver.cs | 6 +++--- .../SchemaGenerator/PropertyInfoExtensions.cs | 6 ------ 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/JsonSerializerDataContractResolver.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/JsonSerializerDataContractResolver.cs index 86c2255f57..4322f73f5a 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/JsonSerializerDataContractResolver.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/JsonSerializerDataContractResolver.cs @@ -166,8 +166,8 @@ private IEnumerable GetDataPropertiesFor(Type objectType, out Type return (property.IsPubliclyReadable() || property.IsPubliclyWritable()) && !(property.GetIndexParameters().Any()) && - !(property.HasAttribute() && isIgnoredViaNet5Attribute) && - !(property.HasAttribute()) && + !(property.IsDefined(typeof(JsonIgnoreAttribute)) && isIgnoredViaNet5Attribute) && + !(property.IsDefined(typeof(SwaggerIgnoreAttribute))) && !(_serializerOptions.IgnoreReadOnlyProperties && !property.IsPubliclyWritable()); }) .OrderBy(property => property.DeclaringType.GetInheritanceChain().Length); @@ -176,7 +176,7 @@ private IEnumerable GetDataPropertiesFor(Type objectType, out Type foreach (var propertyInfo in applicableProperties) { - if (propertyInfo.HasAttribute() + if (propertyInfo.IsDefined(typeof(JsonExtensionDataAttribute)) && propertyInfo.PropertyType.IsConstructedFrom(typeof(IDictionary<,>), out Type constructedDictionary)) { extensionDataType = constructedDictionary.GenericTypeArguments[1]; diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/PropertyInfoExtensions.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/PropertyInfoExtensions.cs index 020a139247..2211e8d62a 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/PropertyInfoExtensions.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/PropertyInfoExtensions.cs @@ -5,12 +5,6 @@ namespace Swashbuckle.AspNetCore.SwaggerGen { public static class PropertyInfoExtensions { - public static bool HasAttribute(this PropertyInfo property) - where TAttribute : Attribute - { - return property.GetCustomAttribute() != null; - } - public static bool IsPubliclyReadable(this PropertyInfo property) { return property.GetMethod?.IsPublic == true;