Skip to content

Commit

Permalink
Use TryGetValue() and Any() when possible (#5042)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Nov 22, 2024
1 parent 0a6a186 commit 705dad9
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 18 deletions.
4 changes: 1 addition & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -112,7 +110,7 @@
[SYSLIB0012] 'Assembly.CodeBase' is obsolete
-->
<NoWarn>$(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>
<NoWarn>$(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>
<NoWarn>$(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</NoWarn>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected override IEnumerable<CodeArtifact> 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));
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.CodeGeneration.CSharp/CSharpGeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public override string GetBinaryResponseTypeName()
/// <param name="document">The document </param>
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ public IEnumerable<string> ResponseClassNames
public string ExtensionCodeImport => _extensionCode.ImportCode;

/// <summary>Gets or sets the extension code to insert at the beginning.</summary>
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;

/// <summary>Gets or sets the extension code to insert at the end.</summary>
public string ExtensionCodeBottom { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions src/NSwag.Generation.WebApi/WebApiOpenApiDocumentGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
}
Expand Down

0 comments on commit 705dad9

Please sign in to comment.