Skip to content

Commit bca9e8b

Browse files
author
Bart Koelman
committed
Use configured STJ options for null/default value inclusion
1 parent f16b0a6 commit bca9e8b

File tree

2 files changed

+18
-46
lines changed

2 files changed

+18
-46
lines changed

src/JsonApiDotNetCore/Serialization/Building/ResourceObjectBuilder.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using System.Text.Json.Serialization;
34
using JetBrains.Annotations;
45
using JsonApiDotNetCore.Configuration;
56
using JsonApiDotNetCore.Resources;
67
using JsonApiDotNetCore.Resources.Annotations;
78
using JsonApiDotNetCore.Resources.Internal;
89
using JsonApiDotNetCore.Serialization.Objects;
9-
using Newtonsoft.Json;
1010

1111
namespace JsonApiDotNetCore.Serialization.Building
1212
{
@@ -168,15 +168,15 @@ private void ProcessAttributes(IIdentifiable resource, IEnumerable<AttrAttribute
168168
{
169169
object value = attr.GetValue(resource);
170170

171-
if (_options.SerializerSettings.NullValueHandling == NullValueHandling.Ignore && value == null)
171+
if (_options.SerializerOptions.DefaultIgnoreCondition == JsonIgnoreCondition.WhenWritingNull && value == null)
172172
{
173-
return;
173+
continue;
174174
}
175175

176-
if (_options.SerializerSettings.DefaultValueHandling == DefaultValueHandling.Ignore &&
176+
if (_options.SerializerOptions.DefaultIgnoreCondition == JsonIgnoreCondition.WhenWritingDefault &&
177177
Equals(value, RuntimeTypeConverter.GetDefaultValue(attr.Property.PropertyType)))
178178
{
179-
return;
179+
continue;
180180
}
181181

182182
ro.Attributes.Add(attr.PublicName, value);

test/JsonApiDotNetCoreTests/IntegrationTests/QueryStrings/SerializerIgnoreValueTests.cs renamed to test/JsonApiDotNetCoreTests/IntegrationTests/QueryStrings/SerializerIgnoreConditionTests.cs

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,46 @@
11
using System.Linq;
22
using System.Net;
33
using System.Net.Http;
4+
using System.Text.Json.Serialization;
45
using System.Threading.Tasks;
56
using FluentAssertions;
67
using JsonApiDotNetCore.Configuration;
78
using JsonApiDotNetCore.Serialization.Objects;
89
using Microsoft.Extensions.DependencyInjection;
9-
using Newtonsoft.Json;
1010
using TestBuildingBlocks;
1111
using Xunit;
1212

1313
namespace JsonApiDotNetCoreTests.IntegrationTests.QueryStrings
1414
{
15-
public sealed class SerializerIgnoreValueTests : IClassFixture<IntegrationTestContext<TestableStartup<QueryStringDbContext>, QueryStringDbContext>>
15+
public sealed class SerializerIgnoreConditionTests : IClassFixture<IntegrationTestContext<TestableStartup<QueryStringDbContext>, QueryStringDbContext>>
1616
{
1717
private readonly IntegrationTestContext<TestableStartup<QueryStringDbContext>, QueryStringDbContext> _testContext;
1818
private readonly QueryStringFakers _fakers = new();
1919

20-
public SerializerIgnoreValueTests(IntegrationTestContext<TestableStartup<QueryStringDbContext>, QueryStringDbContext> testContext)
20+
public SerializerIgnoreConditionTests(IntegrationTestContext<TestableStartup<QueryStringDbContext>, QueryStringDbContext> testContext)
2121
{
2222
_testContext = testContext;
2323

2424
testContext.UseController<CalendarsController>();
2525
}
2626

2727
[Theory]
28-
[InlineData(NullValueHandling.Ignore, false)]
29-
[InlineData(NullValueHandling.Include, true)]
30-
public async Task Applies_configuration_for_nulls(NullValueHandling configurationValue, bool expectInDocument)
28+
[InlineData(JsonIgnoreCondition.Never, true, true)]
29+
[InlineData(JsonIgnoreCondition.WhenWritingDefault, false, false)]
30+
[InlineData(JsonIgnoreCondition.WhenWritingNull, false, true)]
31+
public async Task Applies_configuration_for_ignore_condition(JsonIgnoreCondition configurationValue, bool expectNullValueInDocument,
32+
bool expectDefaultValueInDocument)
3133
{
3234
// Arrange
3335
var options = (JsonApiOptions)_testContext.Factory.Services.GetRequiredService<IJsonApiOptions>();
34-
options.SerializerSettings.NullValueHandling = configurationValue;
36+
options.SerializerOptions.DefaultIgnoreCondition = configurationValue;
3537

3638
Calendar calendar = _fakers.Calendar.Generate();
3739
calendar.TimeZone = null;
40+
calendar.DefaultAppointmentDurationInMinutes = default;
3841
calendar.Appointments = _fakers.Appointment.Generate(1).ToHashSet();
3942
calendar.Appointments.Single().Title = null;
43+
calendar.Appointments.Single().EndTime = default;
4044

4145
await _testContext.RunOnDatabaseAsync(async dbContext =>
4246
{
@@ -55,7 +59,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
5559
responseDocument.SingleData.Should().NotBeNull();
5660
responseDocument.Included.Should().HaveCount(1);
5761

58-
if (expectInDocument)
62+
if (expectNullValueInDocument)
5963
{
6064
responseDocument.SingleData.Attributes.Should().ContainKey("timeZone");
6165
responseDocument.Included[0].Attributes.Should().ContainKey("title");
@@ -65,40 +69,8 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
6569
responseDocument.SingleData.Attributes.Should().NotContainKey("timeZone");
6670
responseDocument.Included[0].Attributes.Should().NotContainKey("title");
6771
}
68-
}
69-
70-
[Theory]
71-
[InlineData(DefaultValueHandling.Ignore, false)]
72-
[InlineData(DefaultValueHandling.Include, true)]
73-
public async Task Applies_configuration_for_defaults(DefaultValueHandling configurationValue, bool expectInDocument)
74-
{
75-
// Arrange
76-
var options = (JsonApiOptions)_testContext.Factory.Services.GetRequiredService<IJsonApiOptions>();
77-
options.SerializerSettings.DefaultValueHandling = configurationValue;
78-
79-
Calendar calendar = _fakers.Calendar.Generate();
80-
calendar.DefaultAppointmentDurationInMinutes = default;
81-
calendar.Appointments = _fakers.Appointment.Generate(1).ToHashSet();
82-
calendar.Appointments.Single().EndTime = default;
83-
84-
await _testContext.RunOnDatabaseAsync(async dbContext =>
85-
{
86-
dbContext.Calendars.Add(calendar);
87-
await dbContext.SaveChangesAsync();
88-
});
89-
90-
string route = $"/calendars/{calendar.StringId}?include=appointments";
91-
92-
// Act
93-
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);
94-
95-
// Assert
96-
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);
97-
98-
responseDocument.SingleData.Should().NotBeNull();
99-
responseDocument.Included.Should().HaveCount(1);
10072

101-
if (expectInDocument)
73+
if (expectDefaultValueInDocument)
10274
{
10375
responseDocument.SingleData.Attributes.Should().ContainKey("defaultAppointmentDurationInMinutes");
10476
responseDocument.Included[0].Attributes.Should().ContainKey("endTime");

0 commit comments

Comments
 (0)