Skip to content

Commit 227d99d

Browse files
committed
fix: missing defensive programming in copy constructors
fix: removes extraneuous null prop op in copy constructor Signed-off-by: Vincent Biret <[email protected]>
1 parent 7ac149c commit 227d99d

File tree

5 files changed

+33
-31
lines changed

5 files changed

+33
-31
lines changed

src/Microsoft.OpenApi/Models/OpenApiCallback.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public OpenApiCallback() { }
3535
/// </summary>
3636
internal OpenApiCallback(IOpenApiCallback callback)
3737
{
38+
Utils.CheckArgumentNull(callback);
3839
PathItems = callback?.PathItems != null ? new(callback?.PathItems) : null;
3940
Extensions = callback?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(callback.Extensions) : null;
4041
}

src/Microsoft.OpenApi/Models/OpenApiHeader.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,19 @@ public OpenApiHeader() { }
6565
/// </summary>
6666
internal OpenApiHeader(IOpenApiHeader header)
6767
{
68-
Description = header?.Description ?? Description;
69-
Required = header?.Required ?? Required;
70-
Deprecated = header?.Deprecated ?? Deprecated;
71-
AllowEmptyValue = header?.AllowEmptyValue ?? AllowEmptyValue;
72-
Style = header?.Style ?? Style;
73-
Explode = header?.Explode ?? Explode;
74-
AllowReserved = header?.AllowReserved ?? AllowReserved;
75-
Schema = header?.Schema?.CreateShallowCopy();
76-
Example = header?.Example != null ? JsonNodeCloneHelper.Clone(header.Example) : null;
77-
Examples = header?.Examples != null ? new Dictionary<string, IOpenApiExample>(header.Examples) : null;
78-
Content = header?.Content != null ? new Dictionary<string, OpenApiMediaType>(header.Content) : null;
79-
Extensions = header?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(header.Extensions) : null;
68+
Utils.CheckArgumentNull(header);
69+
Description = header.Description ?? Description;
70+
Required = header.Required;
71+
Deprecated = header.Deprecated;
72+
AllowEmptyValue = header.AllowEmptyValue;
73+
Style = header.Style ?? Style;
74+
Explode = header.Explode;
75+
AllowReserved = header.AllowReserved;
76+
Schema = header.Schema.CreateShallowCopy();
77+
Example = header.Example != null ? JsonNodeCloneHelper.Clone(header.Example) : null;
78+
Examples = header.Examples != null ? new Dictionary<string, IOpenApiExample>(header.Examples) : null;
79+
Content = header.Content != null ? new Dictionary<string, OpenApiMediaType>(header.Content) : null;
80+
Extensions = header.Extensions != null ? new Dictionary<string, IOpenApiExtension>(header.Extensions) : null;
8081
}
8182

8283
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiPathItem.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ public OpenApiPathItem() { }
5555
internal OpenApiPathItem(IOpenApiPathItem pathItem)
5656
{
5757
Utils.CheckArgumentNull(pathItem);
58-
Summary = pathItem?.Summary ?? Summary;
59-
Description = pathItem?.Description ?? Description;
60-
Operations = pathItem?.Operations != null ? new Dictionary<OperationType, OpenApiOperation>(pathItem.Operations) : null;
61-
Servers = pathItem?.Servers != null ? new List<OpenApiServer>(pathItem.Servers) : null;
62-
Parameters = pathItem?.Parameters != null ? new List<IOpenApiParameter>(pathItem.Parameters) : null;
63-
Extensions = pathItem?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(pathItem.Extensions) : null;
58+
Summary = pathItem.Summary ?? Summary;
59+
Description = pathItem.Description ?? Description;
60+
Operations = pathItem.Operations != null ? new Dictionary<OperationType, OpenApiOperation>(pathItem.Operations) : null;
61+
Servers = pathItem.Servers != null ? new List<OpenApiServer>(pathItem.Servers) : null;
62+
Parameters = pathItem.Parameters != null ? new List<IOpenApiParameter>(pathItem.Parameters) : null;
63+
Extensions = pathItem.Extensions != null ? new Dictionary<string, IOpenApiExtension>(pathItem.Extensions) : null;
6464
}
6565

6666
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ public OpenApiRequestBody() { }
4141
internal OpenApiRequestBody(IOpenApiRequestBody requestBody)
4242
{
4343
Utils.CheckArgumentNull(requestBody);
44-
Description = requestBody?.Description ?? Description;
45-
Required = requestBody?.Required ?? Required;
46-
Content = requestBody?.Content != null ? new Dictionary<string, OpenApiMediaType>(requestBody.Content) : null;
47-
Extensions = requestBody?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(requestBody.Extensions) : null;
44+
Description = requestBody.Description ?? Description;
45+
Required = requestBody.Required;
46+
Content = requestBody.Content != null ? new Dictionary<string, OpenApiMediaType>(requestBody.Content) : null;
47+
Extensions = requestBody.Extensions != null ? new Dictionary<string, IOpenApiExtension>(requestBody.Extensions) : null;
4848
}
4949

5050
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiSecurityScheme.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ public OpenApiSecurityScheme() { }
5353
internal OpenApiSecurityScheme(IOpenApiSecurityScheme securityScheme)
5454
{
5555
Utils.CheckArgumentNull(securityScheme);
56-
Type = securityScheme?.Type;
57-
Description = securityScheme?.Description ?? Description;
58-
Name = securityScheme?.Name ?? Name;
59-
In = securityScheme?.In;
60-
Scheme = securityScheme?.Scheme ?? Scheme;
61-
BearerFormat = securityScheme?.BearerFormat ?? BearerFormat;
62-
Flows = securityScheme?.Flows != null ? new(securityScheme?.Flows) : null;
63-
OpenIdConnectUrl = securityScheme?.OpenIdConnectUrl != null ? new Uri(securityScheme.OpenIdConnectUrl.OriginalString, UriKind.RelativeOrAbsolute) : null;
64-
Extensions = securityScheme?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(securityScheme.Extensions) : null;
56+
Type = securityScheme.Type;
57+
Description = securityScheme.Description ?? Description;
58+
Name = securityScheme.Name ?? Name;
59+
In = securityScheme.In;
60+
Scheme = securityScheme.Scheme ?? Scheme;
61+
BearerFormat = securityScheme.BearerFormat ?? BearerFormat;
62+
Flows = securityScheme.Flows != null ? new(securityScheme.Flows) : null;
63+
OpenIdConnectUrl = securityScheme.OpenIdConnectUrl != null ? new Uri(securityScheme.OpenIdConnectUrl.OriginalString, UriKind.RelativeOrAbsolute) : null;
64+
Extensions = securityScheme.Extensions != null ? new Dictionary<string, IOpenApiExtension>(securityScheme.Extensions) : null;
6565
}
6666

6767
/// <summary>

0 commit comments

Comments
 (0)