Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/json node serialization #2039

Merged
merged 25 commits into from
Jan 3, 2025
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9ff9789
Merge pull request #1846 from microsoft/vnext
MaggieKimani1 Sep 30, 2024
b5b11f5
Merge pull request #1860 from microsoft/vnext
irvinesunday Oct 4, 2024
97f8450
Merge pull request #1873 from microsoft/vnext
irvinesunday Oct 14, 2024
8c64b50
Merge pull request #1885 from microsoft/vnext
irvinesunday Oct 22, 2024
3e0a83f
Merge pull request #1898 from microsoft/vnext
irvinesunday Oct 29, 2024
18482f3
Merge pull request #1906 from microsoft/vnext
MaggieKimani1 Nov 6, 2024
d7b19ce
Merge pull request #1908 from microsoft/vnext
MaggieKimani1 Nov 7, 2024
df5d0c7
Merge pull request #1914 from microsoft/vnext
MaggieKimani1 Nov 12, 2024
55352f1
ci: updates main with the latest ci changes
baywet Dec 19, 2024
a3c6c83
fix: removes extraneous download artifact task
baywet Dec 19, 2024
662e9d7
fix: hidi GH release
baywet Dec 19, 2024
1ae06b1
fix: redundant artifact upload
baywet Dec 19, 2024
e521230
Merge pull request #2005 from microsoft/ci/updates-to-main
baywet Dec 19, 2024
166747f
fix: removes actions steps that create extraneous tags
baywet Dec 20, 2024
ff52d83
fix: updates output syntax for tag version
baywet Dec 20, 2024
2fcda79
ci: moves create github release to its own job
baywet Dec 20, 2024
ee99c4d
nit: fixes indent
baywet Dec 20, 2024
2efd4eb
Merge pull request #2017 from microsoft/fix/ci-hidi-gh-upload-to-main
baywet Dec 20, 2024
c3d0c15
Merge pull request #2015 from microsoft/dev
baywet Dec 20, 2024
e7dfa1a
fix: reverts to a regular variable for the tag value
baywet Dec 20, 2024
5a824bb
ci: fix artefacts path
baywet Dec 20, 2024
249ff45
Merge pull request #2021 from microsoft/fix/ci-tag-value-to-main
baywet Dec 20, 2024
cce2495
Merge pull request #2028 from microsoft/dev
baywet Dec 24, 2024
a6a44a7
fix: date time and date time offset shifting zones
baywet Jan 2, 2025
4a50c77
fix: removes useless condition for null check
baywet Jan 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 46 additions & 26 deletions src/Microsoft.OpenApi/Writers/OpenApiWriterAnyExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Any;
Expand Down Expand Up @@ -49,16 +51,15 @@ public static void WriteExtensions(this IOpenApiWriter writer, IDictionary<strin
/// <param name="node">The JsonNode value</param>
public static void WriteAny(this IOpenApiWriter writer, JsonNode node)
{
Utils.CheckArgumentNull(writer);;
Utils.CheckArgumentNull(writer);

if (node == null)
{
writer.WriteNull();
return;
}

var element = JsonDocument.Parse(node.ToJsonString()).RootElement;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼

switch (element.ValueKind)
switch (node.GetValueKind())
{
case JsonValueKind.Array: // Array
writer.WriteArray(node as JsonArray);
Expand All @@ -67,13 +68,13 @@ public static void WriteAny(this IOpenApiWriter writer, JsonNode node)
writer.WriteObject(node as JsonObject);
break;
case JsonValueKind.String: // Primitive
writer.WritePrimitive(element);
writer.WritePrimitive(node);
break;
case JsonValueKind.Number: // Primitive
writer.WritePrimitive(element);
writer.WritePrimitive(node);
break;
case JsonValueKind.True or JsonValueKind.False: // Primitive
writer.WritePrimitive(element);
writer.WritePrimitive(node);
break;
case JsonValueKind.Null: // null
writer.WriteNull();
Expand Down Expand Up @@ -108,52 +109,71 @@ private static void WriteObject(this IOpenApiWriter writer, JsonObject entity)
writer.WriteEndObject();
}

private static void WritePrimitive(this IOpenApiWriter writer, JsonElement primitive)
private static void WritePrimitive(this IOpenApiWriter writer, JsonNode primitive)
{
if (writer == null)
{
Utils.CheckArgumentNull(writer);
}
Utils.CheckArgumentNull(writer);

if (primitive.ValueKind == JsonValueKind.String)
var valueKind = primitive.GetValueKind();

if (valueKind == JsonValueKind.String && primitive is JsonValue jsonStrValue)
{
// check whether string is actual string or date time object
if (primitive.TryGetDateTime(out var dateTime))
if (jsonStrValue.TryGetValue<DateTimeOffset>(out var dto))
{
writer.WriteValue(dateTime);
writer.WriteValue(dto);
Fixed Show fixed Hide fixed
}
else if (primitive.TryGetDateTimeOffset(out var dateTimeOffset))
else if (jsonStrValue.TryGetValue<DateTime>(out var dt))
{
writer.WriteValue(dateTimeOffset);
writer.WriteValue(dt);
Fixed Show fixed Hide fixed
}
else
else if (jsonStrValue.TryGetValue<string>(out var strValue))
{
writer.WriteValue(primitive.GetString());
// check whether string is actual string or date time object
if (DateTimeOffset.TryParse(strValue, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var dateTimeOffset))
{
writer.WriteValue(dateTimeOffset);
Fixed Show fixed Hide fixed
}
else if (DateTime.TryParse(strValue, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var dateTime))
{ // order matters, DTO needs to be checked first!!!
writer.WriteValue(dateTime);
Fixed Show fixed Hide fixed
}
else
{
writer.WriteValue(strValue);
Fixed Show fixed Hide fixed
}
}
}

if (primitive.ValueKind == JsonValueKind.Number)
else if (valueKind == JsonValueKind.Number && primitive is JsonValue jsonValue)
{
if (primitive.TryGetDecimal(out var decimalValue))

if (jsonValue.TryGetValue<decimal>(out var decimalValue))
{
writer.WriteValue(decimalValue);
}
else if (primitive.TryGetDouble(out var doubleValue))
else if (jsonValue.TryGetValue<double>(out var doubleValue))
{
writer.WriteValue(doubleValue);
}
else if (primitive.TryGetInt64(out var longValue))
else if (jsonValue.TryGetValue<float>(out var floatValue))
{
writer.WriteValue(floatValue);
Fixed Show fixed Hide fixed
}
else if (jsonValue.TryGetValue<long>(out var longValue))
{
writer.WriteValue(longValue);
}
else if (primitive.TryGetInt32(out var intValue))
else if (jsonValue.TryGetValue<int>(out var intValue))
{
writer.WriteValue(intValue);
}
}
if (primitive.ValueKind is JsonValueKind.True or JsonValueKind.False)
else if (valueKind is JsonValueKind.False)
{
writer.WriteValue(false);
Fixed Show fixed Hide fixed
}
else if (valueKind is JsonValueKind.True)
{
writer.WriteValue(primitive.GetBoolean());
writer.WriteValue(true);
Fixed Show fixed Hide fixed
}
}
}
Expand Down
Loading