Skip to content

Commit 9ce0218

Browse files
authored
Simplify collection initialization (#5044)
1 parent 63082af commit 9ce0218

File tree

27 files changed

+102
-102
lines changed

27 files changed

+102
-102
lines changed

Directory.Build.props

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,6 @@
7878
[IDE0260] Use pattern matching
7979
[IDE0270] Null check can be simplified
8080
[IDE0290] Use primary constructor
81-
[IDE0300] Collection initialization can be simplified
82-
[IDE0301] Collection initialization can be simplified
83-
[IDE0305] Collection initialization can be simplified
8481
[IDE0330] Use 'System.Threading.Lock'
8582
[CA1200] Avoid using cref tags with a prefix
8683
[CA1304] The behavior of 'string.ToUpper()' could vary based on the current user's locale settings
@@ -108,7 +105,7 @@
108105
109106
[SYSLIB0012] 'Assembly.CodeBase' is obsolete
110107
-->
111-
<NoWarn>$(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;IDE0300;IDE0305;IDE0301;IDE0330</NoWarn>
108+
<NoWarn>$(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>
112109
<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>
113110
</PropertyGroup>
114111

src/NSwag.Annotations/OpenApiBodyParameterAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class OpenApiBodyParameterAttribute : Attribute
1515
/// <summary>Initializes a new instance of the <see cref="OpenApiBodyParameterAttribute"/> class with the 'application/json' mime type.</summary>
1616
public OpenApiBodyParameterAttribute()
1717
{
18-
MimeTypes = new[] { "application/json" };
18+
MimeTypes = ["application/json"];
1919
}
2020

2121
/// <summary>Initializes a new instance of the <see cref="OpenApiBodyParameterAttribute"/> class.</summary>

src/NSwag.AspNet.Owin/SwaggerExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static IAppBuilder UseOpenApi(
3232
Assembly webApiAssembly,
3333
Action<SwaggerSettings<WebApiOpenApiDocumentGeneratorSettings>> configure = null)
3434
{
35-
return app.UseOpenApi(new[] { webApiAssembly }, configure);
35+
return app.UseOpenApi([webApiAssembly], configure);
3636
}
3737

3838
/// <summary>Adds the OpenAPI/Swagger generator to the OWIN pipeline.</summary>
@@ -81,7 +81,7 @@ public static IAppBuilder UseSwaggerUi(
8181
Assembly webApiAssembly,
8282
Action<SwaggerUiSettings<WebApiOpenApiDocumentGeneratorSettings>> configure = null)
8383
{
84-
return app.UseSwaggerUi(new[] { webApiAssembly }, configure);
84+
return app.UseSwaggerUi([webApiAssembly], configure);
8585
}
8686

8787
/// <summary>Adds the Swagger generator and Swagger UI to the OWIN pipeline.</summary>
@@ -152,7 +152,7 @@ public static IAppBuilder UseSwaggerReDoc(
152152
Assembly webApiAssembly,
153153
Action<ReDocSettings<WebApiOpenApiDocumentGeneratorSettings>> configure = null)
154154
{
155-
return app.UseSwaggerReDoc(new[] { webApiAssembly }, configure);
155+
return app.UseSwaggerReDoc([webApiAssembly], configure);
156156
}
157157

158158
/// <summary>Adds the Swagger generator and Swagger UI to the OWIN pipeline.</summary>

src/NSwag.AspNetCore.Launcher/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private static int Main(string[] args)
129129

130130
try
131131
{
132-
method.Invoke(null, new[] { commandContent, outputFile, applicationName });
132+
method.Invoke(null, [commandContent, outputFile, applicationName]);
133133
}
134134
catch (Exception ex)
135135
{

src/NSwag.CodeGeneration.CSharp/CSharpGeneratorBaseSettings.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ protected CSharpGeneratorBaseSettings()
2626
SchemaType = SchemaType.Swagger2
2727
};
2828

29-
CSharpGeneratorSettings.TemplateFactory = new DefaultTemplateFactory(CSharpGeneratorSettings, new[]
30-
{
29+
CSharpGeneratorSettings.TemplateFactory = new DefaultTemplateFactory(CSharpGeneratorSettings, [
3130
typeof(CSharpGeneratorSettings).GetTypeInfo().Assembly,
32-
typeof(CSharpGeneratorBaseSettings).GetTypeInfo().Assembly,
33-
});
31+
typeof(CSharpGeneratorBaseSettings).GetTypeInfo().Assembly
32+
]);
3433

3534
ResponseArrayType = "System.Collections.Generic.ICollection";
3635
ResponseDictionaryType = "System.Collections.Generic.IDictionary";

src/NSwag.CodeGeneration.CSharp/Models/CSharpFileTemplateModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public IEnumerable<string> ResponseClassNames
128128
.Distinct();
129129
}
130130

131-
return new[] { _settings.ResponseClass.Replace("{controller}", string.Empty) };
131+
return [_settings.ResponseClass.Replace("{controller}", string.Empty)];
132132
}
133133
}
134134

@@ -150,7 +150,7 @@ public IEnumerable<string> ExceptionClassNames
150150
}
151151
else
152152
{
153-
return new[] { settings.ExceptionClass.Replace("{controller}", string.Empty) };
153+
return [settings.ExceptionClass.Replace("{controller}", string.Empty)];
154154
}
155155
}
156156
return [];

src/NSwag.CodeGeneration.CSharp/Models/CSharpOperationModel.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ namespace NSwag.CodeGeneration.CSharp.Models
1616
/// <summary>The CSharp operation model.</summary>
1717
public class CSharpOperationModel : OperationModelBase<CSharpParameterModel, CSharpResponseModel>
1818
{
19-
private static readonly string[] ReservedKeywords =
20-
{
19+
private static readonly HashSet<string> ReservedKeywords =
20+
[
2121
"abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", "class", "const", "continue",
2222
"decimal", "default", "delegate", "do", "double", "else", "enum", "event", "explicit", "extern", "false", "finally", "fixed", "float",
2323
"for", "foreach", "goto", "if", "implicit", "in", "int", "interface", "internal", "is", "lock", "long", "namespace", "new", "null", "object",
2424
"operator", "out", "override", "params", "private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof",
2525
"stackalloc", "static", "string", "struct", "switch", "this", "throw", "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe",
2626
"ushort", "using", "virtual", "void", "volatile", "while"
27-
};
27+
];
2828

2929
private readonly CSharpGeneratorBaseSettings _settings;
3030
private readonly OpenApiOperation _operation;
@@ -55,17 +55,16 @@ public CSharpOperationModel(
5555
// TODO: Move to CSharpControllerOperationModel
5656
if (generator is CSharpControllerGenerator)
5757
{
58-
parameters = parameters
58+
parameters = [.. parameters
5959
.OrderBy(p => p.Position ?? 0)
60-
.OrderBy(p => !p.IsRequired)
61-
.ThenBy(p => p.Default == null).ToList();
60+
.ThenBy(p => !p.IsRequired)
61+
.ThenBy(p => p.Default == null)];
6262
}
6363
else
6464
{
65-
parameters = parameters
65+
parameters = [.. parameters
6666
.OrderBy(p => p.Position ?? 0)
67-
.OrderBy(p => !p.IsRequired)
68-
.ToList();
67+
.ThenBy(p => !p.IsRequired)];
6968
}
7069
}
7170

@@ -181,10 +180,10 @@ public IEnumerable<CSharpExceptionDescriptionModel> ExceptionDescriptions
181180
}
182181
else if (r.InheritsExceptionSchema)
183182
{
184-
return new[]
185-
{
183+
return
184+
[
186185
new CSharpExceptionDescriptionModel(r.Type, r.ExceptionDescription, controllerName, settings)
187-
};
186+
];
188187
}
189188
else
190189
{

src/NSwag.CodeGeneration.Tests/CodeGenerationTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void When_generating_CSharp_code_with_SystemTextJson_and_JsonConverters_t
9999
// Act
100100
var settings = new CSharpClientGeneratorSettings();
101101
settings.CSharpGeneratorSettings.JsonLibrary = NJsonSchema.CodeGeneration.CSharp.CSharpJsonLibrary.SystemTextJson;
102-
settings.CSharpGeneratorSettings.JsonConverters = new[] { "CustomConverter1", "CustomConverter2" };
102+
settings.CSharpGeneratorSettings.JsonConverters = ["CustomConverter1", "CustomConverter2"];
103103

104104
var generator = new CSharpClientGenerator(document, settings);
105105
var code = generator.GenerateFile();
@@ -140,7 +140,7 @@ public static Person FromJson(string data)
140140
// Act
141141
var settings = new CSharpClientGeneratorSettings();
142142
settings.CSharpGeneratorSettings.JsonLibrary = NJsonSchema.CodeGeneration.CSharp.CSharpJsonLibrary.SystemTextJson;
143-
settings.CSharpGeneratorSettings.JsonConverters = new[] { "CustomConverter1", "CustomConverter2" };
143+
settings.CSharpGeneratorSettings.JsonConverters = ["CustomConverter1", "CustomConverter2"];
144144
settings.CSharpGeneratorSettings.GenerateJsonMethods = true;
145145

146146
var generator = new CSharpClientGenerator(document, settings);
@@ -237,7 +237,7 @@ public void When_using_MultipleClientsFromFirstTagAndOperationName_then_ensure_t
237237
// Arrange
238238
var operation = new OpenApiOperation
239239
{
240-
Tags = tags.ToList()
240+
Tags = [.. tags]
241241
};
242242
var generator = new MultipleClientsFromFirstTagAndOperationNameGenerator();
243243

src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptFileTemplateModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public IEnumerable<string> ResponseClassNames
8181
.Distinct();
8282
}
8383

84-
return new[] { _settings.ResponseClass.Replace("{controller}", string.Empty) };
84+
return [_settings.ResponseClass.Replace("{controller}", string.Empty)];
8585
}
8686
}
8787

src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptOperationModel.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ public TypeScriptOperationModel(
3939

4040
if (settings.GenerateOptionalParameters)
4141
{
42-
parameters = parameters
43-
.OrderBy(p => p.Position ?? 0)
44-
.OrderBy(p => !p.IsRequired)
45-
.ToList();
42+
parameters = [.. parameters.OrderBy(p => p.Position ?? 0).ThenBy(p => !p.IsRequired)];
4643
}
4744

4845
Parameters = parameters
@@ -131,7 +128,7 @@ public override string ExceptionType
131128
return string.Join(" | ", _operation.ActualResponses
132129
.Where(r => !HttpUtilities.IsSuccessStatusCode(r.Key) && r.Value.Schema != null)
133130
.Select(r => _generator.GetTypeName(r.Value.Schema, r.Value.IsNullable(_settings.CodeGeneratorSettings.SchemaType), "Exception"))
134-
.Concat(new[] { "string" }));
131+
.Concat(["string"]));
135132
}
136133
}
137134

0 commit comments

Comments
 (0)