Skip to content

Commit

Permalink
Merge pull request #1616 from microsoft/is/v2-fixes
Browse files Browse the repository at this point in the history
Fixes cloning of `JsonNode` objects
  • Loading branch information
irvinesunday authored Apr 5, 2024
2 parents 77cd25a + f884968 commit b9d5360
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 16 deletions.
16 changes: 13 additions & 3 deletions src/Microsoft.OpenApi/Helpers/JsonNodeCloneHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,15 +18,24 @@ internal static class JsonNodeCloneHelper

internal static OpenApiAny Clone(OpenApiAny value)
{
var jsonString = Serialize(value);
var result = JsonSerializer.Deserialize<OpenApiAny>(jsonString, options);
var jsonString = Serialize(value?.Node);
if (string.IsNullOrEmpty(jsonString))
{
return null;
}

return result;
var result = JsonSerializer.Deserialize<JsonNode>(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<JsonSchema>(jsonString, options);
return result;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, IOpenApiExtension>(example.Extensions) : null;
Reference = example?.Reference != null ? new(example?.Reference) : null;
Reference = example?.Reference != null ? new(example.Reference) : null;
UnresolvedReference = example?.UnresolvedReference ?? UnresolvedReference;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, OpenApiExample>(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;
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiMediaType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public OpenApiMediaType() { }
/// </summary>
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<string, OpenApiExample>(mediaType.Examples) : null;
Encoding = mediaType?.Encoding != null ? new Dictionary<string, OpenApiEncoding>(mediaType.Encoding) : null;
Extensions = mediaType?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(mediaType.Extensions) : null;
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.OpenApi/Models/OpenApiParameter.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<string, OpenApiExample>(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<string, OpenApiMediaType>(parameter.Content) : null;
Extensions = parameter?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(parameter.Extensions) : null;
AllowEmptyValue = parameter?.AllowEmptyValue ?? AllowEmptyValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public override void SerializeAsV31(IOpenApiWriter writer)
private void SerializeInternal(IOpenApiWriter writer,
Action<IOpenApiWriter, IOpenApiReferenceable> action)
{
Utils.CheckArgumentNull(writer);;
Utils.CheckArgumentNull(writer);
action(writer, Target);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit b9d5360

Please sign in to comment.