Skip to content

Commit 050b51e

Browse files
authored
Problem with CreateUser (Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: [. Path 'tags', line 1, position 159.) (#153)
Based on return Tags property type from RabbitMQ converter StringOrArrayConverter will convert data to comma delimited string.
1 parent 6960055 commit 050b51e

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

Source/EasyNetQ.Management.Client.IntegrationTests/ManagementClientTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,12 @@ public async Task Should_be_able_to_create_a_queue_with_plus_char_in_the_name()
134134
[Fact]
135135
public async Task Should_be_able_to_create_a_user()
136136
{
137-
var userInfo = new UserInfo(testUser, "topSecret").AddTag("administrator");
137+
var userTag = "administrator";
138+
var userInfo = new UserInfo(testUser, "topSecret").AddTag(userTag);
138139

139140
var user = await managementClient.CreateUserAsync(userInfo).ConfigureAwait(false);
140141
user.Name.Should().Be(testUser);
142+
user.Tags.Should().Contain(userTag);
141143
}
142144

143145
[Fact]
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
using EasyNetQ.Management.Client.Serialization;
2+
using Newtonsoft.Json;
3+
14
namespace EasyNetQ.Management.Client.Model
25
{
36
public class User
47
{
58
public string Name { get; set; }
69
public string PasswordHash { get; set; }
10+
[JsonConverter(typeof(StringOrArrayConverter))]
711
public string Tags { get; set; }
812
}
9-
}
13+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
5+
namespace EasyNetQ.Management.Client.Serialization
6+
{
7+
public class StringOrArrayConverter : JsonConverter
8+
{
9+
public override bool CanConvert(Type objectType)
10+
{
11+
return objectType == typeof(string);
12+
}
13+
14+
public override object ReadJson(
15+
JsonReader reader,
16+
Type objectType,
17+
object existingValue,
18+
JsonSerializer serializer)
19+
{
20+
JToken token = JToken.Load(reader);
21+
if (token.Type == JTokenType.Array)
22+
return string.Join(",", token.ToObject<string[]>());
23+
else
24+
return token.ToString();
25+
}
26+
27+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
28+
{
29+
serializer.Serialize(writer, value);
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)