From 705dad9f3af463d58800ea9c8411c3a4324c2edc Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Fri, 22 Nov 2024 19:55:10 +0200 Subject: [PATCH] Use TryGetValue() and Any() when possible (#5042) --- Directory.Build.props | 4 +--- .../CSharpControllerGenerator.cs | 2 +- .../CSharpGeneratorBase.cs | 2 +- .../Models/TypeScriptFileTemplateModel.cs | 6 +++--- .../Models/TypeScriptOperationModel.cs | 2 +- .../Processors/OperationParameterProcessor.cs | 11 ++++++----- .../WebApiOpenApiDocumentGenerator.cs | 9 +++++---- 7 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 8e1db1d2f2..17352d6f83 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -96,11 +96,9 @@ [CA1720] Identifier 'xxx' contains type name [CA1725] Overriden parameter name mismatch [CA1805] Member is explicitly initialized to its default value - [CA1827] Count() is used where Any() could be used instead to improve performance [CA1834] Use 'StringBuilder.Append(char)' instead of 'StringBuilder.Append(string)' when the input is a constant unit string [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 [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 @@ -112,7 +110,7 @@ [SYSLIB0012] 'Assembly.CodeBase' is obsolete --> $(NoWarn);IDE0005;IDE0008;IDE0011;IDE0017;IDE0019;IDE0021;IDE0022;IDE0025;IDE0027;IDE0028;IDE0029;IDE0032;IDE0039;IDE0045;IDE0046;IDE0055;IDE0056;IDE0057;IDE0059;IDE0060;IDE0061;IDE0063;IDE0074;IDE0078;IDE0083;IDE0090;IDE0130;IDE0160;IDE0200;IDE0260;IDE0270;IDE0290;IDE0300;IDE0305;IDE0301;IDE0330 - $(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 + $(NoWarn);CA1200;CA1304;CA1305;CA1310;CA1311;CA1507;CA1510;CA1514;CA1710;CA1716;CA1720;CA1725;CA1805;CA1834;CA1845;CA1847;CA1861;CA1862;CA1865;CA1866;CA1870;CA2249;CA2263;SYSLIB0012 diff --git a/src/NSwag.CodeGeneration.CSharp/CSharpControllerGenerator.cs b/src/NSwag.CodeGeneration.CSharp/CSharpControllerGenerator.cs index d41a7d9fd7..8d965ca59e 100644 --- a/src/NSwag.CodeGeneration.CSharp/CSharpControllerGenerator.cs +++ b/src/NSwag.CodeGeneration.CSharp/CSharpControllerGenerator.cs @@ -51,7 +51,7 @@ protected override IEnumerable GenerateAllClientTypes() var artifacts = base.GenerateAllClientTypes().ToList(); if (Settings.ControllerTarget == CSharpControllerTarget.AspNet && - _document.Operations.Count(operation => operation.Operation.ActualParameters.Any(p => p.Kind == OpenApiParameterKind.Header)) > 0) + _document.Operations.Any(operation => operation.Operation.ActualParameters.Any(p => p.Kind == OpenApiParameterKind.Header))) { var template = Settings.CodeGeneratorSettings.TemplateFactory.CreateTemplate("CSharp", "Controller.AspNet.FromHeaderAttribute", new object()); artifacts.Add(new CodeArtifact("FromHeaderAttribute", CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Utility, template)); diff --git a/src/NSwag.CodeGeneration.CSharp/CSharpGeneratorBase.cs b/src/NSwag.CodeGeneration.CSharp/CSharpGeneratorBase.cs index 6fe56d6a5a..0d2f6cf905 100644 --- a/src/NSwag.CodeGeneration.CSharp/CSharpGeneratorBase.cs +++ b/src/NSwag.CodeGeneration.CSharp/CSharpGeneratorBase.cs @@ -74,7 +74,7 @@ public override string GetBinaryResponseTypeName() /// The document public static CSharpTypeResolver CreateResolverWithExceptionSchema(CSharpGeneratorSettings settings, OpenApiDocument document) { - var exceptionSchema = document.Definitions.ContainsKey("Exception") ? document.Definitions["Exception"] : null; + var exceptionSchema = document.Definitions.TryGetValue("Exception", out JsonSchema value) ? value : null; var resolver = new CSharpTypeResolver(settings, exceptionSchema); resolver.RegisterSchemaDefinitions(document.Definitions diff --git a/src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptFileTemplateModel.cs b/src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptFileTemplateModel.cs index 06927a870e..24285d90c5 100644 --- a/src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptFileTemplateModel.cs +++ b/src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptFileTemplateModel.cs @@ -104,9 +104,9 @@ public IEnumerable ResponseClassNames public string ExtensionCodeImport => _extensionCode.ImportCode; /// Gets or sets the extension code to insert at the beginning. - public string ExtensionCodeTop => _settings.ConfigurationClass != null && _extensionCode.ExtensionClasses.ContainsKey(_settings.ConfigurationClass) ? - _extensionCode.ExtensionClasses[_settings.ConfigurationClass] + "\n\n" + _extensionCode.TopCode : - _extensionCode.TopCode; + public string ExtensionCodeTop => _settings.ConfigurationClass != null && _extensionCode.ExtensionClasses.TryGetValue(_settings.ConfigurationClass, out string value) + ? value + "\n\n" + _extensionCode.TopCode + : _extensionCode.TopCode; /// Gets or sets the extension code to insert at the end. public string ExtensionCodeBottom { get; } diff --git a/src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptOperationModel.cs b/src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptOperationModel.cs index 4469e4cc09..02fcac12e6 100644 --- a/src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptOperationModel.cs +++ b/src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptOperationModel.cs @@ -123,7 +123,7 @@ public override string ExceptionType { get { - if (_operation.ActualResponses.Count(r => !HttpUtilities.IsSuccessStatusCode(r.Key)) == 0) + if (_operation.ActualResponses.All(r => HttpUtilities.IsSuccessStatusCode(r.Key))) { return "string"; } diff --git a/src/NSwag.Generation.AspNetCore/Processors/OperationParameterProcessor.cs b/src/NSwag.Generation.AspNetCore/Processors/OperationParameterProcessor.cs index e96ac0de78..3049c67098 100644 --- a/src/NSwag.Generation.AspNetCore/Processors/OperationParameterProcessor.cs +++ b/src/NSwag.Generation.AspNetCore/Processors/OperationParameterProcessor.cs @@ -329,20 +329,21 @@ private static JsonSchema CreateOrGetFormDataSchema(OperationProcessorContext co } var requestBody = context.OperationDescription.Operation.RequestBody; - if (!requestBody.Content.ContainsKey(MultipartFormData)) + if (!requestBody.Content.TryGetValue(MultipartFormData, out OpenApiMediaType value)) { - requestBody.Content[MultipartFormData] = new OpenApiMediaType + value = new OpenApiMediaType { Schema = new JsonSchema() }; + requestBody.Content[MultipartFormData] = value; } - if (requestBody.Content[MultipartFormData].Schema == null) + if (value.Schema == null) { - requestBody.Content[MultipartFormData].Schema = new JsonSchema(); + value.Schema = new JsonSchema(); } - return requestBody.Content[MultipartFormData].Schema; + return value.Schema; } private static JsonSchemaProperty CreateFormDataProperty(OperationProcessorContext context, ExtendedApiParameterDescription extendedApiParameter, JsonSchema schema) diff --git a/src/NSwag.Generation.WebApi/WebApiOpenApiDocumentGenerator.cs b/src/NSwag.Generation.WebApi/WebApiOpenApiDocumentGenerator.cs index ee2f0b47a3..ec94c92147 100644 --- a/src/NSwag.Generation.WebApi/WebApiOpenApiDocumentGenerator.cs +++ b/src/NSwag.Generation.WebApi/WebApiOpenApiDocumentGenerator.cs @@ -216,18 +216,19 @@ private bool AddOperationDescriptionsToDocument(OpenApiDocument document, Type c { var path = operation.Path.Replace("//", "/"); - if (!document.Paths.ContainsKey(path)) + if (!document.Paths.TryGetValue(path, out OpenApiPathItem value)) { - document.Paths[path] = new OpenApiPathItem(); + value = new OpenApiPathItem(); + document.Paths[path] = value; } - if (document.Paths[path].ContainsKey(operation.Method)) + if (value.ContainsKey(operation.Method)) { throw new InvalidOperationException("The method '" + operation.Method + "' on path '" + path + "' is registered multiple times " + "(check the DefaultUrlTemplate setting [default for Web API: 'api/{controller}/{id}'; for MVC projects: '{controller}/{action}/{id?}'])."); } - document.Paths[path][operation.Method] = operation.Operation; + value[operation.Method] = operation.Operation; addedOperations++; } }