Skip to content

Commit 2f72e89

Browse files
committed
Fixing failing tests
1 parent a9d5fc7 commit 2f72e89

File tree

7 files changed

+21
-16
lines changed

7 files changed

+21
-16
lines changed

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<PackageVersion Include="MySql.Data" Version="8.0.32.1" />
4545
<PackageVersion Include="MySql.Data.EntityFrameworkCore" Version="8.0.22" />
4646
<PackageVersion Include="NEST" Version="7.17.5" />
47-
<PackageVersion Include="NJsonSchema" Version="10.2.2" />
47+
<PackageVersion Include="NJsonSchema" Version="11.1.0" />
4848
<PackageVersion Include="NLog" Version="4.6.8" />
4949
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
5050
<PackageVersion Include="Nullean.VsTest.Pretty.TestLogger" Version="0.4.0" PrivateAssets="All" />

src/Elastic.Apm.Specification/Elastic.Apm.Specification.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<ItemGroup>
99
<PackageReference Include="NJsonSchema"/>
1010
<PackageReference Include="SharpZipLib"/>
11-
<PackageReference Include="Newtonsoft.Json"/>
1211
</ItemGroup>
1312

1413
<ItemGroup>

src/Elastic.Apm/Api/Service.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class Service
1717
{
1818
private Service() { }
1919

20+
[JsonConstructor]
2021
internal Service(string name, string version) => (Name, Version) = (name, version);
2122

2223
public AgentC Agent { get; set; }

src/Elastic.Apm/Report/Serialization/PayloadItemSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ internal PayloadItemSerializer() =>
3838
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
3939
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
4040
WriteIndented = false,
41-
Converters = { new JsonConverterDouble(), new JsonConverterDecimal() },
41+
Converters = { new JsonConverterDouble(), new JsonConverterDecimal(), new JsonStringEnumConverter(JsonNamingPolicy.SnakeCaseLower) },
4242
TypeInfoResolver = JsonTypeInfoResolver.Combine(SourceGenerationContext.Default, new DefaultJsonTypeInfoResolver
4343
{
4444
Modifiers = { j =>

test/Elastic.Apm.Tests.MockApmServer/ContextDto.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
using System.Text.Json.Serialization;
66
using Elastic.Apm.Api;
77
using Elastic.Apm.Helpers;
8-
98
using Elastic.Apm.Model;
9+
using Elastic.Apm.Report.Serialization;
1010

1111
// ReSharper disable UnusedAutoPropertyAccessor.Global
1212
// ReSharper disable MemberCanBePrivate.Global
@@ -16,6 +16,7 @@ namespace Elastic.Apm.Tests.MockApmServer
1616
internal class ContextDto : IDto
1717
{
1818
[JsonPropertyName("tags")]
19+
[JsonConverter(typeof(LabelsJsonConverter))]
1920
public LabelsDictionary Labels { get; set; }
2021

2122
public Request Request { get; set; }

test/Elastic.Apm.Tests.MockApmServer/Controllers/IntakeV2EventsController.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
using Elastic.Apm.Specification;
1919
using Elastic.Apm.Tests.Utilities;
2020
using FluentAssertions;
21+
using Microsoft.AspNetCore.Authentication;
2122
using Microsoft.AspNetCore.Mvc;
23+
using Namotion.Reflection;
2224
using NJsonSchema;
2325
using Xunit.Sdk;
2426
using JsonSerializerOptions = System.Text.Json.JsonSerializerOptions;
@@ -31,8 +33,13 @@ namespace Elastic.Apm.Tests.MockApmServer.Controllers
3133
[ApiController]
3234
public class IntakeV2EventsController : ControllerBase
3335
{
34-
private static readonly ConcurrentDictionary<Type, Lazy<Task<JsonSchema>>> Schemata =
35-
new ConcurrentDictionary<Type, Lazy<Task<JsonSchema>>>();
36+
private static readonly ConcurrentDictionary<Type, Lazy<Task<JsonSchema>>> Schemata = new();
37+
38+
private static readonly JsonSerializerOptions JsonSerializerOptions = new()
39+
{
40+
UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow,
41+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
42+
};
3643

3744
private const string ExpectedContentType = "application/x-ndjson; charset=utf-8";
3845
private const string ThisClassName = nameof(IntakeV2EventsController);
@@ -100,12 +107,8 @@ private async Task<int> ParsePayload()
100107
private async Task ParsePayloadLineAndAddToReceivedData(string line)
101108
{
102109
var foundDto = false;
103-
var payload = JsonSerializer.Deserialize<PayloadLineDto>(
104-
line,
105-
new JsonSerializerOptions
106-
{
107-
UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow,
108-
}) ?? throw new ArgumentException("Deserialization failed");
110+
var payload = JsonSerializer
111+
.Deserialize<PayloadLineDto>(line, JsonSerializerOptions) ?? throw new ArgumentException("Deserialization failed");
109112

110113
await HandleParsed(nameof(payload.Error), payload.Error, _mockApmServer.ReceivedData.Errors, _mockApmServer.AddError);
111114
await HandleParsed(nameof(payload.Metadata), payload.Metadata, _mockApmServer.ReceivedData.Metadata, _mockApmServer.AddMetadata);
@@ -136,11 +139,12 @@ async Task HandleParsed<TDto>(string dtoType, TDto dto, ImmutableList<TDto> accu
136139
}, _validator)
137140
.Value;
138141

139-
var jObject = JsonNode.Parse(line) as JsonObject;
140-
var value = jObject!.TryGetPropertyValue(dtoType, out var dtoValue) ? dtoValue.GetValue<string>() : null;
142+
using var jsonDocument = JsonDocument.Parse(line);
141143

142-
var validationErrors = schema.Validate(value);
144+
var root = jsonDocument.RootElement;
145+
var property = root.GetProperty(dtoType.ToLower());
143146

147+
var validationErrors = schema.Validate(property.ToString());
144148
validationErrors.Should().BeEmpty();
145149
}
146150
catch (Exception ex)

test/Elastic.Apm.Tests/SerializationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ public void MetricSet_Serializes_And_Deserializes()
411411
{
412412
var samples = new List<MetricSample>
413413
{
414-
new MetricSample("sample_1", 1), new MetricSample("sample*\"2", 2.1), new MetricSample("sample_1", 3)
414+
new("sample_1", 1), new("sample*\"2", 2.1), new("sample_1", 3)
415415
};
416416

417417
var metricSet = new Elastic.Apm.Metrics.MetricSet(1603343944891, samples);

0 commit comments

Comments
 (0)