|
| 1 | +using System; |
| 2 | +using System.Net.Http; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using JsonApiDotNetCore.Configuration; |
| 5 | +using JsonApiDotNetCore.Models; |
| 6 | +using JsonApiDotNetCoreExample; |
| 7 | +using JsonApiDotNetCoreExample.Data; |
| 8 | +using JsonApiDotNetCoreExample.Models; |
| 9 | +using Newtonsoft.Json; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace JsonApiDotNetCoreExampleTests.Acceptance.Extensibility |
| 13 | +{ |
| 14 | + [Collection("WebHostCollection")] |
| 15 | + public class NullValuedAttributeHandlingTests : IAsyncLifetime |
| 16 | + { |
| 17 | + private readonly TestFixture<Startup> _fixture; |
| 18 | + private readonly AppDbContext _dbContext; |
| 19 | + private readonly TodoItem _todoItem; |
| 20 | + |
| 21 | + public NullValuedAttributeHandlingTests(TestFixture<Startup> fixture) |
| 22 | + { |
| 23 | + _fixture = fixture; |
| 24 | + _dbContext = fixture.GetService<AppDbContext>(); |
| 25 | + _todoItem = new TodoItem |
| 26 | + { |
| 27 | + Description = null, |
| 28 | + Ordinal = 1, |
| 29 | + CreatedDate = DateTime.Now, |
| 30 | + AchievedDate = DateTime.Now.AddDays(2) |
| 31 | + }; |
| 32 | + _todoItem = _dbContext.TodoItems.Add(_todoItem).Entity; |
| 33 | + } |
| 34 | + |
| 35 | + public async Task InitializeAsync() |
| 36 | + { |
| 37 | + await _dbContext.SaveChangesAsync(); |
| 38 | + } |
| 39 | + |
| 40 | + public Task DisposeAsync() |
| 41 | + { |
| 42 | + return Task.CompletedTask; |
| 43 | + } |
| 44 | + |
| 45 | + [Theory] |
| 46 | + [InlineData(null, null, null, false)] |
| 47 | + [InlineData(true, null, null, true)] |
| 48 | + [InlineData(false, true, "true", true)] |
| 49 | + [InlineData(false, false, "true", false)] |
| 50 | + [InlineData(true, true, "false", false)] |
| 51 | + [InlineData(true, false, "false", true)] |
| 52 | + [InlineData(null, false, "false", false)] |
| 53 | + [InlineData(null, false, "true", false)] |
| 54 | + [InlineData(null, true, "true", true)] |
| 55 | + [InlineData(null, true, "false", false)] |
| 56 | + [InlineData(null, true, "foo", false)] |
| 57 | + [InlineData(null, false, "foo", false)] |
| 58 | + [InlineData(true, true, "foo", true)] |
| 59 | + [InlineData(true, false, "foo", true)] |
| 60 | + [InlineData(null, true, null, false)] |
| 61 | + [InlineData(null, false, null, false)] |
| 62 | + public async Task CheckNullBehaviorCombination(bool? omitNullValuedAttributes, bool? allowClientOverride, |
| 63 | + string clientOverride, bool omitsNulls) |
| 64 | + { |
| 65 | + |
| 66 | + // Override some null handling options |
| 67 | + NullAttributeResponseBehavior nullAttributeResponseBehavior; |
| 68 | + if (omitNullValuedAttributes.HasValue && allowClientOverride.HasValue) |
| 69 | + { |
| 70 | + nullAttributeResponseBehavior = new NullAttributeResponseBehavior(omitNullValuedAttributes.Value, allowClientOverride.Value); |
| 71 | + } |
| 72 | + else if (omitNullValuedAttributes.HasValue) |
| 73 | + { |
| 74 | + nullAttributeResponseBehavior = new NullAttributeResponseBehavior(omitNullValuedAttributes.Value); |
| 75 | + } |
| 76 | + else if (allowClientOverride.HasValue) |
| 77 | + { |
| 78 | + nullAttributeResponseBehavior = new NullAttributeResponseBehavior(allowClientOverride: allowClientOverride.Value); |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + nullAttributeResponseBehavior = new NullAttributeResponseBehavior(); |
| 83 | + } |
| 84 | + var jsonApiOptions = _fixture.GetService<JsonApiOptions>(); |
| 85 | + jsonApiOptions.NullAttributeResponseBehavior = nullAttributeResponseBehavior; |
| 86 | + jsonApiOptions.AllowCustomQueryParameters = true; |
| 87 | + |
| 88 | + var httpMethod = new HttpMethod("GET"); |
| 89 | + var queryString = allowClientOverride.HasValue |
| 90 | + ? $"?omitNullValuedAttributes={clientOverride}" |
| 91 | + : ""; |
| 92 | + var route = $"/api/v1/todo-items/{_todoItem.Id}{queryString}"; |
| 93 | + var request = new HttpRequestMessage(httpMethod, route); |
| 94 | + |
| 95 | + // act |
| 96 | + var response = await _fixture.Client.SendAsync(request); |
| 97 | + var body = await response.Content.ReadAsStringAsync(); |
| 98 | + var deserializeBody = JsonConvert.DeserializeObject<Document>(body); |
| 99 | + |
| 100 | + // assert. does response contain a null valued attribute |
| 101 | + Assert.Equal(omitsNulls, !deserializeBody.Data.Attributes.ContainsKey("description")); |
| 102 | + |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments