Skip to content

Commit 5686de4

Browse files
committed
chore: simplify collection initialization
1 parent a28aa21 commit 5686de4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+347
-354
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ var document = new OpenApiDocument
5555
{
5656
["/pets"] = new OpenApiPathItem
5757
{
58-
Operations = new Dictionary<HttpMethod, OpenApiOperation>
58+
Operations = new()
5959
{
6060
[HttpMethod.Get] = new OpenApiOperation
6161
{

src/Microsoft.OpenApi/Extensions/OpenApiExtensibleExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static void AddExtension<T>(this T element, string name, IOpenApiExtensio
3333
throw new OpenApiException(string.Format(SRResource.ExtensionFieldNameMustBeginWithXDash, name));
3434
}
3535

36-
element.Extensions ??= new Dictionary<string, IOpenApiExtension>();
36+
element.Extensions ??= [];
3737
element.Extensions[name] = Utils.CheckArgumentNull(any);
3838
}
3939
}

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public OpenApiDocument(OpenApiDocument? document)
133133
Info = document?.Info != null ? new(document.Info) : new OpenApiInfo();
134134
JsonSchemaDialect = document?.JsonSchemaDialect ?? JsonSchemaDialect;
135135
Servers = document?.Servers != null ? new List<OpenApiServer>(document.Servers) : null;
136-
Paths = document?.Paths != null ? new(document.Paths) : new OpenApiPaths();
136+
Paths = document?.Paths != null ? new(document.Paths) : [];
137137
Webhooks = document?.Webhooks != null ? new Dictionary<string, IOpenApiPathItem>(document.Webhooks) : null;
138138
Components = document?.Components != null ? new(document?.Components) : null;
139139
Security = document?.Security != null ? new List<OpenApiSecurityRequirement>(document.Security) : null;
@@ -414,7 +414,7 @@ public void SerializeAsV2(IOpenApiWriter writer)
414414

415415
private static string? ParseServerUrl(OpenApiServer server)
416416
{
417-
return server.ReplaceServerUrlVariables(new Dictionary<string, string>(0));
417+
return server.ReplaceServerUrlVariables([]);
418418
}
419419

420420
private static void WriteHostInfoV2(IOpenApiWriter writer, IList<OpenApiServer>? servers)
@@ -651,43 +651,43 @@ public bool AddComponent<T>(string id, T componentToRegister)
651651
switch (componentToRegister)
652652
{
653653
case IOpenApiSchema openApiSchema:
654-
Components.Schemas ??= new Dictionary<string, IOpenApiSchema>();
654+
Components.Schemas ??= [];
655655
Components.Schemas.Add(id, openApiSchema);
656656
break;
657657
case IOpenApiParameter openApiParameter:
658-
Components.Parameters ??= new Dictionary<string, IOpenApiParameter>();
658+
Components.Parameters ??= [];
659659
Components.Parameters.Add(id, openApiParameter);
660660
break;
661661
case IOpenApiResponse openApiResponse:
662-
Components.Responses ??= new Dictionary<string, IOpenApiResponse>();
662+
Components.Responses ??= [];
663663
Components.Responses.Add(id, openApiResponse);
664664
break;
665665
case IOpenApiRequestBody openApiRequestBody:
666-
Components.RequestBodies ??= new Dictionary<string, IOpenApiRequestBody>();
666+
Components.RequestBodies ??= [];
667667
Components.RequestBodies.Add(id, openApiRequestBody);
668668
break;
669669
case IOpenApiLink openApiLink:
670-
Components.Links ??= new Dictionary<string, IOpenApiLink>();
670+
Components.Links ??= [];
671671
Components.Links.Add(id, openApiLink);
672672
break;
673673
case IOpenApiCallback openApiCallback:
674-
Components.Callbacks ??= new Dictionary<string, IOpenApiCallback>();
674+
Components.Callbacks ??= [];
675675
Components.Callbacks.Add(id, openApiCallback);
676676
break;
677677
case IOpenApiPathItem openApiPathItem:
678-
Components.PathItems ??= new Dictionary<string, IOpenApiPathItem>();
678+
Components.PathItems ??= [];
679679
Components.PathItems.Add(id, openApiPathItem);
680680
break;
681681
case IOpenApiExample openApiExample:
682-
Components.Examples ??= new Dictionary<string, IOpenApiExample>();
682+
Components.Examples ??= [];
683683
Components.Examples.Add(id, openApiExample);
684684
break;
685685
case IOpenApiHeader openApiHeader:
686-
Components.Headers ??= new Dictionary<string, IOpenApiHeader>();
686+
Components.Headers ??= [];
687687
Components.Headers.Add(id, openApiHeader);
688688
break;
689689
case IOpenApiSecurityScheme openApiSecurityScheme:
690-
Components.SecuritySchemes ??= new Dictionary<string, IOpenApiSecurityScheme>();
690+
Components.SecuritySchemes ??= [];
691691
Components.SecuritySchemes.Add(id, openApiSecurityScheme);
692692
break;
693693
default:

src/Microsoft.OpenApi/Reader/V2/OpenApiOperationDeserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ internal static IOpenApiRequestBody CreateRequestBody(
241241

242242
if (bodyParameter.Name is not null)
243243
{
244-
requestBody.Extensions ??= new Dictionary<string, IOpenApiExtension>();
244+
requestBody.Extensions ??= [];
245245
requestBody.Extensions[OpenApiConstants.BodyName] = new OpenApiAny(bodyParameter.Name);
246246
}
247247
return requestBody;

src/Microsoft.OpenApi/Reader/V2/OpenApiResponseDeserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private static void ProcessProduces(MapNode mapNode, OpenApiResponse response, P
6262
{
6363
if (response.Content == null)
6464
{
65-
response.Content = new Dictionary<string, OpenApiMediaType>();
65+
response.Content = [];
6666
}
6767
else if (context.GetFromTempStorage<bool>(TempStorageKeys.ResponseProducesSet, response))
6868
{

src/Microsoft.OpenApi/Services/CopyReferences.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private void AddSchemaToComponents(IOpenApiSchema? schema, string? referenceId =
9191
EnsureSchemasExist();
9292
if (referenceId is not null && schema is not null && !(Components.Schemas?.ContainsKey(referenceId) ?? false))
9393
{
94-
Components.Schemas ??= new Dictionary<string, IOpenApiSchema>();
94+
Components.Schemas ??= [];
9595
Components.Schemas.Add(referenceId, schema);
9696
}
9797
}
@@ -102,7 +102,7 @@ private void AddParameterToComponents(IOpenApiParameter? parameter, string? refe
102102
EnsureParametersExist();
103103
if (parameter is not null && referenceId is not null && !(Components.Parameters?.ContainsKey(referenceId) ?? false))
104104
{
105-
Components.Parameters ??= new Dictionary<string, IOpenApiParameter>();
105+
Components.Parameters ??= [];
106106
Components.Parameters.Add(referenceId, parameter);
107107
}
108108
}
@@ -113,7 +113,7 @@ private void AddResponseToComponents(IOpenApiResponse? response, string? referen
113113
EnsureResponsesExist();
114114
if (referenceId is not null && response is not null && !(Components.Responses?.ContainsKey(referenceId) ?? false))
115115
{
116-
Components.Responses ??= new Dictionary<string, IOpenApiResponse>();
116+
Components.Responses ??= [];
117117
Components.Responses.Add(referenceId, response);
118118
}
119119
}
@@ -123,7 +123,7 @@ private void AddRequestBodyToComponents(IOpenApiRequestBody? requestBody, string
123123
EnsureRequestBodiesExist();
124124
if (requestBody is not null && referenceId is not null && !(Components.RequestBodies?.ContainsKey(referenceId) ?? false))
125125
{
126-
Components.RequestBodies ??= new Dictionary<string, IOpenApiRequestBody>();
126+
Components.RequestBodies ??= [];
127127
Components.RequestBodies.Add(referenceId, requestBody);
128128
}
129129
}
@@ -133,7 +133,7 @@ private void AddLinkToComponents(IOpenApiLink? link, string? referenceId = null)
133133
EnsureLinksExist();
134134
if (link is not null && referenceId is not null && !(Components.Links?.ContainsKey(referenceId) ?? false))
135135
{
136-
Components.Links ??= new Dictionary<string, IOpenApiLink>();
136+
Components.Links ??= [];
137137
Components.Links.Add(referenceId, link);
138138
}
139139
}
@@ -143,7 +143,7 @@ private void AddCallbackToComponents(IOpenApiCallback? callback, string? referen
143143
EnsureCallbacksExist();
144144
if (callback is not null && referenceId is not null && !(Components.Callbacks?.ContainsKey(referenceId) ?? false))
145145
{
146-
Components.Callbacks ??= new Dictionary<string, IOpenApiCallback>();
146+
Components.Callbacks ??= [];
147147
Components.Callbacks.Add(referenceId, callback);
148148
}
149149
}
@@ -153,7 +153,7 @@ private void AddHeaderToComponents(IOpenApiHeader? header, string? referenceId =
153153
EnsureHeadersExist();
154154
if (header is not null && referenceId is not null && !(Components.Headers?.ContainsKey(referenceId) ?? false))
155155
{
156-
Components.Headers ??= new Dictionary<string, IOpenApiHeader>();
156+
Components.Headers ??= [];
157157
Components.Headers.Add(referenceId, header);
158158
}
159159
}
@@ -163,7 +163,7 @@ private void AddExampleToComponents(IOpenApiExample? example, string? referenceI
163163
EnsureExamplesExist();
164164
if (example is not null && referenceId is not null && !(Components.Examples?.ContainsKey(referenceId) ?? false))
165165
{
166-
Components.Examples ??= new Dictionary<string, IOpenApiExample>();
166+
Components.Examples ??= [];
167167
Components.Examples.Add(referenceId, example);
168168
}
169169
}
@@ -173,7 +173,7 @@ private void AddPathItemToComponents(IOpenApiPathItem? pathItem, string? referen
173173
EnsurePathItemsExist();
174174
if (pathItem is not null && referenceId is not null && !(Components.PathItems?.ContainsKey(referenceId) ?? false))
175175
{
176-
Components.PathItems ??= new Dictionary<string, IOpenApiPathItem>();
176+
Components.PathItems ??= [];
177177
Components.PathItems.Add(referenceId, pathItem);
178178
}
179179
}
@@ -183,7 +183,7 @@ private void AddSecuritySchemeToComponents(IOpenApiSecurityScheme? securitySchem
183183
EnsureSecuritySchemesExist();
184184
if (securityScheme is not null && referenceId is not null && !(Components.SecuritySchemes?.ContainsKey(referenceId) ?? false))
185185
{
186-
Components.SecuritySchemes ??= new Dictionary<string, IOpenApiSecurityScheme>();
186+
Components.SecuritySchemes ??= [];
187187
Components.SecuritySchemes.Add(referenceId, securityScheme);
188188
}
189189
}
@@ -208,78 +208,78 @@ private void EnsureSchemasExist()
208208
{
209209
if (_target.Components is not null)
210210
{
211-
_target.Components.Schemas ??= new Dictionary<string, IOpenApiSchema>();
211+
_target.Components.Schemas ??= [];
212212
}
213213
}
214214

215215
private void EnsureParametersExist()
216216
{
217217
if (_target.Components is not null)
218218
{
219-
_target.Components.Parameters ??= new Dictionary<string, IOpenApiParameter>();
219+
_target.Components.Parameters ??= [];
220220
}
221221
}
222222

223223
private void EnsureResponsesExist()
224224
{
225225
if (_target.Components is not null)
226226
{
227-
_target.Components.Responses ??= new Dictionary<string, IOpenApiResponse>();
227+
_target.Components.Responses ??= [];
228228
}
229229
}
230230

231231
private void EnsureRequestBodiesExist()
232232
{
233233
if (_target.Components is not null)
234234
{
235-
_target.Components.RequestBodies ??= new Dictionary<string, IOpenApiRequestBody>();
235+
_target.Components.RequestBodies ??= [];
236236
}
237237
}
238238

239239
private void EnsureExamplesExist()
240240
{
241241
if (_target.Components is not null)
242242
{
243-
_target.Components.Examples ??= new Dictionary<string, IOpenApiExample>();
243+
_target.Components.Examples ??= [];
244244
}
245245
}
246246

247247
private void EnsureHeadersExist()
248248
{
249249
if (_target.Components is not null)
250250
{
251-
_target.Components.Headers ??= new Dictionary<string, IOpenApiHeader>();
251+
_target.Components.Headers ??= [];
252252
}
253253
}
254254

255255
private void EnsureCallbacksExist()
256256
{
257257
if (_target.Components is not null)
258258
{
259-
_target.Components.Callbacks ??= new Dictionary<string, IOpenApiCallback>();
259+
_target.Components.Callbacks ??= [];
260260
}
261261
}
262262

263263
private void EnsureLinksExist()
264264
{
265265
if (_target.Components is not null)
266266
{
267-
_target.Components.Links ??= new Dictionary<string, IOpenApiLink>();
267+
_target.Components.Links ??= [];
268268
}
269269
}
270270

271271
private void EnsureSecuritySchemesExist()
272272
{
273273
if (_target.Components is not null)
274274
{
275-
_target.Components.SecuritySchemes ??= new Dictionary<string, IOpenApiSecurityScheme>();
275+
_target.Components.SecuritySchemes ??= [];
276276
}
277277
}
278278
private void EnsurePathItemsExist()
279279
{
280280
if (_target.Components is not null)
281281
{
282-
_target.Components.PathItems = new Dictionary<string, IOpenApiPathItem>();
282+
_target.Components.PathItems = [];
283283
}
284284
}
285285
}

src/Microsoft.OpenApi/Services/OpenApiFilterService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public static OpenApiDocument CreateFilteredDocument(OpenApiDocument source, Fun
111111

112112
if (result.CurrentKeys?.Operation != null && result.Operation != null && pathItem is OpenApiPathItem openApiPathItem)
113113
{
114-
openApiPathItem.Operations ??= new Dictionary<HttpMethod, OpenApiOperation>();
114+
openApiPathItem.Operations ??= [];
115115
openApiPathItem.Operations?.Add(result.CurrentKeys.Operation, result.Operation);
116116

117117
if (result.Parameters?.Any() ?? false)

test/Microsoft.OpenApi.Hidi.Tests/Formatters/PowerShellFormatterTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void FormatOperationIdsInOpenAPIDocument(string operationId, string expec
3636
Paths = new()
3737
{
3838
{ path, new OpenApiPathItem() {
39-
Operations = new Dictionary<HttpMethod, OpenApiOperation>
39+
Operations = new()
4040
{
4141
{ operationType, new() { OperationId = operationId } }
4242
}
@@ -113,7 +113,7 @@ private static OpenApiDocument GetSampleOpenApiDocument()
113113
Paths = new() {
114114
{ "/foo", new OpenApiPathItem()
115115
{
116-
Operations = new Dictionary<HttpMethod, OpenApiOperation>
116+
Operations = new()
117117
{
118118
{
119119
HttpMethod.Get, new()
@@ -125,7 +125,7 @@ private static OpenApiDocument GetSampleOpenApiDocument()
125125
{
126126
Name = "ids",
127127
In = ParameterLocation.Query,
128-
Content = new Dictionary<string, OpenApiMediaType>
128+
Content = new()
129129
{
130130
{
131131
"application/json",
@@ -144,7 +144,7 @@ private static OpenApiDocument GetSampleOpenApiDocument()
144144
}
145145
}
146146
],
147-
Extensions = new Dictionary<string, IOpenApiExtension>
147+
Extensions = new()
148148
{
149149
{
150150
"x-ms-docs-operation-type", new OpenApiAny("function")
@@ -158,12 +158,12 @@ private static OpenApiDocument GetSampleOpenApiDocument()
158158
},
159159
Components = new()
160160
{
161-
Schemas = new Dictionary<string, IOpenApiSchema>
161+
Schemas = new()
162162
{
163163
{ "TestSchema", new OpenApiSchema
164164
{
165165
Type = JsonSchemaType.Object,
166-
Properties = new Dictionary<string, IOpenApiSchema>
166+
Properties = new()
167167
{
168168
{
169169
"averageAudioDegradation", new OpenApiSchema

test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiFilterServiceTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void TestPredicateFiltersUsingRelativeRequestUrls()
8383
Paths = new()
8484
{
8585
{"/foo", new OpenApiPathItem() {
86-
Operations = new Dictionary<HttpMethod, OpenApiOperation>
86+
Operations = new()
8787
{
8888
{ HttpMethod.Get, new() },
8989
{ HttpMethod.Patch, new() },
@@ -121,7 +121,7 @@ public void CreateFilteredDocumentUsingPredicateFromRequestUrl()
121121
{
122122
["/test/{id}"] = new OpenApiPathItem()
123123
{
124-
Operations = new Dictionary<HttpMethod, OpenApiOperation>
124+
Operations = new()
125125
{
126126
{ HttpMethod.Get, new() },
127127
{ HttpMethod.Patch, new() }

test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void CreateFilteredDocumentOnMinimalOpenApi()
4545
{
4646
["/test"] = new OpenApiPathItem()
4747
{
48-
Operations = new Dictionary<HttpMethod, OpenApiOperation>
48+
Operations = new()
4949
{
5050
[HttpMethod.Get] = new OpenApiOperation()
5151
}

0 commit comments

Comments
 (0)