11using System . Linq ;
22using System . Net ;
33using System . Net . Http ;
4+ using System . Text . Json . Serialization ;
45using System . Threading . Tasks ;
56using FluentAssertions ;
67using JsonApiDotNetCore . Configuration ;
78using JsonApiDotNetCore . Serialization . Objects ;
89using Microsoft . Extensions . DependencyInjection ;
9- using Newtonsoft . Json ;
1010using TestBuildingBlocks ;
1111using Xunit ;
1212
1313namespace 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