Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<PackageReference Include="coverlet.msbuild" Version="6.0.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" PrivateAssets="all" />
<PackageReference Include="coverlet.collector" Version="6.0.2" PrivateAssets="all" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
<PackageReference Include="coverlet.msbuild" Version="6.0.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="FluentAssertions" Version="6.12.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SharpYaml" Version="2.1.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" PrivateAssets="all" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
<PackageReference Include="FluentAssertions" Version="6.12.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SharpYaml" Version="2.1.1" />
<PackageReference Include="Verify.Xunit" Version="28.2.1" />
<PackageReference Include="xunit" Version="2.9.2" />
Expand Down
72 changes: 61 additions & 11 deletions test/Microsoft.OpenApi.Tests/Writers/OpenApiJsonWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using FluentAssertions;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Writers;
using Newtonsoft.Json;
using Xunit;

namespace Microsoft.OpenApi.Tests.Writers
Expand Down Expand Up @@ -62,9 +64,9 @@ public void WriteStringListAsJsonShouldMatchExpected(string[] stringValues, bool
writer.WriteEndArray();
writer.Flush();

var parsedObject = JsonConvert.DeserializeObject(outputString.GetStringBuilder().ToString());
var parsedObject = JsonSerializer.Deserialize<List<string>>(outputString.GetStringBuilder().ToString());
var expectedObject =
JsonConvert.DeserializeObject(JsonConvert.SerializeObject(new List<string>(stringValues)));
JsonSerializer.Deserialize<List<string>>(JsonSerializer.Serialize(new List<string>(stringValues)));

// Assert
parsedObject.Should().BeEquivalentTo(expectedObject);
Expand Down Expand Up @@ -222,17 +224,17 @@ private void WriteValueRecursive(OpenApiJsonWriter writer, object value)
public void WriteMapAsJsonShouldMatchExpected(IDictionary<string, object> inputMap, bool produceTerseOutput)
{
// Arrange
var outputString = new StringWriter(CultureInfo.InvariantCulture);
using var outputString = new StringWriter(CultureInfo.InvariantCulture);
var writer = new OpenApiJsonWriter(outputString, new() { Terse = produceTerseOutput });

// Act
WriteValueRecursive(writer, inputMap);

var parsedObject = JsonConvert.DeserializeObject(outputString.GetStringBuilder().ToString());
var expectedObject = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(inputMap));
using var parsedObject = JsonDocument.Parse(outputString.GetStringBuilder().ToString());
using var expectedObject = JsonDocument.Parse(JsonSerializer.Serialize(inputMap, _jsonSerializerOptions.Value));

// Assert
parsedObject.Should().BeEquivalentTo(expectedObject);
Assert.True(JsonElement.DeepEquals(parsedObject.RootElement, expectedObject.RootElement));
}

public static IEnumerable<object[]> WriteDateTimeAsJsonTestCases()
Expand All @@ -248,6 +250,57 @@ from shouldBeTerse in shouldProduceTerseOutputValues
select new object[] { input, shouldBeTerse };
}

public class CustomDateTimeOffsetConverter : JsonConverter<DateTimeOffset>
{
public CustomDateTimeOffsetConverter(string format)
{
ArgumentException.ThrowIfNullOrEmpty(format);
Format = format;
}

public string Format { get; }

public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DateTime.ParseExact(reader.GetString(), Format, CultureInfo.InvariantCulture);
}

public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString(Format));
}
}
public class CustomDateTimeConverter : JsonConverter<DateTime>
{
public CustomDateTimeConverter(string format)
{
ArgumentException.ThrowIfNullOrEmpty(format);
Format = format;
}

public string Format { get; }

public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DateTime.ParseExact(reader.GetString(), Format, CultureInfo.InvariantCulture);
}

public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString(Format));
}
}
private static readonly Lazy<JsonSerializerOptions> _jsonSerializerOptions = new(() =>
{
var options = new JsonSerializerOptions
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
options.Converters.Add(new CustomDateTimeOffsetConverter("yyyy-MM-ddTHH:mm:ss.fffffffK"));
options.Converters.Add(new CustomDateTimeConverter("yyyy-MM-ddTHH:mm:ss.fffffffK"));
return options;
});

[Theory]
[MemberData(nameof(WriteDateTimeAsJsonTestCases))]
public void WriteDateTimeAsJsonShouldMatchExpected(DateTimeOffset dateTimeOffset, bool produceTerseOutput)
Expand All @@ -260,10 +313,7 @@ public void WriteDateTimeAsJsonShouldMatchExpected(DateTimeOffset dateTimeOffset
writer.WriteValue(dateTimeOffset);

var writtenString = outputString.GetStringBuilder().ToString();
var expectedString = JsonConvert.SerializeObject(dateTimeOffset, new JsonSerializerSettings
{
DateFormatString = "yyyy-MM-ddTHH:mm:ss.fffffffK",
});
var expectedString = JsonSerializer.Serialize(dateTimeOffset, _jsonSerializerOptions.Value);

// Assert
writtenString.Should().Be(expectedString);
Expand Down
Loading