From 227d99d23557fab82fcb7eb7d6e8fa34b486719d Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Thu, 30 Jan 2025 09:48:50 -0500 Subject: [PATCH] fix: missing defensive programming in copy constructors fix: removes extraneuous null prop op in copy constructor Signed-off-by: Vincent Biret --- .../Models/OpenApiCallback.cs | 1 + src/Microsoft.OpenApi/Models/OpenApiHeader.cs | 25 ++++++++++--------- .../Models/OpenApiPathItem.cs | 12 ++++----- .../Models/OpenApiRequestBody.cs | 8 +++--- .../Models/OpenApiSecurityScheme.cs | 18 ++++++------- 5 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/Microsoft.OpenApi/Models/OpenApiCallback.cs b/src/Microsoft.OpenApi/Models/OpenApiCallback.cs index cd74bfd75..96f5c5cf4 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiCallback.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiCallback.cs @@ -35,6 +35,7 @@ public OpenApiCallback() { } /// internal OpenApiCallback(IOpenApiCallback callback) { + Utils.CheckArgumentNull(callback); PathItems = callback?.PathItems != null ? new(callback?.PathItems) : null; Extensions = callback?.Extensions != null ? new Dictionary(callback.Extensions) : null; } diff --git a/src/Microsoft.OpenApi/Models/OpenApiHeader.cs b/src/Microsoft.OpenApi/Models/OpenApiHeader.cs index 3e6bd1944..dd7a0ec84 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiHeader.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiHeader.cs @@ -65,18 +65,19 @@ public OpenApiHeader() { } /// 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(header.Examples) : null; - Content = header?.Content != null ? new Dictionary(header.Content) : null; - Extensions = header?.Extensions != null ? new Dictionary(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(header.Examples) : null; + Content = header.Content != null ? new Dictionary(header.Content) : null; + Extensions = header.Extensions != null ? new Dictionary(header.Extensions) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiPathItem.cs b/src/Microsoft.OpenApi/Models/OpenApiPathItem.cs index 88ea160bf..f3baa5743 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiPathItem.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiPathItem.cs @@ -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(pathItem.Operations) : null; - Servers = pathItem?.Servers != null ? new List(pathItem.Servers) : null; - Parameters = pathItem?.Parameters != null ? new List(pathItem.Parameters) : null; - Extensions = pathItem?.Extensions != null ? new Dictionary(pathItem.Extensions) : null; + Summary = pathItem.Summary ?? Summary; + Description = pathItem.Description ?? Description; + Operations = pathItem.Operations != null ? new Dictionary(pathItem.Operations) : null; + Servers = pathItem.Servers != null ? new List(pathItem.Servers) : null; + Parameters = pathItem.Parameters != null ? new List(pathItem.Parameters) : null; + Extensions = pathItem.Extensions != null ? new Dictionary(pathItem.Extensions) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs b/src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs index 5ec43a961..95f86dba3 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs @@ -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(requestBody.Content) : null; - Extensions = requestBody?.Extensions != null ? new Dictionary(requestBody.Extensions) : null; + Description = requestBody.Description ?? Description; + Required = requestBody.Required; + Content = requestBody.Content != null ? new Dictionary(requestBody.Content) : null; + Extensions = requestBody.Extensions != null ? new Dictionary(requestBody.Extensions) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiSecurityScheme.cs b/src/Microsoft.OpenApi/Models/OpenApiSecurityScheme.cs index 06a4dd4e1..dddbbffec 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiSecurityScheme.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiSecurityScheme.cs @@ -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(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(securityScheme.Extensions) : null; } ///