diff --git a/src/Microsoft.OpenApi/Helpers/JsonNodeCloneHelper.cs b/src/Microsoft.OpenApi/Helpers/JsonNodeCloneHelper.cs index 9385f8ceb..32025d198 100644 --- a/src/Microsoft.OpenApi/Helpers/JsonNodeCloneHelper.cs +++ b/src/Microsoft.OpenApi/Helpers/JsonNodeCloneHelper.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. using System.Text.Json; +using System.Text.Json.Nodes; using System.Text.Json.Serialization; using Json.Schema; using Microsoft.OpenApi.Any; @@ -17,15 +18,24 @@ internal static class JsonNodeCloneHelper internal static OpenApiAny Clone(OpenApiAny value) { - var jsonString = Serialize(value); - var result = JsonSerializer.Deserialize(jsonString, options); + var jsonString = Serialize(value?.Node); + if (string.IsNullOrEmpty(jsonString)) + { + return null; + } - return result; + var result = JsonSerializer.Deserialize(jsonString, options); + return new OpenApiAny(result); } internal static JsonSchema CloneJsonSchema(JsonSchema schema) { var jsonString = Serialize(schema); + if (string.IsNullOrEmpty(jsonString)) + { + return null; + } + var result = JsonSerializer.Deserialize(jsonString, options); return result; } diff --git a/src/Microsoft.OpenApi/Models/OpenApiExample.cs b/src/Microsoft.OpenApi/Models/OpenApiExample.cs index 648004ab4..b0e76ca90 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiExample.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiExample.cs @@ -68,10 +68,10 @@ public OpenApiExample(OpenApiExample example) { Summary = example?.Summary ?? Summary; Description = example?.Description ?? Description; - Value = example?.Value ?? JsonNodeCloneHelper.Clone(example?.Value); + Value = example?.Value != null ? JsonNodeCloneHelper.Clone(example.Value) : null; ExternalValue = example?.ExternalValue ?? ExternalValue; Extensions = example?.Extensions != null ? new Dictionary(example.Extensions) : null; - Reference = example?.Reference != null ? new(example?.Reference) : null; + Reference = example?.Reference != null ? new(example.Reference) : null; UnresolvedReference = example?.UnresolvedReference ?? UnresolvedReference; } diff --git a/src/Microsoft.OpenApi/Models/OpenApiHeader.cs b/src/Microsoft.OpenApi/Models/OpenApiHeader.cs index 9655bf587..d2bb6267c 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiHeader.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiHeader.cs @@ -114,8 +114,8 @@ public OpenApiHeader(OpenApiHeader header) Style = header?.Style ?? Style; Explode = header?.Explode ?? Explode; AllowReserved = header?.AllowReserved ?? AllowReserved; - Schema = header?.Schema != null ? JsonNodeCloneHelper.CloneJsonSchema(header?.Schema) : null; - Example = header?.Example != null ? JsonNodeCloneHelper.Clone(header?.Example) : null; + Schema = header?.Schema != null ? JsonNodeCloneHelper.CloneJsonSchema(header.Schema) : null; + 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/OpenApiMediaType.cs b/src/Microsoft.OpenApi/Models/OpenApiMediaType.cs index 353f88f11..cb97f3185 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiMediaType.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiMediaType.cs @@ -62,8 +62,8 @@ public OpenApiMediaType() { } /// public OpenApiMediaType(OpenApiMediaType mediaType) { - _schema = JsonNodeCloneHelper.CloneJsonSchema(mediaType?.Schema); - Example = JsonNodeCloneHelper.Clone(mediaType?.Example); + Schema = mediaType?.Schema != null ? JsonNodeCloneHelper.CloneJsonSchema(mediaType.Schema) : null; + Example = mediaType?.Example != null ? JsonNodeCloneHelper.Clone(mediaType.Example) : null; Examples = mediaType?.Examples != null ? new Dictionary(mediaType.Examples) : null; Encoding = mediaType?.Encoding != null ? new Dictionary(mediaType.Encoding) : null; Extensions = mediaType?.Extensions != null ? new Dictionary(mediaType.Extensions) : null; diff --git a/src/Microsoft.OpenApi/Models/OpenApiParameter.cs b/src/Microsoft.OpenApi/Models/OpenApiParameter.cs index 29003da51..a7ad97b2d 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiParameter.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiParameter.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. using System; @@ -168,9 +168,9 @@ public OpenApiParameter(OpenApiParameter parameter) Style = parameter?.Style ?? Style; Explode = parameter?.Explode ?? Explode; AllowReserved = parameter?.AllowReserved ?? AllowReserved; - Schema = parameter?.Schema != null ? JsonNodeCloneHelper.CloneJsonSchema(parameter?.Schema) : null; + Schema = parameter?.Schema != null ? JsonNodeCloneHelper.CloneJsonSchema(parameter.Schema) : null; Examples = parameter?.Examples != null ? new Dictionary(parameter.Examples) : null; - Example = parameter?.Example != null ? JsonNodeCloneHelper.Clone(parameter?.Example) : null; + Example = parameter?.Example != null ? JsonNodeCloneHelper.Clone(parameter.Example) : null; Content = parameter?.Content != null ? new Dictionary(parameter.Content) : null; Extensions = parameter?.Extensions != null ? new Dictionary(parameter.Extensions) : null; AllowEmptyValue = parameter?.AllowEmptyValue ?? AllowEmptyValue; diff --git a/src/Microsoft.OpenApi/Models/References/OpenApiExampleReference.cs b/src/Microsoft.OpenApi/Models/References/OpenApiExampleReference.cs index b177bc059..eeee360a9 100644 --- a/src/Microsoft.OpenApi/Models/References/OpenApiExampleReference.cs +++ b/src/Microsoft.OpenApi/Models/References/OpenApiExampleReference.cs @@ -125,7 +125,7 @@ public override void SerializeAsV31(IOpenApiWriter writer) private void SerializeInternal(IOpenApiWriter writer, Action action) { - Utils.CheckArgumentNull(writer);; + Utils.CheckArgumentNull(writer); action(writer, Target); } } diff --git a/src/Microsoft.OpenApi/Reader/Services/OpenApiRemoteReferenceCollector.cs b/src/Microsoft.OpenApi/Reader/Services/OpenApiRemoteReferenceCollector.cs index 4d44b98a9..bb66cf9b2 100644 --- a/src/Microsoft.OpenApi/Reader/Services/OpenApiRemoteReferenceCollector.cs +++ b/src/Microsoft.OpenApi/Reader/Services/OpenApiRemoteReferenceCollector.cs @@ -1,7 +1,6 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. -using System; using System.Collections.Generic; using Microsoft.OpenApi.Interfaces; using Microsoft.OpenApi.Models; diff --git a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiExampleReferenceTests.cs b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiExampleReferenceTests.cs index 28a91aa8e..a10fba5ff 100644 --- a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiExampleReferenceTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiExampleReferenceTests.cs @@ -133,7 +133,7 @@ public OpenApiExampleReferenceTests() [Fact] public void ExampleReferenceResolutionWorks() - { + { // Assert Assert.NotNull(_localExampleReference.Value); Assert.Equal("[{\"id\":1,\"name\":\"John Doe\"}]", _localExampleReference.Value.Node.ToJsonString());