Skip to content

Commit 46e0339

Browse files
authored
Fixes AddCustomAttribute throws Newtonsoft.Json.JsonWriterException: Unsupported type error when used to add header values (newrelic#378)
* Added a test for Microsoft.Extensions.Primitives.StringValues * Check for StringValues and explicitly tostring it
1 parent e15fb00 commit 46e0339

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/Agent/NewRelic/Agent/Core/JsonConverters/JsonSerializerHelpers.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,19 @@ public static void WriteCollection(JsonWriter writer, IEnumerable<IAttributeValu
2727
}
2828

2929
writer.WritePropertyName(attribVal.AttributeDefinition.Name);
30-
writer.WriteValue(outputValue);
30+
31+
// Causes an exception since this type is unsupported by the JsonConverter
32+
if (outputValue.GetType().ToString() == "Microsoft.Extensions.Primitives.StringValues")
33+
{
34+
writer.WriteValue(outputValue.ToString());
35+
}
36+
else
37+
{
38+
writer.WriteValue(outputValue);
39+
}
3140
}
3241
}
42+
3343
writer.WriteEndObject();
3444
}
3545

tests/Agent/UnitTests/CompositeTests/AgentApiTests.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System;
1818
using System.Collections.Generic;
1919
using System.Linq;
20+
using Microsoft.Extensions.Primitives;
2021

2122
namespace CompositeTests
2223
{
@@ -2256,6 +2257,10 @@ public void SpanCustomAttributes()
22562257
segment.AddCustomAttribute("key8", null);
22572258
segment.AddCustomAttribute("", dtm2);
22582259

2260+
var singleStringValue = new StringValues("avalue");
2261+
var multiStringValue = new StringValues(new[] { "onevalue", "twovalue", "threevalue" });
2262+
segment.AddCustomAttribute("key9a", singleStringValue);
2263+
segment.AddCustomAttribute("key9b", multiStringValue);
22592264

22602265
var expectedAttributes = new[]
22612266
{
@@ -2265,7 +2270,9 @@ public void SpanCustomAttributes()
22652270
new ExpectedAttribute(){ Key = "key4", Value = 4.0d},
22662271
new ExpectedAttribute(){ Key = "key5", Value = true},
22672272
new ExpectedAttribute(){ Key = "key6", Value = dtm1.ToString("o")},
2268-
new ExpectedAttribute(){ Key = "key7", Value = dtm2.ToString("o")}
2273+
new ExpectedAttribute(){ Key = "key7", Value = dtm2.ToString("o")},
2274+
new ExpectedAttribute(){ Key = "key9a", Value = "avalue"},
2275+
new ExpectedAttribute(){ Key = "key9b", Value = "onevalue,twovalue,threevalue"}
22692276
};
22702277

22712278
var unexpectedAttributes = new[]

tests/Agent/UnitTests/CompositeTests/CompositeTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<PackageReference Include="Castle.Core" Version="3.3.0" />
1111
<PackageReference Include="Castle.Windsor" Version="3.3.0" />
1212
<PackageReference Include="JustMock" Version="2020.1.113.1" />
13+
<PackageReference Include="Microsoft.Extensions.Primitives" Version="1.1.1" />
1314
<PackageReference Include="MoreLinq" Version="2.4.0" />
1415
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
1516
<PackageReference Include="NUnit" Version="3.12.0" />

0 commit comments

Comments
 (0)