From c9b774165c36ba4e3878708f81e0c6b9918c1818 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Tue, 23 Apr 2024 17:39:58 +1000 Subject: [PATCH] Use TryGetValue() for dictionaries (#2822) --- .../OpenApiDocumentExtensions.cs | 4 ++-- src/Swashbuckle.AspNetCore.Cli/Program.cs | 8 ++++---- .../SchemaGenerator/NewtonsoftDataContractResolver.cs | 4 ++-- .../SchemaGenerator/JsonSerializerDataContractResolver.cs | 8 +++----- .../SwaggerGenerator/SwaggerGenerator.cs | 5 +++-- .../XmlComments/XmlCommentsOperationFilter.cs | 6 +++--- 6 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/Swashbuckle.AspNetCore.ApiTesting/OpenApiDocumentExtensions.cs b/src/Swashbuckle.AspNetCore.ApiTesting/OpenApiDocumentExtensions.cs index 350677e400..4385c6b866 100644 --- a/src/Swashbuckle.AspNetCore.ApiTesting/OpenApiDocumentExtensions.cs +++ b/src/Swashbuckle.AspNetCore.ApiTesting/OpenApiDocumentExtensions.cs @@ -39,8 +39,8 @@ internal static OpenApiOperation GetOperationByPathAndType( { if (openApiDocument.Paths.TryGetValue(pathTemplate, out pathSpec)) { - if (pathSpec.Operations.ContainsKey(operationType)) - return pathSpec.Operations[operationType]; + if (pathSpec.Operations.TryGetValue(operationType, out var type)) + return type; } throw new InvalidOperationException($"Operation with path '{pathTemplate}' and type '{operationType}' not found"); diff --git a/src/Swashbuckle.AspNetCore.Cli/Program.cs b/src/Swashbuckle.AspNetCore.Cli/Program.cs index 0a84c091b2..62b4dd162a 100644 --- a/src/Swashbuckle.AspNetCore.Cli/Program.cs +++ b/src/Swashbuckle.AspNetCore.Cli/Program.cs @@ -89,12 +89,12 @@ public static int Main(string[] args) var swaggerProvider = serviceProvider.GetRequiredService(); var swagger = swaggerProvider.GetSwagger( namedArgs["swaggerdoc"], - namedArgs.ContainsKey("--host") ? namedArgs["--host"] : null, - namedArgs.ContainsKey("--basepath") ? namedArgs["--basepath"] : null); + namedArgs.TryGetValue("--host", out var arg) ? arg : null, + namedArgs.TryGetValue("--basepath", out var namedArg) ? namedArg : null); // 4) Serialize to specified output location or stdout - var outputPath = namedArgs.ContainsKey("--output") - ? Path.Combine(Directory.GetCurrentDirectory(), namedArgs["--output"]) + var outputPath = namedArgs.TryGetValue("--output", out var arg1) + ? Path.Combine(Directory.GetCurrentDirectory(), arg1) : null; using (var streamWriter = (outputPath != null ? File.CreateText(outputPath) : Console.Out)) diff --git a/src/Swashbuckle.AspNetCore.Newtonsoft/SchemaGenerator/NewtonsoftDataContractResolver.cs b/src/Swashbuckle.AspNetCore.Newtonsoft/SchemaGenerator/NewtonsoftDataContractResolver.cs index 3dc2bd55eb..fac6ff812c 100644 --- a/src/Swashbuckle.AspNetCore.Newtonsoft/SchemaGenerator/NewtonsoftDataContractResolver.cs +++ b/src/Swashbuckle.AspNetCore.Newtonsoft/SchemaGenerator/NewtonsoftDataContractResolver.cs @@ -33,8 +33,8 @@ public DataContract GetDataContractForType(Type type) if (jsonContract is JsonPrimitiveContract && !jsonContract.UnderlyingType.IsEnum) { - var primitiveTypeAndFormat = PrimitiveTypesAndFormats.ContainsKey(jsonContract.UnderlyingType) - ? PrimitiveTypesAndFormats[jsonContract.UnderlyingType] + var primitiveTypeAndFormat = PrimitiveTypesAndFormats.TryGetValue(jsonContract.UnderlyingType, out var format) + ? format : Tuple.Create(DataType.String, (string)null); return DataContract.ForPrimitive( diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/JsonSerializerDataContractResolver.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/JsonSerializerDataContractResolver.cs index 86c2255f57..18267a63c6 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/JsonSerializerDataContractResolver.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/JsonSerializerDataContractResolver.cs @@ -27,14 +27,12 @@ public DataContract GetDataContractForType(Type type) jsonConverter: JsonConverterFunc); } - if (PrimitiveTypesAndFormats.ContainsKey(type)) + if (PrimitiveTypesAndFormats.TryGetValue(type, out var primitiveTypeAndFormat1)) { - var primitiveTypeAndFormat = PrimitiveTypesAndFormats[type]; - return DataContract.ForPrimitive( underlyingType: type, - dataType: primitiveTypeAndFormat.Item1, - dataFormat: primitiveTypeAndFormat.Item2, + dataType: primitiveTypeAndFormat1.Item1, + dataFormat: primitiveTypeAndFormat1.Item2, jsonConverter: JsonConverterFunc); } diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs index 15e4099024..7baef87025 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs @@ -342,8 +342,9 @@ private OpenApiParameter GenerateParameter( ? apiParameter.Name.ToCamelCase() : apiParameter.Name; - var location = (apiParameter.Source != null && ParameterLocationMap.ContainsKey(apiParameter.Source)) - ? ParameterLocationMap[apiParameter.Source] + var location = apiParameter.Source != null && + ParameterLocationMap.TryGetValue(apiParameter.Source, out var value) + ? value : ParameterLocation.Query; var isRequired = apiParameter.IsRequiredParameter(); diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsOperationFilter.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsOperationFilter.cs index 46522e0057..5af72d42c1 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsOperationFilter.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsOperationFilter.cs @@ -60,12 +60,12 @@ private void ApplyResponseTags(OpenApiOperation operation, XPathNodeIterator res while (responseNodes.MoveNext()) { var code = responseNodes.Current.GetAttribute("code", ""); - var response = operation.Responses.ContainsKey(code) - ? operation.Responses[code] + var response = operation.Responses.TryGetValue(code, out var operationResponse) + ? operationResponse : operation.Responses[code] = new OpenApiResponse(); response.Description = XmlCommentsTextHelper.Humanize(responseNodes.Current.InnerXml); } } } -} \ No newline at end of file +}