Skip to content

Commit

Permalink
Use configured STJ options for null/default value inclusion
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart Koelman committed Sep 10, 2021
1 parent 705bcd8 commit 85e2abe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -168,15 +168,15 @@ private void ProcessAttributes(IIdentifiable resource, IEnumerable<AttrAttribute
{
object value = attr.GetValue(resource);

if (_options.SerializerSettings.NullValueHandling == NullValueHandling.Ignore && value == null)
if (_options.SerializerOptions.DefaultIgnoreCondition == JsonIgnoreCondition.WhenWritingNull && value == null)
{
return;
continue;
}

if (_options.SerializerSettings.DefaultValueHandling == DefaultValueHandling.Ignore &&
if (_options.SerializerOptions.DefaultIgnoreCondition == JsonIgnoreCondition.WhenWritingDefault &&
Equals(value, RuntimeTypeConverter.GetDefaultValue(attr.Property.PropertyType)))
{
return;
continue;
}

ro.Attributes.Add(attr.PublicName, value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,46 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using FluentAssertions;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Serialization.Objects;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using TestBuildingBlocks;
using Xunit;

namespace JsonApiDotNetCoreTests.IntegrationTests.QueryStrings
{
public sealed class SerializerIgnoreValueTests : IClassFixture<IntegrationTestContext<TestableStartup<QueryStringDbContext>, QueryStringDbContext>>
public sealed class SerializerIgnoreConditionTests : IClassFixture<IntegrationTestContext<TestableStartup<QueryStringDbContext>, QueryStringDbContext>>
{
private readonly IntegrationTestContext<TestableStartup<QueryStringDbContext>, QueryStringDbContext> _testContext;
private readonly QueryStringFakers _fakers = new();

public SerializerIgnoreValueTests(IntegrationTestContext<TestableStartup<QueryStringDbContext>, QueryStringDbContext> testContext)
public SerializerIgnoreConditionTests(IntegrationTestContext<TestableStartup<QueryStringDbContext>, QueryStringDbContext> testContext)
{
_testContext = testContext;

testContext.UseController<CalendarsController>();
}

[Theory]
[InlineData(NullValueHandling.Ignore, false)]
[InlineData(NullValueHandling.Include, true)]
public async Task Applies_configuration_for_nulls(NullValueHandling configurationValue, bool expectInDocument)
[InlineData(JsonIgnoreCondition.Never, true, true)]
[InlineData(JsonIgnoreCondition.WhenWritingDefault, false, false)]
[InlineData(JsonIgnoreCondition.WhenWritingNull, false, true)]
public async Task Applies_configuration_for_ignore_condition(JsonIgnoreCondition configurationValue, bool expectNullValueInDocument,
bool expectDefaultValueInDocument)
{
// Arrange
var options = (JsonApiOptions)_testContext.Factory.Services.GetRequiredService<IJsonApiOptions>();
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 =>
{
Expand All @@ -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");
Expand All @@ -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<IJsonApiOptions>();
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<Document>(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");
Expand Down

0 comments on commit 85e2abe

Please sign in to comment.