From 09f4554218e3099dc13265bcf18472b29fd3dd48 Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Fri, 22 Nov 2024 20:34:01 +0200 Subject: [PATCH] Use pattern matching --- Directory.Build.props | 4 +--- .../Models/CSharpFileTemplateModel.cs | 8 ++------ .../Models/TypeScriptFrameworkModel.cs | 8 +++----- .../Models/TypeScriptOperationModel.cs | 6 ++---- .../TypeScriptClientGenerator.cs | 5 +++-- src/NSwag.CodeGeneration/Models/OperationModelBase.cs | 9 +++------ src/NSwag.Commands/NSwagDocument.cs | 7 +++---- .../Processors/OperationResponseProcessor.cs | 2 +- src/NSwag.Generation/GenericResultWrapperTypes.cs | 8 ++------ src/NSwag.Generation/OpenApiDocumentGenerator.cs | 4 ++-- 10 files changed, 22 insertions(+), 39 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 1dd5c95b90..402a9ecdc2 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -69,8 +69,6 @@ [IDE0060] Remove unused parameter [IDE0061] Use block body for local function [IDE0074] Use compound assignment - [IDE0078] Use pattern matching - [IDE0083] Use pattern matching [IDE0090] 'new' expression can be simplified [IDE0130] Namespace does not match folder structure [IDE0160] Convert to block scoped namespace @@ -105,7 +103,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;IDE0074;IDE0078;IDE0083;IDE0090;IDE0130;IDE0160;IDE0200;IDE0260;IDE0270;IDE0290;IDE0330 + $(NoWarn);IDE0005;IDE0008;IDE0011;IDE0017;IDE0019;IDE0021;IDE0022;IDE0025;IDE0027;IDE0028;IDE0029;IDE0032;IDE0039;IDE0045;IDE0046;IDE0055;IDE0056;IDE0057;IDE0059;IDE0060;IDE0061;IDE0074;IDE0090;IDE0130;IDE0160;IDE0200;IDE0260;IDE0270;IDE0290;IDE0330 $(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/Models/CSharpFileTemplateModel.cs b/src/NSwag.CodeGeneration.CSharp/Models/CSharpFileTemplateModel.cs index 04e51e9db0..a210100825 100644 --- a/src/NSwag.CodeGeneration.CSharp/Models/CSharpFileTemplateModel.cs +++ b/src/NSwag.CodeGeneration.CSharp/Models/CSharpFileTemplateModel.cs @@ -60,14 +60,10 @@ public CSharpFileTemplateModel( public bool GenerateNullableReferenceTypes => _settings.CSharpGeneratorSettings.GenerateNullableReferenceTypes; /// Gets a value indicating whether to generate contract code. - public bool GenerateContracts => - _outputType == ClientGeneratorOutputType.Full || - _outputType == ClientGeneratorOutputType.Contracts; + public bool GenerateContracts => _outputType is ClientGeneratorOutputType.Full or ClientGeneratorOutputType.Contracts; /// Gets a value indicating whether to generate implementation code. - public bool GenerateImplementation => - _outputType == ClientGeneratorOutputType.Full || - _outputType == ClientGeneratorOutputType.Implementation; + public bool GenerateImplementation => _outputType is ClientGeneratorOutputType.Full or ClientGeneratorOutputType.Implementation; /// Gets or sets a value indicating whether to generate client types. public bool GenerateClientClasses => _settings.GenerateClientClasses; diff --git a/src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptFrameworkModel.cs b/src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptFrameworkModel.cs index 5f1f8e4f8a..760eb38958 100644 --- a/src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptFrameworkModel.cs +++ b/src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptFrameworkModel.cs @@ -36,18 +36,16 @@ internal TypeScriptFrameworkModel(TypeScriptClientGeneratorSettings settings) public bool IsKnockout => _settings.TypeScriptGeneratorSettings.TypeStyle == TypeScriptTypeStyle.KnockoutClass; /// Gets a value indicating whether to render for JQuery. - public bool IsJQuery => _settings.Template == TypeScriptTemplate.JQueryCallbacks || _settings.Template == TypeScriptTemplate.JQueryPromises; + public bool IsJQuery => _settings.Template is TypeScriptTemplate.JQueryCallbacks or TypeScriptTemplate.JQueryPromises; /// Gets a value indicating whether to render for Fetch or Aurelia - public bool IsFetchOrAurelia => _settings.Template == TypeScriptTemplate.Fetch || - _settings.Template == TypeScriptTemplate.Aurelia; + public bool IsFetchOrAurelia => _settings.Template is TypeScriptTemplate.Fetch or TypeScriptTemplate.Aurelia; /// Gets a value indicating whether to render for Axios. public bool IsAxios => _settings.Template == TypeScriptTemplate.Axios; /// Gets a value indicating whether MomentJS is required. - public bool UseMomentJS => _settings.TypeScriptGeneratorSettings.DateTimeType == TypeScriptDateTimeType.MomentJS || - _settings.TypeScriptGeneratorSettings.DateTimeType == TypeScriptDateTimeType.OffsetMomentJS; + public bool UseMomentJS => _settings.TypeScriptGeneratorSettings.DateTimeType is TypeScriptDateTimeType.MomentJS or TypeScriptDateTimeType.OffsetMomentJS; /// Gets a value indicating whether DayJS is required. public bool UseDayJS => _settings.TypeScriptGeneratorSettings.DateTimeType == TypeScriptDateTimeType.DayJS; diff --git a/src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptOperationModel.cs b/src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptOperationModel.cs index 1354bdfc58..d277ab401c 100644 --- a/src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptOperationModel.cs +++ b/src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptOperationModel.cs @@ -105,12 +105,10 @@ public override string ResultType public bool IsAngular => _settings.Template == TypeScriptTemplate.Angular; /// Gets a value indicating whether to render for JQuery. - public bool IsJQuery => _settings.Template == TypeScriptTemplate.JQueryCallbacks || - _settings.Template == TypeScriptTemplate.JQueryPromises; + public bool IsJQuery => _settings.Template is TypeScriptTemplate.JQueryCallbacks or TypeScriptTemplate.JQueryPromises; /// Gets a value indicating whether to render for Fetch or Aurelia - public bool IsFetchOrAurelia => _settings.Template == TypeScriptTemplate.Fetch || - _settings.Template == TypeScriptTemplate.Aurelia; + public bool IsFetchOrAurelia => _settings.Template is TypeScriptTemplate.Fetch or TypeScriptTemplate.Aurelia; /// Gets a value indicating whether to use HttpClient with the Angular template. public bool UseAngularHttpClient => IsAngular && _settings.HttpClass == HttpClass.HttpClient; diff --git a/src/NSwag.CodeGeneration.TypeScript/TypeScriptClientGenerator.cs b/src/NSwag.CodeGeneration.TypeScript/TypeScriptClientGenerator.cs index f7eea46c09..f12b23849d 100644 --- a/src/NSwag.CodeGeneration.TypeScript/TypeScriptClientGenerator.cs +++ b/src/NSwag.CodeGeneration.TypeScript/TypeScriptClientGenerator.cs @@ -79,8 +79,9 @@ public override string GetTypeName(JsonSchema schema, bool isNullable, string ty /// The type name. public override string GetBinaryResponseTypeName() { - return Settings.Template != TypeScriptTemplate.JQueryCallbacks && - Settings.Template != TypeScriptTemplate.JQueryPromises ? "FileResponse" : "any"; + return Settings.Template is not TypeScriptTemplate.JQueryCallbacks and not TypeScriptTemplate.JQueryPromises + ? "FileResponse" + : "any"; } /// Generates the file. diff --git a/src/NSwag.CodeGeneration/Models/OperationModelBase.cs b/src/NSwag.CodeGeneration/Models/OperationModelBase.cs index 235d0c596b..f8a2d14845 100644 --- a/src/NSwag.CodeGeneration/Models/OperationModelBase.cs +++ b/src/NSwag.CodeGeneration/Models/OperationModelBase.cs @@ -85,13 +85,10 @@ protected abstract TResponseModel CreateResponseModel(OpenApiOperation operation public string HttpMethodLower => ConversionUtilities.ConvertToLowerCamelCase(HttpMethod.ToString(), false); /// Gets a value indicating whether the HTTP method is GET or DELETE or HEAD. - public bool IsGetOrDeleteOrHead => - HttpMethod == OpenApiOperationMethod.Get || - HttpMethod == OpenApiOperationMethod.Delete || - HttpMethod == OpenApiOperationMethod.Head; + public bool IsGetOrDeleteOrHead => HttpMethod is OpenApiOperationMethod.Get or OpenApiOperationMethod.Delete or OpenApiOperationMethod.Head; /// Gets a value indicating whether the HTTP method is GET or HEAD. - public bool IsGetOrHead => HttpMethod == OpenApiOperationMethod.Get || HttpMethod == OpenApiOperationMethod.Head; + public bool IsGetOrHead => HttpMethod is OpenApiOperationMethod.Get or OpenApiOperationMethod.Head; // TODO: Remove this (may not work correctly) /// Gets or sets a value indicating whether the operation has a result type (i.e. not void). @@ -196,7 +193,7 @@ public TParameterModel ContentParameter public IEnumerable PathParameters => Parameters.Where(p => p.Kind == OpenApiParameterKind.Path); /// Gets the query parameters. - public IEnumerable QueryParameters => Parameters.Where(p => p.Kind == OpenApiParameterKind.Query || p.Kind == OpenApiParameterKind.ModelBinding); + public IEnumerable QueryParameters => Parameters.Where(p => p.Kind is OpenApiParameterKind.Query or OpenApiParameterKind.ModelBinding); /// Gets a value indicating whether the operation has query parameters. public bool HasQueryParameters => QueryParameters.Any(); diff --git a/src/NSwag.Commands/NSwagDocument.cs b/src/NSwag.Commands/NSwagDocument.cs index 5d16347c38..c024fdafd3 100644 --- a/src/NSwag.Commands/NSwagDocument.cs +++ b/src/NSwag.Commands/NSwagDocument.cs @@ -255,17 +255,16 @@ private string GetProgramName() #pragma warning restore CA1822 { #if NET462 - var runtime = Runtime != Runtime.Default ? Runtime : RuntimeUtilities.CurrentRuntime; - if (runtime == Runtime.WinX64 || runtime == Runtime.Debug) + if (runtime is Runtime.WinX64 or Runtime.Debug) { return System.IO.Path.Combine(RootBinaryDirectory, "Win/nswag.exe"); } - else if (runtime == Runtime.WinX86) + + if (runtime == Runtime.WinX86) { return System.IO.Path.Combine(RootBinaryDirectory, "Win/nswag.x86.exe"); } - else #endif return "dotnet"; } diff --git a/src/NSwag.Generation.AspNetCore/Processors/OperationResponseProcessor.cs b/src/NSwag.Generation.AspNetCore/Processors/OperationResponseProcessor.cs index 69050569fd..63befcf6ae 100644 --- a/src/NSwag.Generation.AspNetCore/Processors/OperationResponseProcessor.cs +++ b/src/NSwag.Generation.AspNetCore/Processors/OperationResponseProcessor.cs @@ -33,7 +33,7 @@ public OperationResponseProcessor(AspNetCoreOpenApiDocumentGeneratorSettings set /// true if the operation should be added to the Swagger specification. public bool Process(OperationProcessorContext operationProcessorContext) { - if (!(operationProcessorContext is AspNetCoreOperationProcessorContext context)) + if (operationProcessorContext is not AspNetCoreOperationProcessorContext context) { return false; } diff --git a/src/NSwag.Generation/GenericResultWrapperTypes.cs b/src/NSwag.Generation/GenericResultWrapperTypes.cs index 33eaccf27b..7dc170906e 100644 --- a/src/NSwag.Generation/GenericResultWrapperTypes.cs +++ b/src/NSwag.Generation/GenericResultWrapperTypes.cs @@ -2,11 +2,7 @@ { internal static class GenericResultWrapperTypes { - internal static bool IsGenericWrapperType(string typeName) => - typeName == "Task`1" || - typeName == "ValueTask`1" || - typeName == "JsonResult`1" || - typeName == "ActionResult`1"; + private static bool IsGenericWrapperType(string typeName) => typeName is "Task`1" or "ValueTask`1" or "JsonResult`1" or "ActionResult`1"; internal static T RemoveGenericWrapperTypes(T type, Func getName, Func unwrap) { @@ -19,4 +15,4 @@ internal static T RemoveGenericWrapperTypes(T type, Func getName, return type; } } -} +} \ No newline at end of file diff --git a/src/NSwag.Generation/OpenApiDocumentGenerator.cs b/src/NSwag.Generation/OpenApiDocumentGenerator.cs index 7cf74c17c3..e1c9a5055b 100644 --- a/src/NSwag.Generation/OpenApiDocumentGenerator.cs +++ b/src/NSwag.Generation/OpenApiDocumentGenerator.cs @@ -57,11 +57,11 @@ public OpenApiParameter CreateUntypedPathParameter(string parameterName, string parameter.Type = JsonObjectType.String; parameter.Format = JsonFormatStrings.Guid; } - else if (parameterType == "int" || parameterType == "integer" || parameterType == "short" || parameterType == "long") + else if (parameterType is "int" or "integer" or "short" or "long") { parameter.Type = JsonObjectType.Integer; } - else if (parameterType == "number" || parameterType == "decimal" || parameterType == "double") + else if (parameterType is "number" or "decimal" or "double") { parameter.Type = JsonObjectType.Number; }