From 6b684a537006f2b81c74a9c2c49158976bd576db Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Thu, 21 Nov 2024 22:36:42 +0200 Subject: [PATCH] Use Count instead of using Any() --- Directory.Build.props | 3 +-- src/NSwag.AspNetCore/SwaggerUiSettings.cs | 4 ++-- .../Models/CSharpOperationModel.cs | 2 +- .../Commands/Document/ExecuteDocumentCommand.cs | 2 +- .../Commands/InputOutputCommandBase.cs | 2 +- src/NSwag.Core/OpenApiParameter.cs | 12 ++++++------ src/NSwag.Core/OpenApiPathItem.cs | 4 ++-- .../Parameters/DefaultParametersTests.cs | 2 +- .../AspNetCoreOpenApiDocumentGenerator.cs | 2 +- .../AspNetCoreOperationSecurityScopeProcessor.cs | 2 +- .../Processors/OperationParameterProcessor.cs | 2 +- .../Processors/OperationParameterProcessor.cs | 2 +- .../WebApiOpenApiDocumentGenerator.cs | 2 +- .../Processors/ApiVersionProcessor.cs | 4 ++-- .../Processors/DocumentTagsProcessor.cs | 9 +++------ .../Processors/OperationTagsProcessor.cs | 4 ++-- .../Security/OperationSecurityScopeProcessor.cs | 4 ++-- 17 files changed, 29 insertions(+), 33 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index d38c7488e8..260dfa0d63 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -104,7 +104,6 @@ [CA1845] Use span-based 'string.Concat' and 'AsSpan' instead of 'Substring' [CA1847] Use 'string.Contains(char)' instead of 'string.Contains(string)' - needs polyfill [CA1854] Prefer a 'TryGetValue' call over a Dictionary indexer access - [CA1860] Prefer comparing 'Count' to 0 rather than using 'Any()' [CA1861] Prefer 'static readonly' fields over constant array arguments [CA1862] Prefer using 'string.Equals(string, StringComparison)' to perform a case-insensitive comparison [CA1865] Use 'string.StartsWith(char)' instead of 'string.StartsWith(string)' - needs polyfill @@ -116,7 +115,7 @@ [SYSLIB0012] 'Assembly.CodeBase' is obsolete --> $(NoWarn);IDE0005;IDE0008;IDE0011;IDE0017;IDE0019;IDE0021;IDE0022;IDE0025;IDE0027;IDE0028;IDE0029;IDE0032;IDE0039;IDE0040;IDE0044;IDE0045;IDE0046;IDE0055;IDE0056;IDE0057;IDE0059;IDE0060;IDE0061;IDE0063;IDE0074;IDE0078;IDE0083;IDE0090;IDE0100;IDE0130;IDE0160;IDE0200;IDE0260;IDE0270;IDE0290;IDE0300;IDE0305;IDE0301;IDE0330;IDE1005 - $(NoWarn);CA1200;CA1304;CA1305;CA1310;CA1311;CA1507;CA1510;CA1514;CA1710;CA1716;CA1720;CA1725;CA1805;CA1827;CA1834;CA1845;CA1847;CA1854;CA1860;CA1861;CA1862;CA1865;CA1866;CA1870;CA2249;CA2263;SYSLIB0012 + $(NoWarn);CA1200;CA1304;CA1305;CA1310;CA1311;CA1507;CA1510;CA1514;CA1710;CA1716;CA1720;CA1725;CA1805;CA1827;CA1834;CA1845;CA1847;CA1854;CA1861;CA1862;CA1865;CA1866;CA1870;CA2249;CA2263;SYSLIB0012 diff --git a/src/NSwag.AspNetCore/SwaggerUiSettings.cs b/src/NSwag.AspNetCore/SwaggerUiSettings.cs index 7b6a2e7d1e..eed7d02176 100644 --- a/src/NSwag.AspNetCore/SwaggerUiSettings.cs +++ b/src/NSwag.AspNetCore/SwaggerUiSettings.cs @@ -134,7 +134,7 @@ public bool WithCredentials public Func>> SwaggerRoutesFactory { get; set; } #endif - internal override string ActualSwaggerDocumentPath => SwaggerRoutes.Any() ? "" : base.ActualSwaggerDocumentPath; + internal override string ActualSwaggerDocumentPath => SwaggerRoutes.Count > 0 ? "" : base.ActualSwaggerDocumentPath; #if AspNetOwin internal override async Task TransformHtmlAsync(string html, IOwinRequest request, CancellationToken cancellationToken) @@ -165,7 +165,7 @@ internal override async Task TransformHtmlAsync(string html, HttpRequest (await SwaggerRoutesFactory(request, cancellationToken)).ToList() : SwaggerRoutes; - htmlBuilder.Replace("{Urls}", !swaggerRoutes.Any() + htmlBuilder.Replace("{Urls}", swaggerRoutes.Count == 0 ? "undefined" : JsonConvert.SerializeObject( #pragma warning disable 618 diff --git a/src/NSwag.CodeGeneration.CSharp/Models/CSharpOperationModel.cs b/src/NSwag.CodeGeneration.CSharp/Models/CSharpOperationModel.cs index 1de28ebd91..2ab1bd37d9 100644 --- a/src/NSwag.CodeGeneration.CSharp/Models/CSharpOperationModel.cs +++ b/src/NSwag.CodeGeneration.CSharp/Models/CSharpOperationModel.cs @@ -169,7 +169,7 @@ public IEnumerable ExceptionDescriptions .Where(r => r.ThrowsException) .SelectMany(r => { - if (r.ExpectedSchemas?.Any() == true) + if (r.ExpectedSchemas?.Count > 0) { return r.ExpectedSchemas .Where(s => s.Schema.ActualSchema?.InheritsSchema(_resolver.ExceptionSchema) == true) diff --git a/src/NSwag.Commands/Commands/Document/ExecuteDocumentCommand.cs b/src/NSwag.Commands/Commands/Document/ExecuteDocumentCommand.cs index 6c6538ba4f..2bfcd45aae 100644 --- a/src/NSwag.Commands/Commands/Document/ExecuteDocumentCommand.cs +++ b/src/NSwag.Commands/Commands/Document/ExecuteDocumentCommand.cs @@ -42,7 +42,7 @@ public async Task RunAsync(CommandLineProcessor processor, IConsoleHost var currentDirectory = Directory.GetCurrentDirectory(); var files = Directory.GetFiles(currentDirectory, "*.nswag"); - if (files.Any()) + if (files.Length > 0) { foreach (var file in files) { diff --git a/src/NSwag.Commands/Commands/InputOutputCommandBase.cs b/src/NSwag.Commands/Commands/InputOutputCommandBase.cs index 6358d5d386..c235f01760 100644 --- a/src/NSwag.Commands/Commands/InputOutputCommandBase.cs +++ b/src/NSwag.Commands/Commands/InputOutputCommandBase.cs @@ -59,7 +59,7 @@ protected async Task GetInputSwaggerDocument() document.Host = ServiceHost; } - if (ServiceSchemes != null && ServiceSchemes.Any()) + if (ServiceSchemes != null && ServiceSchemes.Length > 0) { document.Schemes = ServiceSchemes.Select(s => (OpenApiSchema)Enum.Parse(typeof(OpenApiSchema), s, true)).ToList(); } diff --git a/src/NSwag.Core/OpenApiParameter.cs b/src/NSwag.Core/OpenApiParameter.cs index 1800e83dec..49f476cccc 100644 --- a/src/NSwag.Core/OpenApiParameter.cs +++ b/src/NSwag.Core/OpenApiParameter.cs @@ -223,11 +223,11 @@ public bool IsXmlBodyParameter } var parent = Parent as OpenApiOperation; - var consumes = parent?.ActualConsumes?.Any() == true ? + var consumes = parent?.ActualConsumes?.Count > 0 ? parent.ActualConsumes : parent?.ActualRequestBody?.Content.Keys; - return consumes?.Any() == true && + return consumes?.Count > 0 && consumes.Any(p => p.Contains("application/xml")) && consumes.Any(p => AppJsonRegex.IsMatch(p)) == false; } @@ -245,10 +245,10 @@ public bool IsBinaryBodyParameter } var parent = Parent as OpenApiOperation; - if (parent?.ActualConsumes?.Any() == true) + if (parent?.ActualConsumes?.Count > 0 == true) { var consumes = parent.ActualConsumes; - return consumes?.Any() == true && + return consumes?.Count > 0 && (Schema?.IsBinary != false || consumes.Contains("multipart/form-data")) && consumes?.Any(p => p.Contains("*/*")) == false && @@ -277,10 +277,10 @@ public bool HasBinaryBodyWithMultipleMimeTypes } var parent = Parent as OpenApiOperation; - if (parent?.ActualConsumes?.Any() == true) + if (parent?.ActualConsumes?.Count > 0) { var consumes = parent.ActualConsumes; - return consumes?.Any() == true && + return consumes?.Count > 0 && (consumes.Count > 1 || consumes.Any(p => p.Contains("*"))); } diff --git a/src/NSwag.Core/OpenApiPathItem.cs b/src/NSwag.Core/OpenApiPathItem.cs index d0e648adba..140851058c 100644 --- a/src/NSwag.Core/OpenApiPathItem.cs +++ b/src/NSwag.Core/OpenApiPathItem.cs @@ -132,13 +132,13 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s } } - if (operations.Parameters != null && operations.Parameters.Any()) + if (operations.Parameters != null && operations.Parameters.Count > 0) { writer.WritePropertyName("parameters"); serializer.Serialize(writer, operations.Parameters); } - if (operations.Servers != null && operations.Servers.Any()) + if (operations.Servers != null && operations.Servers.Count > 0) { writer.WritePropertyName("servers"); serializer.Serialize(writer, operations.Servers); diff --git a/src/NSwag.Generation.AspNetCore.Tests/Parameters/DefaultParametersTests.cs b/src/NSwag.Generation.AspNetCore.Tests/Parameters/DefaultParametersTests.cs index f2a78abe30..f7c7554d03 100644 --- a/src/NSwag.Generation.AspNetCore.Tests/Parameters/DefaultParametersTests.cs +++ b/src/NSwag.Generation.AspNetCore.Tests/Parameters/DefaultParametersTests.cs @@ -56,7 +56,7 @@ public async Task When_parameter_has_default_and_schema_type_is_OpenApi3_then_sc var operation = document.Operations.First(o => o.Path.Contains(nameof(DefaultParametersController.WithDefaultEnum))).Operation; Assert.Equal((int)MyEnum.Def, operation.Parameters.First().Schema.Default); - Assert.True(operation.Parameters.First().Schema.OneOf.Any()); + Assert.True(operation.Parameters[0].Schema.OneOf.Count > 0); } [Fact] diff --git a/src/NSwag.Generation.AspNetCore/AspNetCoreOpenApiDocumentGenerator.cs b/src/NSwag.Generation.AspNetCore/AspNetCoreOpenApiDocumentGenerator.cs index 62337c1263..b11b8d4e38 100644 --- a/src/NSwag.Generation.AspNetCore/AspNetCoreOpenApiDocumentGenerator.cs +++ b/src/NSwag.Generation.AspNetCore/AspNetCoreOpenApiDocumentGenerator.cs @@ -277,7 +277,7 @@ private List GenerateApiGroups( } var addedOperations = AddOperationDescriptionsToDocument(document, controllerType, operations, generator, schemaResolver); - if (addedOperations.Any() && apiGroup.Key != null) + if (addedOperations.Count > 0 && apiGroup.Key != null) { usedControllerTypes.Add(apiGroup.Key); } diff --git a/src/NSwag.Generation.AspNetCore/Processors/AspNetCoreOperationSecurityScopeProcessor.cs b/src/NSwag.Generation.AspNetCore/Processors/AspNetCoreOperationSecurityScopeProcessor.cs index b433f9d42f..4829f646f8 100644 --- a/src/NSwag.Generation.AspNetCore/Processors/AspNetCoreOperationSecurityScopeProcessor.cs +++ b/src/NSwag.Generation.AspNetCore/Processors/AspNetCoreOperationSecurityScopeProcessor.cs @@ -51,7 +51,7 @@ public bool Process(OperationProcessorContext context) } var authorizeAttributes = endpointMetadata.OfType().ToList(); - if (!authorizeAttributes.Any()) + if (authorizeAttributes.Count == 0) { return true; } diff --git a/src/NSwag.Generation.AspNetCore/Processors/OperationParameterProcessor.cs b/src/NSwag.Generation.AspNetCore/Processors/OperationParameterProcessor.cs index 349dceab07..42c62ad2a8 100644 --- a/src/NSwag.Generation.AspNetCore/Processors/OperationParameterProcessor.cs +++ b/src/NSwag.Generation.AspNetCore/Processors/OperationParameterProcessor.cs @@ -362,7 +362,7 @@ private bool IsFileArray(Type type, JsonTypeDescription typeInfo) return true; } - if (typeInfo.Type == JsonObjectType.Array && type.GenericTypeArguments.Any()) + if (typeInfo.Type == JsonObjectType.Array && type.GenericTypeArguments.Length > 0) { var description = _settings.SchemaSettings.ReflectionService.GetDescription(type.GenericTypeArguments[0].ToContextualType(), _settings.SchemaSettings); if (description.Type == JsonObjectType.File || description.Format == JsonFormatStrings.Binary) diff --git a/src/NSwag.Generation.WebApi/Processors/OperationParameterProcessor.cs b/src/NSwag.Generation.WebApi/Processors/OperationParameterProcessor.cs index 09b69b2d62..7643061f29 100644 --- a/src/NSwag.Generation.WebApi/Processors/OperationParameterProcessor.cs +++ b/src/NSwag.Generation.WebApi/Processors/OperationParameterProcessor.cs @@ -337,7 +337,7 @@ private bool IsFileArray(Type type, JsonTypeDescription typeInfo) return true; } - if (typeInfo.Type == JsonObjectType.Array && type.GenericTypeArguments.Any()) + if (typeInfo.Type == JsonObjectType.Array && type.GenericTypeArguments.Length > 0) { var description = _settings.SchemaSettings.ReflectionService.GetDescription(type.GenericTypeArguments[0].ToContextualType(), _settings.SchemaSettings); if (description.Type == JsonObjectType.File || diff --git a/src/NSwag.Generation.WebApi/WebApiOpenApiDocumentGenerator.cs b/src/NSwag.Generation.WebApi/WebApiOpenApiDocumentGenerator.cs index feff0bf4ee..d65346fa02 100644 --- a/src/NSwag.Generation.WebApi/WebApiOpenApiDocumentGenerator.cs +++ b/src/NSwag.Generation.WebApi/WebApiOpenApiDocumentGenerator.cs @@ -354,7 +354,7 @@ private List GetHttpPaths(Type controllerType, MethodInfo method) var routeAttributesOnClass = GetAllRouteAttributes(controllerType); var routePrefixAttribute = GetRoutePrefixAttribute(controllerType); - if (routeAttributes.Any()) + if (routeAttributes.Count > 0) { foreach (var attribute in routeAttributes) { diff --git a/src/NSwag.Generation/Processors/ApiVersionProcessor.cs b/src/NSwag.Generation/Processors/ApiVersionProcessor.cs index 57e3f1dfd5..af6af4e459 100644 --- a/src/NSwag.Generation/Processors/ApiVersionProcessor.cs +++ b/src/NSwag.Generation/Processors/ApiVersionProcessor.cs @@ -45,7 +45,7 @@ public bool Process(OperationProcessorContext context) else { var versions = GetVersions(context, "ApiVersionAttribute"); - if (versions.Any()) + if (versions.Length > 0) { if (versions.Any(v => IncludedVersions == null || IncludedVersions.Length == 0 || IncludedVersions.Contains(v))) { @@ -54,7 +54,7 @@ public bool Process(OperationProcessorContext context) var version = mappedVersions.FirstOrDefault(v => IncludedVersions == null || IncludedVersions.Length == 0 || IncludedVersions.Contains(v)); if (version == null && mappedVersions.Length == 0) { - version = IncludedVersions != null && IncludedVersions.Any() ? IncludedVersions[0] : versions[0]; + version = IncludedVersions != null && IncludedVersions.Length > 0 ? IncludedVersions[0] : versions[0]; } if (version != null) diff --git a/src/NSwag.Generation/Processors/DocumentTagsProcessor.cs b/src/NSwag.Generation/Processors/DocumentTagsProcessor.cs index 6a01591ccd..a3c57724d9 100644 --- a/src/NSwag.Generation/Processors/DocumentTagsProcessor.cs +++ b/src/NSwag.Generation/Processors/DocumentTagsProcessor.cs @@ -42,7 +42,7 @@ private static void ProcessTagsAttribute(OpenApiDocument document, Type controll .Select(t => new OpenApiTag { Name = t }) .ToList(); - if (tags.Any()) + if (tags.Count > 0) { if (document.Tags == null) { @@ -69,12 +69,9 @@ private static void ProcessTagAttributes(OpenApiDocument document, Type controll .Select(a => (dynamic)a) .ToArray(); - if (tagAttributes.Any()) + foreach (var tagAttribute in tagAttributes) { - foreach (var tagAttribute in tagAttributes) - { - ProcessTagAttribute(document, tagAttribute); - } + ProcessTagAttribute(document, tagAttribute); } } diff --git a/src/NSwag.Generation/Processors/OperationTagsProcessor.cs b/src/NSwag.Generation/Processors/OperationTagsProcessor.cs index 961cfc20fd..314210672d 100644 --- a/src/NSwag.Generation/Processors/OperationTagsProcessor.cs +++ b/src/NSwag.Generation/Processors/OperationTagsProcessor.cs @@ -32,7 +32,7 @@ public virtual bool Process(OperationProcessorContext context) if (context.ControllerType != null) { - if (!context.OperationDescription.Operation.Tags.Any()) + if (context.OperationDescription.Operation.Tags.Count == 0) { var typeInfo = context.ControllerType.GetTypeInfo(); @@ -40,7 +40,7 @@ public virtual bool Process(OperationProcessorContext context) ProcessControllerSwaggerTagAttributes(context.OperationDescription, typeInfo); } - if (!context.OperationDescription.Operation.Tags.Any()) + if (context.OperationDescription.Operation.Tags.Count == 0) { AddControllerNameTag(context); } diff --git a/src/NSwag.Generation/Processors/Security/OperationSecurityScopeProcessor.cs b/src/NSwag.Generation/Processors/Security/OperationSecurityScopeProcessor.cs index 667d9efe80..f3a15ebe4c 100644 --- a/src/NSwag.Generation/Processors/Security/OperationSecurityScopeProcessor.cs +++ b/src/NSwag.Generation/Processors/Security/OperationSecurityScopeProcessor.cs @@ -59,9 +59,9 @@ protected virtual IEnumerable GetScopes(OpenApiOperationDescription oper methodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes()); var authorizeAttributes = allAttributes.Where(a => a.GetType().Name == "AuthorizeAttribute").ToList(); - if (!authorizeAttributes.Any()) + if (authorizeAttributes.Count == 0) { - return Enumerable.Empty(); + return []; } return authorizeAttributes