From f0c94fd3e5043f9d230d9d476a5f32000223742a Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Tue, 23 Apr 2024 17:38:09 +1000 Subject: [PATCH] Avoid some LINQ allocations (#2819) --- .../SchemaGenerator/MemberInfoExtensions.cs | 9 ++++----- .../SwaggerGenerator/SwaggerGenerator.cs | 10 +++++++--- .../ApiExplorer/ApiDescriptionFactory.cs | 3 +-- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/MemberInfoExtensions.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/MemberInfoExtensions.cs index aa6165758e..e3f104bfa5 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/MemberInfoExtensions.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/MemberInfoExtensions.cs @@ -85,9 +85,9 @@ public static bool IsDictionaryValueNonNullable(this MemberInfo memberInfo) private static object GetNullableAttribute(this MemberInfo memberInfo) { - var nullableAttribute = memberInfo.GetCustomAttributes() - .Where(attr => string.Equals(attr.GetType().FullName, NullableAttributeFullTypeName)) - .FirstOrDefault(); + var nullableAttribute = memberInfo + .GetCustomAttributes() + .FirstOrDefault(attr => string.Equals(attr.GetType().FullName, NullableAttributeFullTypeName)); return nullableAttribute; } @@ -103,8 +103,7 @@ private static bool GetNullableFallbackValue(this MemberInfo memberInfo) var attributes = (IEnumerable)declaringType.GetCustomAttributes(false); var nullableContext = attributes - .Where(attr => string.Equals(attr.GetType().FullName, NullableContextAttributeFullTypeName)) - .FirstOrDefault(); + .FirstOrDefault(attr => string.Equals(attr.GetType().FullName, NullableContextAttributeFullTypeName)); if (nullableContext != null) { diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs index 302ab35731..15e4099024 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs @@ -86,9 +86,13 @@ public OpenApiDocument GetSwagger(string documentName, string host = null, strin var applicableApiDescriptions = _apiDescriptionsProvider.ApiDescriptionGroups.Items .SelectMany(group => group.Items) - .Where(apiDesc => !(_options.IgnoreObsoleteActions && apiDesc.CustomAttributes().OfType().Any())) - .Where(apiDesc => !apiDesc.CustomAttributes().OfType().Any()) - .Where(apiDesc => _options.DocInclusionPredicate(documentName, apiDesc)); + .Where(apiDesc => + { + var attributes = apiDesc.CustomAttributes().ToList(); + return !(_options.IgnoreObsoleteActions && attributes.OfType().Any()) && + !attributes.OfType().Any() && + _options.DocInclusionPredicate(documentName, apiDesc); + }); var schemaRepository = new SchemaRepository(documentName); diff --git a/test/Swashbuckle.AspNetCore.TestSupport/ApiExplorer/ApiDescriptionFactory.cs b/test/Swashbuckle.AspNetCore.TestSupport/ApiExplorer/ApiDescriptionFactory.cs index 91c198814e..0f7a0b782b 100644 --- a/test/Swashbuckle.AspNetCore.TestSupport/ApiExplorer/ApiDescriptionFactory.cs +++ b/test/Swashbuckle.AspNetCore.TestSupport/ApiExplorer/ApiDescriptionFactory.cs @@ -36,8 +36,7 @@ public static ApiDescription Create( // If the provided action has a matching parameter - use it to assign ParameterDescriptor & ModelMetadata parameter.ParameterDescriptor = actionDescriptor.Parameters .OfType() - .Where(parameterDescriptor => parameterDescriptor.Name == parameter.Name) - .FirstOrDefault(); + .FirstOrDefault(parameterDescriptor => parameterDescriptor.Name == parameter.Name); var parameterDescriptorWithParameterInfo = parameter.ParameterDescriptor as #if NETCOREAPP2_2_OR_GREATER