Skip to content

Commit 4e97803

Browse files
committed
chore: clean up code; initialize collections where applicable
1 parent 1fa4647 commit 4e97803

File tree

11 files changed

+54
-34
lines changed

11 files changed

+54
-34
lines changed

src/Microsoft.OpenApi/Models/OpenApiDiscriminator.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,13 @@ private void SerializeInternal(IOpenApiWriter writer)
8181
writer.WriteProperty(OpenApiConstants.PropertyName, PropertyName);
8282

8383
// mapping
84-
writer.WriteOptionalMap(OpenApiConstants.Mapping, Mapping, (w, s) => w.WriteValue(s));
84+
writer.WriteOptionalMap(OpenApiConstants.Mapping, Mapping, (w, s) =>
85+
{
86+
if (!string.IsNullOrEmpty(s.Reference.ReferenceV3) && s.Reference.ReferenceV3 is not null)
87+
{
88+
w.WriteValue(s.Reference.ReferenceV3);
89+
}
90+
});
8591
}
8692

8793
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void RegisterComponents()
4949
/// <summary>
5050
/// An array of Server Objects, which provide connectivity information to a target server.
5151
/// </summary>
52-
public IList<OpenApiServer>? Servers { get; set; }
52+
public IList<OpenApiServer>? Servers { get; set; } = [];
5353

5454
/// <summary>
5555
/// REQUIRED. The available paths and operations for the API.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.OpenApi.Models.References;
1111
using Microsoft.OpenApi.Models.Interfaces;
1212
using System;
13+
using Microsoft.OpenApi.Interfaces;
1314

1415
namespace Microsoft.OpenApi.Reader.V2
1516
{
@@ -238,8 +239,9 @@ internal static IOpenApiRequestBody CreateRequestBody(
238239
Extensions = bodyParameter.Extensions
239240
};
240241

241-
if (requestBody.Extensions is not null && bodyParameter.Name is not null)
242+
if (bodyParameter.Name is not null)
242243
{
244+
requestBody.Extensions ??= new Dictionary<string, IOpenApiExtension>();
243245
requestBody.Extensions[OpenApiConstants.BodyName] = new OpenApiAny(bodyParameter.Name);
244246
}
245247
return requestBody;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ private static void ProcessProduces(MapNode mapNode, OpenApiResponse response, P
7575
?? context.DefaultContentType ?? new List<string> { "application/octet-stream" };
7676

7777
var schema = context.GetFromTempStorage<IOpenApiSchema>(TempStorageKeys.ResponseSchema, response);
78-
var examples = context.GetFromTempStorage<Dictionary<string, IOpenApiExample>>(TempStorageKeys.Examples, response)
79-
?? new Dictionary<string, IOpenApiExample>();
78+
var examples = context.GetFromTempStorage<Dictionary<string, IOpenApiExample>>(TempStorageKeys.Examples, response);
8079

8180
foreach (var produce in produces)
8281
{

src/Microsoft.OpenApi/Services/CopyReferences.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ private void AddSchemaToComponents(IOpenApiSchema? schema, string? referenceId =
8989
{
9090
EnsureComponentsExist();
9191
EnsureSchemasExist();
92-
if (Components.Schemas is not null && referenceId is not null && schema is not null && !Components.Schemas.ContainsKey(referenceId))
92+
Components.Schemas ??= new Dictionary<string, IOpenApiSchema>();
93+
if (referenceId is not null && schema is not null && !Components.Schemas.ContainsKey(referenceId))
9394
{
9495
Components.Schemas.Add(referenceId, schema);
9596
}
@@ -99,7 +100,8 @@ private void AddParameterToComponents(IOpenApiParameter? parameter, string? refe
99100
{
100101
EnsureComponentsExist();
101102
EnsureParametersExist();
102-
if (Components.Parameters is not null && parameter is not null && referenceId is not null && !Components.Parameters.ContainsKey(referenceId))
103+
Components.Parameters ??= new Dictionary<string, IOpenApiParameter>();
104+
if (parameter is not null && referenceId is not null && !Components.Parameters.ContainsKey(referenceId))
103105
{
104106
Components.Parameters.Add(referenceId, parameter);
105107
}
@@ -109,7 +111,8 @@ private void AddResponseToComponents(IOpenApiResponse? response, string? referen
109111
{
110112
EnsureComponentsExist();
111113
EnsureResponsesExist();
112-
if (Components.Responses is not null && referenceId is not null && response is not null && !Components.Responses.ContainsKey(referenceId))
114+
Components.Responses ??= new Dictionary<string, IOpenApiResponse>();
115+
if (referenceId is not null && response is not null && !Components.Responses.ContainsKey(referenceId))
113116
{
114117
Components.Responses.Add(referenceId, response);
115118
}
@@ -118,7 +121,8 @@ private void AddRequestBodyToComponents(IOpenApiRequestBody? requestBody, string
118121
{
119122
EnsureComponentsExist();
120123
EnsureRequestBodiesExist();
121-
if (Components.RequestBodies is not null && requestBody is not null && referenceId is not null && !Components.RequestBodies.ContainsKey(referenceId))
124+
Components.RequestBodies ??= new Dictionary<string, IOpenApiRequestBody>();
125+
if (requestBody is not null && referenceId is not null && !Components.RequestBodies.ContainsKey(referenceId))
122126
{
123127
Components.RequestBodies.Add(referenceId, requestBody);
124128
}
@@ -127,7 +131,8 @@ private void AddLinkToComponents(IOpenApiLink? link, string? referenceId = null)
127131
{
128132
EnsureComponentsExist();
129133
EnsureLinksExist();
130-
if (Components.Links is not null && link is not null && referenceId is not null && !Components.Links.ContainsKey(referenceId))
134+
Components.Links ??= new Dictionary<string, IOpenApiLink>();
135+
if (link is not null && referenceId is not null && !Components.Links.ContainsKey(referenceId))
131136
{
132137
Components.Links.Add(referenceId, link);
133138
}
@@ -136,7 +141,8 @@ private void AddCallbackToComponents(IOpenApiCallback? callback, string? referen
136141
{
137142
EnsureComponentsExist();
138143
EnsureCallbacksExist();
139-
if (Components.Callbacks is not null && callback is not null && referenceId is not null && !Components.Callbacks.ContainsKey(referenceId))
144+
Components.Callbacks ??= new Dictionary<string, IOpenApiCallback>();
145+
if (callback is not null && referenceId is not null && !Components.Callbacks.ContainsKey(referenceId))
140146
{
141147
Components.Callbacks.Add(referenceId, callback);
142148
}
@@ -145,7 +151,8 @@ private void AddHeaderToComponents(IOpenApiHeader? header, string? referenceId =
145151
{
146152
EnsureComponentsExist();
147153
EnsureHeadersExist();
148-
if (Components.Headers is not null && header is not null && referenceId is not null && !Components.Headers.ContainsKey(referenceId))
154+
Components.Headers ??= new Dictionary<string, IOpenApiHeader>();
155+
if (header is not null && referenceId is not null && !Components.Headers.ContainsKey(referenceId))
149156
{
150157
Components.Headers.Add(referenceId, header);
151158
}
@@ -154,7 +161,8 @@ private void AddExampleToComponents(IOpenApiExample? example, string? referenceI
154161
{
155162
EnsureComponentsExist();
156163
EnsureExamplesExist();
157-
if (Components.Examples is not null && example is not null && referenceId is not null && !Components.Examples.ContainsKey(referenceId))
164+
Components.Examples ??= new Dictionary<string, IOpenApiExample>();
165+
if (example is not null && referenceId is not null && !Components.Examples.ContainsKey(referenceId))
158166
{
159167
Components.Examples.Add(referenceId, example);
160168
}
@@ -163,7 +171,8 @@ private void AddPathItemToComponents(IOpenApiPathItem? pathItem, string? referen
163171
{
164172
EnsureComponentsExist();
165173
EnsurePathItemsExist();
166-
if (Components.PathItems is not null && pathItem is not null && referenceId is not null && !Components.PathItems.ContainsKey(referenceId))
174+
Components.PathItems ??= new Dictionary<string, IOpenApiPathItem>();
175+
if (pathItem is not null && referenceId is not null && !Components.PathItems.ContainsKey(referenceId))
167176
{
168177
Components.PathItems.Add(referenceId, pathItem);
169178
}
@@ -172,7 +181,8 @@ private void AddSecuritySchemeToComponents(IOpenApiSecurityScheme? securitySchem
172181
{
173182
EnsureComponentsExist();
174183
EnsureSecuritySchemesExist();
175-
if (Components.SecuritySchemes is not null && securityScheme is not null && referenceId is not null && !Components.SecuritySchemes.ContainsKey(referenceId))
184+
Components.SecuritySchemes ??= new Dictionary<string, IOpenApiSecurityScheme>();
185+
if (securityScheme is not null && referenceId is not null && !Components.SecuritySchemes.ContainsKey(referenceId))
176186
{
177187
Components.SecuritySchemes.Add(referenceId, securityScheme);
178188
}

src/Microsoft.OpenApi/Services/OpenApiFilterService.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,19 @@ public static OpenApiDocument CreateFilteredDocument(OpenApiDocument source, Fun
109109
}
110110
}
111111

112-
if (result.CurrentKeys?.Operation != null && result.Operation != null)
112+
if (result.CurrentKeys?.Operation != null && result.Operation != null && pathItem is OpenApiPathItem openApiPathItem)
113113
{
114-
pathItem?.Operations?.Add(result.CurrentKeys.Operation, result.Operation);
114+
openApiPathItem.Operations ??= new Dictionary<HttpMethod, OpenApiOperation>();
115+
openApiPathItem.Operations?.Add(result.CurrentKeys.Operation, result.Operation);
115116

116117
if (result.Parameters?.Any() ?? false)
117118
{
119+
openApiPathItem.Parameters ??= [];
118120
foreach (var parameter in result.Parameters)
119121
{
120-
if (pathItem?.Parameters is not null && !pathItem.Parameters.Contains(parameter))
122+
if (openApiPathItem?.Parameters is not null && !openApiPathItem.Parameters.Contains(parameter))
121123
{
122-
pathItem.Parameters.Add(parameter);
124+
openApiPathItem.Parameters.Add(parameter);
123125
}
124126
}
125127
}

src/Microsoft.OpenApi/Validations/Rules/OpenApiContactRules.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ public static class OpenApiContactRules
2121
(context, item) =>
2222
{
2323
context.Enter("email");
24-
if (item is {Email: not null})
24+
if (item is {Email: not null} && !item.Email.IsEmailAddress())
2525
{
26-
if (!item.Email.IsEmailAddress())
27-
{
28-
context.CreateError(nameof(EmailMustBeEmailFormat),
29-
String.Format(SRResource.Validation_StringMustBeEmailAddress, item.Email));
30-
}
26+
context.CreateError(nameof(EmailMustBeEmailFormat),
27+
String.Format(SRResource.Validation_StringMustBeEmailAddress, item.Email));
3128
}
3229
context.Exit();
3330
});

src/Microsoft.OpenApi/Validations/Rules/OpenApiExtensionRules.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public static class OpenApiExtensibleRules
2929
{
3030
context.CreateError(nameof(ExtensionNameMustStartWithXDash),
3131
string.Format(SRResource.Validation_ExtensionNameMustBeginWithXDash, extensible, context.PathString));
32-
}
33-
context.Exit();
34-
}
32+
}
33+
}
34+
context.Exit();
3535
});
3636
}
3737
}

src/Microsoft.OpenApi/Validations/Rules/OpenApiServerRules.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public static class OpenApiServerRules
3636
context.Enter(variable.Key);
3737
ValidateServerVariableRequiredFields(context, variable.Key, variable.Value);
3838
context.Exit();
39-
}
40-
context.Exit();
41-
}
39+
}
40+
}
41+
context.Exit();
4242
});
4343

4444
// add more rules

test/Microsoft.OpenApi.Tests/Models/References/OpenApiHeaderReferenceTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
using Microsoft.OpenApi.Writers;
1313
using VerifyXunit;
1414
using Xunit;
15+
using System.Collections.Generic;
16+
using Microsoft.OpenApi.Models.Interfaces;
1517

1618
namespace Microsoft.OpenApi.Tests.Models.References
1719
{
@@ -171,7 +173,7 @@ public void OpenApiHeaderTargetShouldResolveReference()
171173
{
172174
Components = new OpenApiComponents
173175
{
174-
Headers =
176+
Headers = new Dictionary<string, IOpenApiHeader>
175177
{
176178
{ "header1", new OpenApiHeader
177179
{

0 commit comments

Comments
 (0)