diff --git a/src/JsonApiDotNetCore/Serialization/Building/ResourceObjectBuilder.cs b/src/JsonApiDotNetCore/Serialization/Building/ResourceObjectBuilder.cs index 3cb7c20435..8fc37aea38 100644 --- a/src/JsonApiDotNetCore/Serialization/Building/ResourceObjectBuilder.cs +++ b/src/JsonApiDotNetCore/Serialization/Building/ResourceObjectBuilder.cs @@ -1,12 +1,12 @@ using System.Collections.Generic; using System.Linq; +using System.Text.Json.Serialization; using JetBrains.Annotations; using JsonApiDotNetCore.Configuration; using JsonApiDotNetCore.Resources; using JsonApiDotNetCore.Resources.Annotations; using JsonApiDotNetCore.Resources.Internal; using JsonApiDotNetCore.Serialization.Objects; -using Newtonsoft.Json; namespace JsonApiDotNetCore.Serialization.Building { @@ -168,15 +168,15 @@ private void ProcessAttributes(IIdentifiable resource, IEnumerable, QueryStringDbContext>> + public sealed class SerializerIgnoreConditionTests : IClassFixture, QueryStringDbContext>> { private readonly IntegrationTestContext, QueryStringDbContext> _testContext; private readonly QueryStringFakers _fakers = new(); - public SerializerIgnoreValueTests(IntegrationTestContext, QueryStringDbContext> testContext) + public SerializerIgnoreConditionTests(IntegrationTestContext, QueryStringDbContext> testContext) { _testContext = testContext; @@ -25,18 +25,22 @@ public SerializerIgnoreValueTests(IntegrationTestContext(); - options.SerializerSettings.NullValueHandling = configurationValue; + options.SerializerOptions.DefaultIgnoreCondition = configurationValue; Calendar calendar = _fakers.Calendar.Generate(); calendar.TimeZone = null; + calendar.DefaultAppointmentDurationInMinutes = default; calendar.Appointments = _fakers.Appointment.Generate(1).ToHashSet(); calendar.Appointments.Single().Title = null; + calendar.Appointments.Single().EndTime = default; await _testContext.RunOnDatabaseAsync(async dbContext => { @@ -55,7 +59,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext => responseDocument.SingleData.Should().NotBeNull(); responseDocument.Included.Should().HaveCount(1); - if (expectInDocument) + if (expectNullValueInDocument) { responseDocument.SingleData.Attributes.Should().ContainKey("timeZone"); responseDocument.Included[0].Attributes.Should().ContainKey("title"); @@ -65,40 +69,8 @@ await _testContext.RunOnDatabaseAsync(async dbContext => responseDocument.SingleData.Attributes.Should().NotContainKey("timeZone"); responseDocument.Included[0].Attributes.Should().NotContainKey("title"); } - } - - [Theory] - [InlineData(DefaultValueHandling.Ignore, false)] - [InlineData(DefaultValueHandling.Include, true)] - public async Task Applies_configuration_for_defaults(DefaultValueHandling configurationValue, bool expectInDocument) - { - // Arrange - var options = (JsonApiOptions)_testContext.Factory.Services.GetRequiredService(); - options.SerializerSettings.DefaultValueHandling = configurationValue; - - Calendar calendar = _fakers.Calendar.Generate(); - calendar.DefaultAppointmentDurationInMinutes = default; - calendar.Appointments = _fakers.Appointment.Generate(1).ToHashSet(); - calendar.Appointments.Single().EndTime = default; - - await _testContext.RunOnDatabaseAsync(async dbContext => - { - dbContext.Calendars.Add(calendar); - await dbContext.SaveChangesAsync(); - }); - - string route = $"/calendars/{calendar.StringId}?include=appointments"; - - // Act - (HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync(route); - - // Assert - httpResponse.Should().HaveStatusCode(HttpStatusCode.OK); - - responseDocument.SingleData.Should().NotBeNull(); - responseDocument.Included.Should().HaveCount(1); - if (expectInDocument) + if (expectDefaultValueInDocument) { responseDocument.SingleData.Attributes.Should().ContainKey("defaultAppointmentDurationInMinutes"); responseDocument.Included[0].Attributes.Should().ContainKey("endTime");