Skip to content

Commit b9d5360

Browse files
authored
Merge pull request #1616 from microsoft/is/v2-fixes
Fixes cloning of `JsonNode` objects
2 parents 77cd25a + f884968 commit b9d5360

File tree

8 files changed

+25
-16
lines changed

8 files changed

+25
-16
lines changed

src/Microsoft.OpenApi/Helpers/JsonNodeCloneHelper.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
using System.Text.Json;
5+
using System.Text.Json.Nodes;
56
using System.Text.Json.Serialization;
67
using Json.Schema;
78
using Microsoft.OpenApi.Any;
@@ -17,15 +18,24 @@ internal static class JsonNodeCloneHelper
1718

1819
internal static OpenApiAny Clone(OpenApiAny value)
1920
{
20-
var jsonString = Serialize(value);
21-
var result = JsonSerializer.Deserialize<OpenApiAny>(jsonString, options);
21+
var jsonString = Serialize(value?.Node);
22+
if (string.IsNullOrEmpty(jsonString))
23+
{
24+
return null;
25+
}
2226

23-
return result;
27+
var result = JsonSerializer.Deserialize<JsonNode>(jsonString, options);
28+
return new OpenApiAny(result);
2429
}
2530

2631
internal static JsonSchema CloneJsonSchema(JsonSchema schema)
2732
{
2833
var jsonString = Serialize(schema);
34+
if (string.IsNullOrEmpty(jsonString))
35+
{
36+
return null;
37+
}
38+
2939
var result = JsonSerializer.Deserialize<JsonSchema>(jsonString, options);
3040
return result;
3141
}

src/Microsoft.OpenApi/Models/OpenApiExample.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ public OpenApiExample(OpenApiExample example)
6868
{
6969
Summary = example?.Summary ?? Summary;
7070
Description = example?.Description ?? Description;
71-
Value = example?.Value ?? JsonNodeCloneHelper.Clone(example?.Value);
71+
Value = example?.Value != null ? JsonNodeCloneHelper.Clone(example.Value) : null;
7272
ExternalValue = example?.ExternalValue ?? ExternalValue;
7373
Extensions = example?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(example.Extensions) : null;
74-
Reference = example?.Reference != null ? new(example?.Reference) : null;
74+
Reference = example?.Reference != null ? new(example.Reference) : null;
7575
UnresolvedReference = example?.UnresolvedReference ?? UnresolvedReference;
7676
}
7777

src/Microsoft.OpenApi/Models/OpenApiHeader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ public OpenApiHeader(OpenApiHeader header)
114114
Style = header?.Style ?? Style;
115115
Explode = header?.Explode ?? Explode;
116116
AllowReserved = header?.AllowReserved ?? AllowReserved;
117-
Schema = header?.Schema != null ? JsonNodeCloneHelper.CloneJsonSchema(header?.Schema) : null;
118-
Example = header?.Example != null ? JsonNodeCloneHelper.Clone(header?.Example) : null;
117+
Schema = header?.Schema != null ? JsonNodeCloneHelper.CloneJsonSchema(header.Schema) : null;
118+
Example = header?.Example != null ? JsonNodeCloneHelper.Clone(header.Example) : null;
119119
Examples = header?.Examples != null ? new Dictionary<string, OpenApiExample>(header.Examples) : null;
120120
Content = header?.Content != null ? new Dictionary<string, OpenApiMediaType>(header.Content) : null;
121121
Extensions = header?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(header.Extensions) : null;

src/Microsoft.OpenApi/Models/OpenApiMediaType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public OpenApiMediaType() { }
6262
/// </summary>
6363
public OpenApiMediaType(OpenApiMediaType mediaType)
6464
{
65-
_schema = JsonNodeCloneHelper.CloneJsonSchema(mediaType?.Schema);
66-
Example = JsonNodeCloneHelper.Clone(mediaType?.Example);
65+
Schema = mediaType?.Schema != null ? JsonNodeCloneHelper.CloneJsonSchema(mediaType.Schema) : null;
66+
Example = mediaType?.Example != null ? JsonNodeCloneHelper.Clone(mediaType.Example) : null;
6767
Examples = mediaType?.Examples != null ? new Dictionary<string, OpenApiExample>(mediaType.Examples) : null;
6868
Encoding = mediaType?.Encoding != null ? new Dictionary<string, OpenApiEncoding>(mediaType.Encoding) : null;
6969
Extensions = mediaType?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(mediaType.Extensions) : null;

src/Microsoft.OpenApi/Models/OpenApiParameter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System;
@@ -168,9 +168,9 @@ public OpenApiParameter(OpenApiParameter parameter)
168168
Style = parameter?.Style ?? Style;
169169
Explode = parameter?.Explode ?? Explode;
170170
AllowReserved = parameter?.AllowReserved ?? AllowReserved;
171-
Schema = parameter?.Schema != null ? JsonNodeCloneHelper.CloneJsonSchema(parameter?.Schema) : null;
171+
Schema = parameter?.Schema != null ? JsonNodeCloneHelper.CloneJsonSchema(parameter.Schema) : null;
172172
Examples = parameter?.Examples != null ? new Dictionary<string, OpenApiExample>(parameter.Examples) : null;
173-
Example = parameter?.Example != null ? JsonNodeCloneHelper.Clone(parameter?.Example) : null;
173+
Example = parameter?.Example != null ? JsonNodeCloneHelper.Clone(parameter.Example) : null;
174174
Content = parameter?.Content != null ? new Dictionary<string, OpenApiMediaType>(parameter.Content) : null;
175175
Extensions = parameter?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(parameter.Extensions) : null;
176176
AllowEmptyValue = parameter?.AllowEmptyValue ?? AllowEmptyValue;

src/Microsoft.OpenApi/Models/References/OpenApiExampleReference.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public override void SerializeAsV31(IOpenApiWriter writer)
125125
private void SerializeInternal(IOpenApiWriter writer,
126126
Action<IOpenApiWriter, IOpenApiReferenceable> action)
127127
{
128-
Utils.CheckArgumentNull(writer);;
128+
Utils.CheckArgumentNull(writer);
129129
action(writer, Target);
130130
}
131131
}

src/Microsoft.OpenApi/Reader/Services/OpenApiRemoteReferenceCollector.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
using System;
54
using System.Collections.Generic;
65
using Microsoft.OpenApi.Interfaces;
76
using Microsoft.OpenApi.Models;

test/Microsoft.OpenApi.Tests/Models/References/OpenApiExampleReferenceTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public OpenApiExampleReferenceTests()
133133

134134
[Fact]
135135
public void ExampleReferenceResolutionWorks()
136-
{
136+
{
137137
// Assert
138138
Assert.NotNull(_localExampleReference.Value);
139139
Assert.Equal("[{\"id\":1,\"name\":\"John Doe\"}]", _localExampleReference.Value.Node.ToJsonString());

0 commit comments

Comments
 (0)