Skip to content

Commit

Permalink
Use TryGetValue() for dictionaries (#2822)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Apr 23, 2024
1 parent f0c94fd commit c9b7741
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public static class OpenApiDocumentExtensions
{
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");
Expand Down
8 changes: 4 additions & 4 deletions src/Swashbuckle.AspNetCore.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ public static int Main(string[] args)
var swaggerProvider = serviceProvider.GetRequiredService<ISwaggerProvider>();
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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,9 @@ private IList<OpenApiParameter> GenerateParameters(ApiDescription apiDescription
? 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
}

0 comments on commit c9b7741

Please sign in to comment.