Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove some duplicate dictionary lookups #2822

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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");
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 @@ -338,8 +338,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();
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);
}
}
}
}
}