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

EmptyArrayAsDefaultConverter: treat empty object {} as default (null) #309

Merged
merged 3 commits into from
Jan 15, 2024
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
@@ -0,0 +1,47 @@
using System.Text.Json;
using EasyNetQ.Management.Client.Model;

namespace EasyNetQ.Management.Client.Tests.Serialization;

public class EmptyArrayAsDefaultConverterTests
{
private const string consumerDetailFormat =
"""{{"arguments":{0},"channel_details":{0},"ack_required":true,"active":true,"activity_status":"up","consumer_tag":"CT","consumer_timeout":"undefined","exclusive":false,"prefetch_count":0,"queue":{{"name":"Q","vhost":"V"}}}}""";

private static readonly ConsumerDetail expectedConsumerDetail = new ConsumerDetail(new QueueName("Q", "V"), "CT", false, true, null, null);

[Fact]
public void Should_deserialize_empty_array()
{
var deserializedObject = JsonSerializer.Deserialize<ConsumerDetail>(string.Format(consumerDetailFormat, "[]"), ManagementClient.SerializerOptions);
deserializedObject.Should().BeEquivalentTo(expectedConsumerDetail);
}

[Fact]
public void Should_deserialize_empty_object()
{
var deserializedObject = JsonSerializer.Deserialize<ConsumerDetail>(string.Format(consumerDetailFormat, "{}"), ManagementClient.SerializerOptions);
deserializedObject.Should().BeEquivalentTo(expectedConsumerDetail);
}

[Fact]
public void Should_deserialize_null()
{
var deserializedObject = JsonSerializer.Deserialize<ConsumerDetail>(string.Format(consumerDetailFormat, "null"), ManagementClient.SerializerOptions);
deserializedObject.Should().BeEquivalentTo(expectedConsumerDetail);
}

[Fact]
public void Should_throw_deserialize_non_empty_array()
{
var action = () => JsonSerializer.Deserialize<ConsumerDetail>(string.Format(consumerDetailFormat, "[1]"), ManagementClient.SerializerOptions);
action.Should().ThrowExactly<JsonException>();
}

[Fact]
public void Should_throw_deserialize_string()
{
var action = () => JsonSerializer.Deserialize<ConsumerDetail>(string.Format(consumerDetailFormat, "\"consumer_detail\""), ManagementClient.SerializerOptions);
action.Should().ThrowExactly<JsonException>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ internal sealed class EmptyArrayAsDefaultConverter<T> : JsonConverter<T>
var jsonElement = JsonElement.ParseValue(ref reader);
switch (jsonElement)
{
case { ValueKind: JsonValueKind.Object }:
case { ValueKind: JsonValueKind.Object } when jsonElement.EnumerateObject().Any():
return jsonElement.Deserialize<T>(options);
case { ValueKind: JsonValueKind.Array } when jsonElement.GetArrayLength() > 0:
throw new JsonException("Empty array is required");
case { ValueKind: JsonValueKind.Array }:
case { ValueKind: JsonValueKind.Object }:
case { ValueKind: JsonValueKind.Array } when jsonElement.GetArrayLength() == 0:
case { ValueKind: JsonValueKind.Null }:
return default;
case { ValueKind: JsonValueKind.Array }:
throw new JsonException($"Unexpected value {jsonElement.GetRawText()}");
default:
throw new JsonException($"Unexpected token type {jsonElement.ValueKind}");
}
Expand Down
Loading