Skip to content

Commit

Permalink
fix: missing defensive programming in copy constructors
Browse files Browse the repository at this point in the history
fix: removes extraneuous null prop op in copy constructor

Signed-off-by: Vincent Biret <[email protected]>
  • Loading branch information
baywet committed Jan 30, 2025
1 parent 7ac149c commit 227d99d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/Microsoft.OpenApi/Models/OpenApiCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public OpenApiCallback() { }
/// </summary>
internal OpenApiCallback(IOpenApiCallback callback)
{
Utils.CheckArgumentNull(callback);
PathItems = callback?.PathItems != null ? new(callback?.PathItems) : null;
Extensions = callback?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(callback.Extensions) : null;
}
Expand Down
25 changes: 13 additions & 12 deletions src/Microsoft.OpenApi/Models/OpenApiHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,19 @@ public OpenApiHeader() { }
/// </summary>
internal OpenApiHeader(IOpenApiHeader header)
{
Description = header?.Description ?? Description;
Required = header?.Required ?? Required;
Deprecated = header?.Deprecated ?? Deprecated;
AllowEmptyValue = header?.AllowEmptyValue ?? AllowEmptyValue;
Style = header?.Style ?? Style;
Explode = header?.Explode ?? Explode;
AllowReserved = header?.AllowReserved ?? AllowReserved;
Schema = header?.Schema?.CreateShallowCopy();
Example = header?.Example != null ? JsonNodeCloneHelper.Clone(header.Example) : null;
Examples = header?.Examples != null ? new Dictionary<string, IOpenApiExample>(header.Examples) : null;
Content = header?.Content != null ? new Dictionary<string, OpenApiMediaType>(header.Content) : null;
Extensions = header?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(header.Extensions) : null;
Utils.CheckArgumentNull(header);
Description = header.Description ?? Description;
Required = header.Required;
Deprecated = header.Deprecated;
AllowEmptyValue = header.AllowEmptyValue;
Style = header.Style ?? Style;
Explode = header.Explode;
AllowReserved = header.AllowReserved;
Schema = header.Schema.CreateShallowCopy();
Example = header.Example != null ? JsonNodeCloneHelper.Clone(header.Example) : null;
Examples = header.Examples != null ? new Dictionary<string, IOpenApiExample>(header.Examples) : null;
Content = header.Content != null ? new Dictionary<string, OpenApiMediaType>(header.Content) : null;
Extensions = header.Extensions != null ? new Dictionary<string, IOpenApiExtension>(header.Extensions) : null;
}

/// <summary>
Expand Down
12 changes: 6 additions & 6 deletions src/Microsoft.OpenApi/Models/OpenApiPathItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ public OpenApiPathItem() { }
internal OpenApiPathItem(IOpenApiPathItem pathItem)
{
Utils.CheckArgumentNull(pathItem);
Summary = pathItem?.Summary ?? Summary;
Description = pathItem?.Description ?? Description;
Operations = pathItem?.Operations != null ? new Dictionary<OperationType, OpenApiOperation>(pathItem.Operations) : null;
Servers = pathItem?.Servers != null ? new List<OpenApiServer>(pathItem.Servers) : null;
Parameters = pathItem?.Parameters != null ? new List<IOpenApiParameter>(pathItem.Parameters) : null;
Extensions = pathItem?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(pathItem.Extensions) : null;
Summary = pathItem.Summary ?? Summary;
Description = pathItem.Description ?? Description;
Operations = pathItem.Operations != null ? new Dictionary<OperationType, OpenApiOperation>(pathItem.Operations) : null;
Servers = pathItem.Servers != null ? new List<OpenApiServer>(pathItem.Servers) : null;
Parameters = pathItem.Parameters != null ? new List<IOpenApiParameter>(pathItem.Parameters) : null;
Extensions = pathItem.Extensions != null ? new Dictionary<string, IOpenApiExtension>(pathItem.Extensions) : null;
}

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public OpenApiRequestBody() { }
internal OpenApiRequestBody(IOpenApiRequestBody requestBody)
{
Utils.CheckArgumentNull(requestBody);
Description = requestBody?.Description ?? Description;
Required = requestBody?.Required ?? Required;
Content = requestBody?.Content != null ? new Dictionary<string, OpenApiMediaType>(requestBody.Content) : null;
Extensions = requestBody?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(requestBody.Extensions) : null;
Description = requestBody.Description ?? Description;
Required = requestBody.Required;
Content = requestBody.Content != null ? new Dictionary<string, OpenApiMediaType>(requestBody.Content) : null;
Extensions = requestBody.Extensions != null ? new Dictionary<string, IOpenApiExtension>(requestBody.Extensions) : null;
}

/// <summary>
Expand Down
18 changes: 9 additions & 9 deletions src/Microsoft.OpenApi/Models/OpenApiSecurityScheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ public OpenApiSecurityScheme() { }
internal OpenApiSecurityScheme(IOpenApiSecurityScheme securityScheme)
{
Utils.CheckArgumentNull(securityScheme);
Type = securityScheme?.Type;
Description = securityScheme?.Description ?? Description;
Name = securityScheme?.Name ?? Name;
In = securityScheme?.In;
Scheme = securityScheme?.Scheme ?? Scheme;
BearerFormat = securityScheme?.BearerFormat ?? BearerFormat;
Flows = securityScheme?.Flows != null ? new(securityScheme?.Flows) : null;
OpenIdConnectUrl = securityScheme?.OpenIdConnectUrl != null ? new Uri(securityScheme.OpenIdConnectUrl.OriginalString, UriKind.RelativeOrAbsolute) : null;
Extensions = securityScheme?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(securityScheme.Extensions) : null;
Type = securityScheme.Type;
Description = securityScheme.Description ?? Description;
Name = securityScheme.Name ?? Name;
In = securityScheme.In;
Scheme = securityScheme.Scheme ?? Scheme;
BearerFormat = securityScheme.BearerFormat ?? BearerFormat;
Flows = securityScheme.Flows != null ? new(securityScheme.Flows) : null;
OpenIdConnectUrl = securityScheme.OpenIdConnectUrl != null ? new Uri(securityScheme.OpenIdConnectUrl.OriginalString, UriKind.RelativeOrAbsolute) : null;
Extensions = securityScheme.Extensions != null ? new Dictionary<string, IOpenApiExtension>(securityScheme.Extensions) : null;
}

/// <summary>
Expand Down

0 comments on commit 227d99d

Please sign in to comment.