From 8ff1c5d0049768f5fad13c487da7404face66069 Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Fri, 22 Nov 2024 21:26:04 +0200 Subject: [PATCH] Simplify collection initialization and use pattern matching (#5047) --- Directory.Build.props | 4 +--- .../JsonExceptionFilterAttribute.cs | 2 +- .../JsonExceptionFilterAttribute.cs | 2 +- .../Middlewares/OpenApiDocumentMiddleware.cs | 3 +-- src/NSwag.AspNetCore/OAuth2ClientSettings.cs | 2 +- src/NSwag.AspNetCore/SwaggerUiSettings.cs | 2 +- .../ControllerGenerationFormatTests.cs | 2 +- .../FormParameterTests.cs | 2 +- .../Models/CSharpFileTemplateModel.cs | 2 +- .../Models/CSharpOperationModel.cs | 2 +- .../CodeGenerationTests.cs | 14 ++++++++------ .../OperationParameterTests.cs | 2 +- .../JsonSchemaExtensions.cs | 2 +- src/NSwag.Commands/NSwagDocumentBase.cs | 6 +++--- .../OpenApiDocumentExecutionResult.cs | 2 +- src/NSwag.Core.Tests/SwaggerResponseTests.cs | 2 +- .../Collections/ObservableDictionary.cs | 2 +- .../OpenApiDocument.Serialization.cs | 18 +++++++++--------- src/NSwag.Core/OpenApiDocument.cs | 6 +++--- src/NSwag.Core/OpenApiEncoding.cs | 2 +- src/NSwag.Core/OpenApiOperation.cs | 6 +++--- src/NSwag.Core/OpenApiPathItem.cs | 4 ++-- src/NSwag.Core/OpenApiResponse.cs | 2 +- src/NSwag.Core/OpenApiServerVariable.cs | 2 +- .../OperationResponseProcessorTest.cs | 2 +- .../AspNetCoreOpenApiDocumentGenerator.cs | 2 +- ...spNetCoreOperationSecurityScopeProcessor.cs | 2 +- .../WebApiOpenApiDocumentGenerator.cs | 8 ++++---- .../OpenApiDocumentGeneratorSettings.cs | 16 ++++++++-------- .../Processors/DocumentTagsProcessor.cs | 2 +- .../Processors/OperationTagsProcessor.cs | 2 +- .../OperationSecurityScopeProcessor.cs | 2 +- .../Security/SecurityDefinitionAppender.cs | 2 +- 33 files changed, 65 insertions(+), 66 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 85657cbeba..6167325428 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -55,7 +55,6 @@ [IDE0022] Use block body for method [IDE0025] Use expression body for property [IDE0027] Use expression body for accessor - [IDE0028] Collection initialization can be simplified [IDE0029] Null check can be simplified [IDE0032] Use auto property [IDE0039] Use local function @@ -71,7 +70,6 @@ [IDE0130] Namespace does not match folder structure [IDE0160] Convert to block scoped namespace [IDE0200] Lambda expression can be removed - [IDE0260] Use pattern matching [IDE0270] Null check can be simplified [IDE0290] Use primary constructor [IDE0330] Use 'System.Threading.Lock' @@ -101,7 +99,7 @@ [SYSLIB0012] 'Assembly.CodeBase' is obsolete --> - $(NoWarn);IDE0005;IDE0008;IDE0011;IDE0017;IDE0021;IDE0022;IDE0025;IDE0027;IDE0028;IDE0029;IDE0032;IDE0039;IDE0045;IDE0046;IDE0055;IDE0056;IDE0057;IDE0059;IDE0060;IDE0061;IDE0090;IDE0130;IDE0160;IDE0200;IDE0260;IDE0270;IDE0290;IDE0330 + $(NoWarn);IDE0005;IDE0008;IDE0011;IDE0017;IDE0021;IDE0022;IDE0025;IDE0027;IDE0029;IDE0032;IDE0039;IDE0045;IDE0046;IDE0055;IDE0056;IDE0057;IDE0059;IDE0060;IDE0061;IDE0090;IDE0130;IDE0160;IDE0200;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.AspNet.WebApi/JsonExceptionFilterAttribute.cs b/src/NSwag.AspNet.WebApi/JsonExceptionFilterAttribute.cs index f60c2fc54a..aeb7ecaf86 100644 --- a/src/NSwag.AspNet.WebApi/JsonExceptionFilterAttribute.cs +++ b/src/NSwag.AspNet.WebApi/JsonExceptionFilterAttribute.cs @@ -23,7 +23,7 @@ public class JsonExceptionFilterAttribute : ActionFilterAttribute { private readonly bool _hideStackTrace; private readonly IDictionary _searchedNamespaces; - private readonly List _exceptionTypes = new List(); + private readonly List _exceptionTypes = []; /// Initializes a new instance of the class. /// The serialized exception types. diff --git a/src/NSwag.AspNetCore/JsonExceptionFilterAttribute.cs b/src/NSwag.AspNetCore/JsonExceptionFilterAttribute.cs index 02289ca769..c6e959b54d 100644 --- a/src/NSwag.AspNetCore/JsonExceptionFilterAttribute.cs +++ b/src/NSwag.AspNetCore/JsonExceptionFilterAttribute.cs @@ -22,7 +22,7 @@ public class JsonExceptionFilterAttribute : ActionFilterAttribute { private readonly bool _hideStackTrace; private readonly IDictionary _searchedNamespaces; - private readonly List _exceptionTypes = new List(); + private readonly List _exceptionTypes = []; /// Initializes a new instance of the class. /// The serialized exception types. diff --git a/src/NSwag.AspNetCore/Middlewares/OpenApiDocumentMiddleware.cs b/src/NSwag.AspNetCore/Middlewares/OpenApiDocumentMiddleware.cs index d68e137500..4d56614aca 100644 --- a/src/NSwag.AspNetCore/Middlewares/OpenApiDocumentMiddleware.cs +++ b/src/NSwag.AspNetCore/Middlewares/OpenApiDocumentMiddleware.cs @@ -26,8 +26,7 @@ public class OpenApiDocumentMiddleware private int _version; private readonly object _documentsCacheLock = new object(); - private readonly Dictionary> _documentsCache - = new Dictionary>(); + private readonly Dictionary> _documentsCache = []; /// Initializes a new instance of the class. /// The next delegate. diff --git a/src/NSwag.AspNetCore/OAuth2ClientSettings.cs b/src/NSwag.AspNetCore/OAuth2ClientSettings.cs index 791409220e..95dc59da24 100644 --- a/src/NSwag.AspNetCore/OAuth2ClientSettings.cs +++ b/src/NSwag.AspNetCore/OAuth2ClientSettings.cs @@ -31,7 +31,7 @@ public class OAuth2ClientSettings public string ScopeSeparator { get; set; } = " "; /// Gets or sets initially selected scopes. - public ICollection Scopes { get; } = new List(); + public ICollection Scopes { get; } = []; /// Gets or sets the additional query string parameters. public IDictionary AdditionalQueryStringParameters { get; } = new Dictionary(); diff --git a/src/NSwag.AspNetCore/SwaggerUiSettings.cs b/src/NSwag.AspNetCore/SwaggerUiSettings.cs index 33c9fe20ec..64edcffb11 100644 --- a/src/NSwag.AspNetCore/SwaggerUiSettings.cs +++ b/src/NSwag.AspNetCore/SwaggerUiSettings.cs @@ -121,7 +121,7 @@ public bool WithCredentials } /// Gets or sets the Swagger URL routes (must start with '/', hides SwaggerRoute). - public ICollection SwaggerRoutes { get; } = new List(); + public ICollection SwaggerRoutes { get; } = []; /// Gets or sets the Swagger URL routes factory (SwaggerRoutes is ignored when set). #if AspNetOwin diff --git a/src/NSwag.CodeGeneration.CSharp.Tests/ControllerGenerationFormatTests.cs b/src/NSwag.CodeGeneration.CSharp.Tests/ControllerGenerationFormatTests.cs index 75a3397025..0d5ca528d0 100644 --- a/src/NSwag.CodeGeneration.CSharp.Tests/ControllerGenerationFormatTests.cs +++ b/src/NSwag.CodeGeneration.CSharp.Tests/ControllerGenerationFormatTests.cs @@ -374,7 +374,7 @@ private static OpenApiDocument GetOpenApiDocument() Type = JsonObjectType.String } }, - Tags = new List { "Secondary" } + Tags = ["Secondary"] } } }; diff --git a/src/NSwag.CodeGeneration.CSharp.Tests/FormParameterTests.cs b/src/NSwag.CodeGeneration.CSharp.Tests/FormParameterTests.cs index 8371e7d897..5ac8baa0e5 100644 --- a/src/NSwag.CodeGeneration.CSharp.Tests/FormParameterTests.cs +++ b/src/NSwag.CodeGeneration.CSharp.Tests/FormParameterTests.cs @@ -97,7 +97,7 @@ public void When_form_parameters_are_defined_then_FormUrlEncodedContent_is_gener OpenApiOperationMethod.Post, new OpenApiOperation { - Consumes = new System.Collections.Generic.List { "application/x-www-form-urlencoded" }, + Consumes = ["application/x-www-form-urlencoded"], Parameters = { new OpenApiParameter diff --git a/src/NSwag.CodeGeneration.CSharp/Models/CSharpFileTemplateModel.cs b/src/NSwag.CodeGeneration.CSharp/Models/CSharpFileTemplateModel.cs index 1a9603b9aa..0a90a87550 100644 --- a/src/NSwag.CodeGeneration.CSharp/Models/CSharpFileTemplateModel.cs +++ b/src/NSwag.CodeGeneration.CSharp/Models/CSharpFileTemplateModel.cs @@ -102,7 +102,7 @@ public CSharpFileTemplateModel( _document.Operations.Any(o => o.Operation.ActualResponses.Any(r => r.Value.IsBinary(o.Operation))); /// Gets or sets a value indicating whether to generate exception classes (default: true). - public bool GenerateExceptionClasses => (_settings as CSharpClientGeneratorSettings)?.GenerateExceptionClasses == true; + public bool GenerateExceptionClasses => _settings is CSharpClientGeneratorSettings { GenerateExceptionClasses: true }; /// Gets or sets a value indicating whether to wrap success responses to allow full response access. public bool WrapResponses => _settings.WrapResponses; diff --git a/src/NSwag.CodeGeneration.CSharp/Models/CSharpOperationModel.cs b/src/NSwag.CodeGeneration.CSharp/Models/CSharpOperationModel.cs index 10b598fae1..d871202c40 100644 --- a/src/NSwag.CodeGeneration.CSharp/Models/CSharpOperationModel.cs +++ b/src/NSwag.CodeGeneration.CSharp/Models/CSharpOperationModel.cs @@ -109,7 +109,7 @@ public string MethodAccessModifier /// /// The default value of the result type, i.e. default(T) or default(T)! depending on whether NRT are enabled. /// - public string UnwrappedResultDefaultValue => $"default({UnwrappedResultType}){((_settings as CSharpClientGeneratorSettings)?.CSharpGeneratorSettings.GenerateNullableReferenceTypes == true ? "!" : "")}"; + public string UnwrappedResultDefaultValue => $"default({UnwrappedResultType}){(_settings is CSharpClientGeneratorSettings { CSharpGeneratorSettings.GenerateNullableReferenceTypes: true } ? "!" : "")}"; /// Gets or sets the synchronous type of the result. public string SyncResultType diff --git a/src/NSwag.CodeGeneration.Tests/CodeGenerationTests.cs b/src/NSwag.CodeGeneration.Tests/CodeGenerationTests.cs index 37c3f6d945..1670f48bf5 100644 --- a/src/NSwag.CodeGeneration.Tests/CodeGenerationTests.cs +++ b/src/NSwag.CodeGeneration.Tests/CodeGenerationTests.cs @@ -315,17 +315,19 @@ private static OpenApiDocument CreateDocument() var settings = new NewtonsoftJsonSchemaGeneratorSettings(); var generator = new JsonSchemaGenerator(settings); - document.Paths["/Person"] = new OpenApiPathItem(); - document.Paths["/Person"][OpenApiOperationMethod.Get] = new OpenApiOperation + document.Paths["/Person"] = new OpenApiPathItem { - Responses = + [OpenApiOperationMethod.Get] = new OpenApiOperation { + Responses = { - "200", new OpenApiResponse { - Schema = new JsonSchema + "200", new OpenApiResponse { - Reference = generator.Generate(typeof(Person), new OpenApiSchemaResolver(document, settings)) + Schema = new JsonSchema + { + Reference = generator.Generate(typeof(Person), new OpenApiSchemaResolver(document, settings)) + } } } } diff --git a/src/NSwag.CodeGeneration.TypeScript.Tests/OperationParameterTests.cs b/src/NSwag.CodeGeneration.TypeScript.Tests/OperationParameterTests.cs index 65f9cfecbc..17ad1c25ae 100644 --- a/src/NSwag.CodeGeneration.TypeScript.Tests/OperationParameterTests.cs +++ b/src/NSwag.CodeGeneration.TypeScript.Tests/OperationParameterTests.cs @@ -45,7 +45,7 @@ public async Task When_query_parameter_is_enum_array_then_the_enum_is_referenced { var serializerSettings = new JsonSerializerSettings { - Converters = new List {new StringEnumConverter()} + Converters = [new StringEnumConverter()] }; // Arrange diff --git a/src/NSwag.CodeGeneration/JsonSchemaExtensions.cs b/src/NSwag.CodeGeneration/JsonSchemaExtensions.cs index 653aae50f8..8ec3e76c77 100644 --- a/src/NSwag.CodeGeneration/JsonSchemaExtensions.cs +++ b/src/NSwag.CodeGeneration/JsonSchemaExtensions.cs @@ -18,7 +18,7 @@ public static class JsonSchema4Extensions /// true or false public static bool UsesComplexObjectSchema(this JsonSchema schema) { - return UsesComplexObjectSchema(schema, new List()); + return UsesComplexObjectSchema(schema, []); } private static bool UsesComplexObjectSchema(this JsonSchema schema, List checkedSchemas) diff --git a/src/NSwag.Commands/NSwagDocumentBase.cs b/src/NSwag.Commands/NSwagDocumentBase.cs index 211765dd69..9e0bf3640a 100644 --- a/src/NSwag.Commands/NSwagDocumentBase.cs +++ b/src/NSwag.Commands/NSwagDocumentBase.cs @@ -297,10 +297,10 @@ private static JsonSerializerSettings GetSerializerSettings() DefaultValueHandling = DefaultValueHandling.Include, NullValueHandling = NullValueHandling.Include, ContractResolver = new CamelCasePropertyNamesContractResolver(), - Converters = new List - { + Converters = + [ new StringEnumConverter() - } + ] }; } diff --git a/src/NSwag.Commands/OpenApiDocumentExecutionResult.cs b/src/NSwag.Commands/OpenApiDocumentExecutionResult.cs index f8f07e3665..b9bde72c52 100644 --- a/src/NSwag.Commands/OpenApiDocumentExecutionResult.cs +++ b/src/NSwag.Commands/OpenApiDocumentExecutionResult.cs @@ -11,7 +11,7 @@ namespace NSwag.Commands /// Stores the result of a execution. public class OpenApiDocumentExecutionResult { - private readonly Dictionary _generatorOutputs = new Dictionary(); + private readonly Dictionary _generatorOutputs = []; /// Initializes a new instance of the class. /// The command line output. diff --git a/src/NSwag.Core.Tests/SwaggerResponseTests.cs b/src/NSwag.Core.Tests/SwaggerResponseTests.cs index a849deb39a..5130d0ccba 100644 --- a/src/NSwag.Core.Tests/SwaggerResponseTests.cs +++ b/src/NSwag.Core.Tests/SwaggerResponseTests.cs @@ -17,7 +17,7 @@ public void When_response_contains_produces_detect_if_binary_response(string con // Arrange var response = new OpenApiResponse(); var operation = new OpenApiOperation(); - operation.Produces = new System.Collections.Generic.List { contentType }; + operation.Produces = [contentType]; operation.Responses.Add("200", response); // Act diff --git a/src/NSwag.Core/Collections/ObservableDictionary.cs b/src/NSwag.Core/Collections/ObservableDictionary.cs index ed47725bbe..5b28de0418 100644 --- a/src/NSwag.Core/Collections/ObservableDictionary.cs +++ b/src/NSwag.Core/Collections/ObservableDictionary.cs @@ -24,7 +24,7 @@ public class ObservableDictionary : /// Initializes a new instance of the class. public ObservableDictionary() { - _dictionary = new Dictionary(); + _dictionary = []; } /// Initializes a new instance of the class. diff --git a/src/NSwag.Core/OpenApiDocument.Serialization.cs b/src/NSwag.Core/OpenApiDocument.Serialization.cs index 6b13073208..9f2fdca4ff 100644 --- a/src/NSwag.Core/OpenApiDocument.Serialization.cs +++ b/src/NSwag.Core/OpenApiDocument.Serialization.cs @@ -113,7 +113,7 @@ private static PropertyRenameAndIgnoreSerializerContractResolver CreateJsonSeria return resolver; } - private ObservableCollection _schemes = new ObservableCollection(); + private ObservableCollection _schemes = []; /// Gets or sets the host (name or ip) serving the API (Swagger only). [JsonProperty(PropertyName = "host", Order = 5, DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] @@ -149,13 +149,13 @@ public ICollection Schemes _schemes = new ObservableCollection(Servers? .Where(s => s.Url.Contains("://")) .Select(s => s.Url.StartsWith("http://") ? OpenApiSchema.Http : OpenApiSchema.Https) - .Distinct() ?? new List()); + .Distinct() ?? []); _schemes.CollectionChanged += OnSchemesChanged; return _schemes; } - set { UpdateServers(value, Host, BasePath); } + set => UpdateServers(value, Host, BasePath); } private void OnSchemesChanged(object sender, NotifyCollectionChangedEventArgs e) @@ -167,30 +167,30 @@ private void UpdateServers(ICollection schemes, string host, stri { if ((schemes == null || schemes.Count == 0) && (!string.IsNullOrEmpty(host) || !string.IsNullOrEmpty(basePath))) { - Servers = new List - { + Servers = + [ new OpenApiServer { Url = host + basePath } - }; + ]; } else { Servers = schemes?.Select(s => new OpenApiServer { Url = s.ToString().ToLowerInvariant() + "://" + host + basePath - }).ToList() ?? new List(); + }).ToList() ?? []; } } /// Gets or sets a list of MIME types the operation can consume. [JsonProperty(PropertyName = "consumes", Order = 8, DefaultValueHandling = DefaultValueHandling.Ignore)] - public ICollection Consumes { get; set; } = new List(); + public ICollection Consumes { get; set; } = []; /// Gets or sets a list of MIME types the operation can produce. [JsonProperty(PropertyName = "produces", Order = 9, DefaultValueHandling = DefaultValueHandling.Ignore)] - public ICollection Produces { get; set; } = new List(); + public ICollection Produces { get; set; } = []; /// Gets or sets the types (Swagger only). [JsonProperty(PropertyName = "definitions", Order = 13, DefaultValueHandling = DefaultValueHandling.Ignore)] diff --git a/src/NSwag.Core/OpenApiDocument.cs b/src/NSwag.Core/OpenApiDocument.cs index bca7d0f77f..077f238468 100644 --- a/src/NSwag.Core/OpenApiDocument.cs +++ b/src/NSwag.Core/OpenApiDocument.cs @@ -75,7 +75,7 @@ public OpenApiDocument() /// Gets or sets the servers (OpenAPI only). [JsonProperty(PropertyName = "servers", Order = 10, DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] - public ICollection Servers { get; private set; } = new Collection(); + public ICollection Servers { get; private set; } = []; /// Gets or sets the operations. [JsonProperty(PropertyName = "paths", Order = 11, DefaultValueHandling = DefaultValueHandling.Ignore)] @@ -87,11 +87,11 @@ public OpenApiDocument() /// Gets or sets a security description. [JsonProperty(PropertyName = "security", Order = 17, DefaultValueHandling = DefaultValueHandling.Ignore)] - public ICollection Security { get; set; } = new Collection(); + public ICollection Security { get; set; } = []; /// Gets or sets the description. [JsonProperty(PropertyName = "tags", Order = 18, DefaultValueHandling = DefaultValueHandling.Ignore)] - public IList Tags { get; set; } = new Collection(); + public IList Tags { get; set; } = []; /// Gets the base URL of the web service. [JsonIgnore] diff --git a/src/NSwag.Core/OpenApiEncoding.cs b/src/NSwag.Core/OpenApiEncoding.cs index 7a90322cf0..177d93bc05 100644 --- a/src/NSwag.Core/OpenApiEncoding.cs +++ b/src/NSwag.Core/OpenApiEncoding.cs @@ -19,7 +19,7 @@ public class OpenApiEncoding /// Gets or sets the headers. [JsonProperty(PropertyName = "headers", DefaultValueHandling = DefaultValueHandling.Ignore)] - public OpenApiHeaders Headers { get; } = new OpenApiHeaders(); + public OpenApiHeaders Headers { get; } = []; /// Gets or sets the encoding type. [JsonProperty(PropertyName = "style", DefaultValueHandling = DefaultValueHandling.Ignore)] diff --git a/src/NSwag.Core/OpenApiOperation.cs b/src/NSwag.Core/OpenApiOperation.cs index e283d8e714..edbb1efe94 100644 --- a/src/NSwag.Core/OpenApiOperation.cs +++ b/src/NSwag.Core/OpenApiOperation.cs @@ -27,7 +27,7 @@ public class OpenApiOperation : JsonExtensionObject /// Initializes a new instance of the class. public OpenApiOperation() { - Tags = new List(); + Tags = []; var parameters = new ObservableCollection(); parameters.CollectionChanged += (sender, args) => @@ -152,7 +152,7 @@ public IReadOnlyList ActualParameters /// Gets or sets the servers (OpenAPI only). [JsonProperty(PropertyName = "servers", Order = 15, DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] - public ICollection Servers { get; set; } = new Collection(); + public ICollection Servers { get; set; } = []; /// Gets the list of MIME types the operation can consume, either from the operation or from the . [JsonIgnore] @@ -184,7 +184,7 @@ public void TryAddConsumes(string mimeType) { if (Consumes == null) { - Consumes = new List { mimeType }; + Consumes = [mimeType]; } else if (!Consumes.Contains(mimeType)) { diff --git a/src/NSwag.Core/OpenApiPathItem.cs b/src/NSwag.Core/OpenApiPathItem.cs index 50bd8a8633..d74ef60bc1 100644 --- a/src/NSwag.Core/OpenApiPathItem.cs +++ b/src/NSwag.Core/OpenApiPathItem.cs @@ -47,11 +47,11 @@ public OpenApiPathItem() /// Gets or sets the servers (OpenAPI only). [JsonProperty(PropertyName = "servers", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] - public ICollection Servers { get; set; } = new Collection(); + public ICollection Servers { get; set; } = []; /// Gets or sets the parameters. [JsonProperty(PropertyName = "parameters", DefaultValueHandling = DefaultValueHandling.Ignore)] - public ICollection Parameters { get; set; } = new Collection(); + public ICollection Parameters { get; set; } = []; /// Gets or sets the extension data (i.e. additional properties which are not directly defined by the JSON object). [JsonExtensionData] diff --git a/src/NSwag.Core/OpenApiResponse.cs b/src/NSwag.Core/OpenApiResponse.cs index e04a045ae5..a81bcab62b 100644 --- a/src/NSwag.Core/OpenApiResponse.cs +++ b/src/NSwag.Core/OpenApiResponse.cs @@ -37,7 +37,7 @@ public class OpenApiResponse : JsonReferenceBase, IJsonReferenc /// Gets or sets the headers. [JsonProperty(PropertyName = "headers", Order = 3, DefaultValueHandling = DefaultValueHandling.Ignore)] - public OpenApiHeaders Headers { get; } = new OpenApiHeaders(); + public OpenApiHeaders Headers { get; } = []; /// Sets a value indicating whether the response can be null (use IsNullable() to get a parameter's nullability). /// The Swagger spec does not support null in schemas, see https://github.com/OAI/OpenAPI-Specification/issues/229 diff --git a/src/NSwag.Core/OpenApiServerVariable.cs b/src/NSwag.Core/OpenApiServerVariable.cs index 9f0fc37a41..7c9b0da86c 100644 --- a/src/NSwag.Core/OpenApiServerVariable.cs +++ b/src/NSwag.Core/OpenApiServerVariable.cs @@ -16,7 +16,7 @@ public class OpenApiServerVariable { /// Gets or sets the enum of the server. [JsonProperty(PropertyName = "enum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] - public ICollection Enum { get; } = new Collection(); + public ICollection Enum { get; } = []; /// Gets or sets the variables of the server. [JsonProperty(PropertyName = "default", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] diff --git a/src/NSwag.Generation.AspNetCore.Tests/Processors/OperationResponseProcessorTest.cs b/src/NSwag.Generation.AspNetCore.Tests/Processors/OperationResponseProcessorTest.cs index fc20775a11..70985fc25a 100644 --- a/src/NSwag.Generation.AspNetCore.Tests/Processors/OperationResponseProcessorTest.cs +++ b/src/NSwag.Generation.AspNetCore.Tests/Processors/OperationResponseProcessorTest.cs @@ -141,7 +141,7 @@ private AspNetCoreOperationProcessorContext GetContext(ApiDescription apiDescrip generator, schemaResolver, settings, - new List()) + []) { ApiDescription = apiDescription, }; diff --git a/src/NSwag.Generation.AspNetCore/AspNetCoreOpenApiDocumentGenerator.cs b/src/NSwag.Generation.AspNetCore/AspNetCoreOpenApiDocumentGenerator.cs index 678d7e53f1..2d93811210 100644 --- a/src/NSwag.Generation.AspNetCore/AspNetCoreOpenApiDocumentGenerator.cs +++ b/src/NSwag.Generation.AspNetCore/AspNetCoreOpenApiDocumentGenerator.cs @@ -330,7 +330,7 @@ private List> Add var path = operation.Path.Replace("//", "/"); if (!document.Paths.TryGetValue(path, out var pathItem)) { - document.Paths[path] = pathItem = new OpenApiPathItem(); + document.Paths[path] = pathItem = []; } if (pathItem.ContainsKey(operation.Method)) diff --git a/src/NSwag.Generation.AspNetCore/Processors/AspNetCoreOperationSecurityScopeProcessor.cs b/src/NSwag.Generation.AspNetCore/Processors/AspNetCoreOperationSecurityScopeProcessor.cs index b24d8bfa86..a52bfe1e0b 100644 --- a/src/NSwag.Generation.AspNetCore/Processors/AspNetCoreOperationSecurityScopeProcessor.cs +++ b/src/NSwag.Generation.AspNetCore/Processors/AspNetCoreOperationSecurityScopeProcessor.cs @@ -54,7 +54,7 @@ public bool Process(OperationProcessorContext context) if (context.OperationDescription.Operation.Security == null) { - context.OperationDescription.Operation.Security = new List(); + context.OperationDescription.Operation.Security = []; } var scopes = GetScopes(authorizeAttributes); diff --git a/src/NSwag.Generation.WebApi/WebApiOpenApiDocumentGenerator.cs b/src/NSwag.Generation.WebApi/WebApiOpenApiDocumentGenerator.cs index 7ca64b0165..dec729ea78 100644 --- a/src/NSwag.Generation.WebApi/WebApiOpenApiDocumentGenerator.cs +++ b/src/NSwag.Generation.WebApi/WebApiOpenApiDocumentGenerator.cs @@ -115,8 +115,8 @@ await OpenApiDocument.FromJsonAsync(Settings.DocumentTemplate).ConfigureAwait(fa document.Generator = $"NSwag{version}"; document.SchemaType = Settings.SchemaSettings.SchemaType; - document.Consumes = new List { "application/json" }; - document.Produces = new List { "application/json" }; + document.Consumes = ["application/json"]; + document.Produces = ["application/json"]; document.Info ??= new OpenApiInfo(); @@ -215,7 +215,7 @@ private bool AddOperationDescriptionsToDocument(OpenApiDocument document, Type c if (!document.Paths.TryGetValue(path, out OpenApiPathItem value)) { - value = new OpenApiPathItem(); + value = []; document.Paths[path] = value; } @@ -605,7 +605,7 @@ private static IEnumerable GetSupportedHttpMethodsFromAttributes(MethodI if (acceptVerbsAttribute != null) { - IEnumerable httpMethods = new List(); + IEnumerable httpMethods = []; if (ObjectExtensions.HasProperty(acceptVerbsAttribute, "HttpMethods")) { diff --git a/src/NSwag.Generation/OpenApiDocumentGeneratorSettings.cs b/src/NSwag.Generation/OpenApiDocumentGeneratorSettings.cs index 64eaee735d..090eb6b3c4 100644 --- a/src/NSwag.Generation/OpenApiDocumentGeneratorSettings.cs +++ b/src/NSwag.Generation/OpenApiDocumentGeneratorSettings.cs @@ -55,20 +55,20 @@ public OpenApiDocumentGeneratorSettings() /// Gets the operation processors. [JsonIgnore] - public OperationProcessorCollection OperationProcessors { get; } = new OperationProcessorCollection - { + public OperationProcessorCollection OperationProcessors { get; } = + [ new OperationSummaryAndDescriptionProcessor(), new OperationTagsProcessor(), - new OperationExtensionDataProcessor(), - }; + new OperationExtensionDataProcessor() + ]; /// Gets the document processors. [JsonIgnore] - public DocumentProcessorCollection DocumentProcessors { get; } = new DocumentProcessorCollection - { + public DocumentProcessorCollection DocumentProcessors { get; } = + [ new DocumentTagsProcessor(), - new DocumentExtensionDataProcessor(), - }; + new DocumentExtensionDataProcessor() + ]; /// Gets or sets the document template representing the initial Swagger specification (JSON data). public string DocumentTemplate { get; set; } diff --git a/src/NSwag.Generation/Processors/DocumentTagsProcessor.cs b/src/NSwag.Generation/Processors/DocumentTagsProcessor.cs index b2eb2ba6b7..c0fca20669 100644 --- a/src/NSwag.Generation/Processors/DocumentTagsProcessor.cs +++ b/src/NSwag.Generation/Processors/DocumentTagsProcessor.cs @@ -41,7 +41,7 @@ private static void ProcessTagsAttribute(OpenApiDocument document, Type controll if (tags.Count > 0) { - document.Tags ??= new List(); + document.Tags ??= []; foreach (var tag in tags) { if (document.Tags.All(t => t.Name != tag.Name)) diff --git a/src/NSwag.Generation/Processors/OperationTagsProcessor.cs b/src/NSwag.Generation/Processors/OperationTagsProcessor.cs index dea75e1b3b..5d3a245c7a 100644 --- a/src/NSwag.Generation/Processors/OperationTagsProcessor.cs +++ b/src/NSwag.Generation/Processors/OperationTagsProcessor.cs @@ -116,7 +116,7 @@ private static void ProcessSwaggerTagsAttribute(OpenApiDocument document, OpenAp if (ObjectExtensions.HasProperty(tagsAttribute, "AddToDocument") && tagsAttribute.AddToDocument) { - document.Tags ??= new List(); + document.Tags ??= []; if (document.Tags.All(t => t.Name != tag)) { document.Tags.Add(new OpenApiTag { Name = tag }); diff --git a/src/NSwag.Generation/Processors/Security/OperationSecurityScopeProcessor.cs b/src/NSwag.Generation/Processors/Security/OperationSecurityScopeProcessor.cs index 81d03c791a..86f5c7a078 100644 --- a/src/NSwag.Generation/Processors/Security/OperationSecurityScopeProcessor.cs +++ b/src/NSwag.Generation/Processors/Security/OperationSecurityScopeProcessor.cs @@ -35,7 +35,7 @@ public bool Process(OperationProcessorContext context) { if (context.OperationDescription.Operation.Security == null) { - context.OperationDescription.Operation.Security = new List(); + context.OperationDescription.Operation.Security = []; } var scopes = GetScopes(context.OperationDescription, context.MethodInfo); diff --git a/src/NSwag.Generation/Processors/Security/SecurityDefinitionAppender.cs b/src/NSwag.Generation/Processors/Security/SecurityDefinitionAppender.cs index a705b101be..877628e0f2 100644 --- a/src/NSwag.Generation/Processors/Security/SecurityDefinitionAppender.cs +++ b/src/NSwag.Generation/Processors/Security/SecurityDefinitionAppender.cs @@ -48,7 +48,7 @@ public void Process(DocumentProcessorContext context) { if (context.Document.Security == null) { - context.Document.Security = new Collection(); + context.Document.Security = []; } context.Document.Security.Add(new OpenApiSecurityRequirement