-
-
Notifications
You must be signed in to change notification settings - Fork 158
Feat/#226: Add DocumentBuilderOptions allowing omission of null attributes #227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace JsonApiDotNetCore.Builders | ||
{ | ||
public struct DocumentBuilderOptions | ||
{ | ||
public DocumentBuilderOptions(bool omitNullValuedAttributes = false) | ||
{ | ||
this.OmitNullValuedAttributes = omitNullValuedAttributes; | ||
} | ||
|
||
public bool OmitNullValuedAttributes { get; private set; } | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/JsonApiDotNetCore/Builders/DocumentBuilderOptionsProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using JsonApiDotNetCore.Services; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace JsonApiDotNetCore.Builders | ||
{ | ||
public class DocumentBuilderOptionsProvider : IDocumentBuilderOptionsProvider | ||
{ | ||
private readonly IJsonApiContext _jsonApiContext; | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
|
||
public DocumentBuilderOptionsProvider(IJsonApiContext jsonApiContext, IHttpContextAccessor httpContextAccessor) | ||
{ | ||
_jsonApiContext = jsonApiContext; | ||
_httpContextAccessor = httpContextAccessor; | ||
} | ||
|
||
public DocumentBuilderOptions GetDocumentBuilderOptions() | ||
{ | ||
var nullAttributeResponseBehaviorConfig = this._jsonApiContext.Options.NullAttributeResponseBehavior; | ||
if (nullAttributeResponseBehaviorConfig.AllowClientOverride && _httpContextAccessor.HttpContext.Request.Query.TryGetValue("omitNullValuedAttributes", out var omitNullValuedAttributesQs)) | ||
{ | ||
if (bool.TryParse(omitNullValuedAttributesQs, out var omitNullValuedAttributes)) | ||
{ | ||
return new DocumentBuilderOptions(omitNullValuedAttributes); | ||
} | ||
} | ||
return new DocumentBuilderOptions(this._jsonApiContext.Options.NullAttributeResponseBehavior.OmitNullValuedAttributes); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/JsonApiDotNetCore/Builders/IDocumentBuilderOptionsProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace JsonApiDotNetCore.Builders | ||
{ | ||
public interface IDocumentBuilderOptionsProvider | ||
{ | ||
DocumentBuilderOptions GetDocumentBuilderOptions(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/JsonApiDotNetCore/Configuration/NullAttributeResponseBehavior.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace JsonApiDotNetCore.Configuration | ||
{ | ||
public struct NullAttributeResponseBehavior | ||
{ | ||
public NullAttributeResponseBehavior(bool omitNullValuedAttributes = false, bool allowClientOverride = false) | ||
{ | ||
OmitNullValuedAttributes = omitNullValuedAttributes; | ||
AllowClientOverride = allowClientOverride; | ||
} | ||
|
||
public bool OmitNullValuedAttributes { get; } | ||
public bool AllowClientOverride { get; } | ||
// ... | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
...sonApiDotNetCoreExampleTests/Acceptance/Extensibility/NullValuedAttributeHandlingTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using JsonApiDotNetCore.Configuration; | ||
using JsonApiDotNetCore.Models; | ||
using JsonApiDotNetCoreExample; | ||
using JsonApiDotNetCoreExample.Data; | ||
using JsonApiDotNetCoreExample.Models; | ||
using Newtonsoft.Json; | ||
using Xunit; | ||
|
||
namespace JsonApiDotNetCoreExampleTests.Acceptance.Extensibility | ||
{ | ||
[Collection("WebHostCollection")] | ||
public class NullValuedAttributeHandlingTests : IAsyncLifetime | ||
{ | ||
private readonly TestFixture<Startup> _fixture; | ||
private readonly AppDbContext _dbContext; | ||
private readonly TodoItem _todoItem; | ||
|
||
public NullValuedAttributeHandlingTests(TestFixture<Startup> fixture) | ||
{ | ||
_fixture = fixture; | ||
_dbContext = fixture.GetService<AppDbContext>(); | ||
_todoItem = new TodoItem | ||
{ | ||
Description = null, | ||
Ordinal = 1, | ||
CreatedDate = DateTime.Now, | ||
AchievedDate = DateTime.Now.AddDays(2) | ||
}; | ||
_todoItem = _dbContext.TodoItems.Add(_todoItem).Entity; | ||
} | ||
|
||
public async Task InitializeAsync() | ||
{ | ||
await _dbContext.SaveChangesAsync(); | ||
} | ||
|
||
public Task DisposeAsync() | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
[Theory] | ||
[InlineData(null, null, null, false)] | ||
[InlineData(true, null, null, true)] | ||
[InlineData(false, true, "true", true)] | ||
[InlineData(false, false, "true", false)] | ||
[InlineData(true, true, "false", false)] | ||
[InlineData(true, false, "false", true)] | ||
[InlineData(null, false, "false", false)] | ||
[InlineData(null, false, "true", false)] | ||
[InlineData(null, true, "true", true)] | ||
[InlineData(null, true, "false", false)] | ||
[InlineData(null, true, "foo", false)] | ||
[InlineData(null, false, "foo", false)] | ||
[InlineData(true, true, "foo", true)] | ||
[InlineData(true, false, "foo", true)] | ||
[InlineData(null, true, null, false)] | ||
[InlineData(null, false, null, false)] | ||
public async Task CheckNullBehaviorCombination(bool? omitNullValuedAttributes, bool? allowClientOverride, | ||
string clientOverride, bool omitsNulls) | ||
{ | ||
|
||
// Override some null handling options | ||
NullAttributeResponseBehavior nullAttributeResponseBehavior; | ||
if (omitNullValuedAttributes.HasValue && allowClientOverride.HasValue) | ||
{ | ||
nullAttributeResponseBehavior = new NullAttributeResponseBehavior(omitNullValuedAttributes.Value, allowClientOverride.Value); | ||
} | ||
else if (omitNullValuedAttributes.HasValue) | ||
{ | ||
nullAttributeResponseBehavior = new NullAttributeResponseBehavior(omitNullValuedAttributes.Value); | ||
} | ||
else if (allowClientOverride.HasValue) | ||
{ | ||
nullAttributeResponseBehavior = new NullAttributeResponseBehavior(allowClientOverride: allowClientOverride.Value); | ||
} | ||
else | ||
{ | ||
nullAttributeResponseBehavior = new NullAttributeResponseBehavior(); | ||
} | ||
var jsonApiOptions = _fixture.GetService<JsonApiOptions>(); | ||
jsonApiOptions.NullAttributeResponseBehavior = nullAttributeResponseBehavior; | ||
jsonApiOptions.AllowCustomQueryParameters = true; | ||
|
||
var httpMethod = new HttpMethod("GET"); | ||
var queryString = allowClientOverride.HasValue | ||
? $"?omitNullValuedAttributes={clientOverride}" | ||
: ""; | ||
var route = $"/api/v1/todo-items/{_todoItem.Id}{queryString}"; | ||
var request = new HttpRequestMessage(httpMethod, route); | ||
|
||
// act | ||
var response = await _fixture.Client.SendAsync(request); | ||
var body = await response.Content.ReadAsStringAsync(); | ||
var deserializeBody = JsonConvert.DeserializeObject<Document>(body); | ||
|
||
// assert. does response contain a null valued attribute | ||
Assert.Equal(omitsNulls, !deserializeBody.Data.Attributes.ContainsKey("description")); | ||
|
||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
☝️