-
Notifications
You must be signed in to change notification settings - Fork 76
feat: support nested filtering in the query parser #366
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
4 commits
Select commit
Hold shift + click to select a range
9c5aa26
feat: support nested filtering
mattpolzin e1fbb47
chore: add requested more deeply nested tests
mattpolzin 54adec7
fix: simplify and correct deep_merge conflict handling
mattpolzin d9ee357
chore: add test that string/string conflicts result in the second str…
mattpolzin 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 |
---|---|---|
|
@@ -64,12 +64,31 @@ defmodule JSONAPI.QueryParserTest do | |
end | ||
end | ||
|
||
test "parse_filter/2 turns filters key/val pairs" do | ||
test "parse_filter/2 returns filters key/val pairs" do | ||
config = struct(Config, opts: [filter: ~w(name)], view: MyView) | ||
filter = parse_filter(config, %{"name" => "jason"}).filter | ||
assert filter[:name] == "jason" | ||
end | ||
|
||
test "parse_filter/2 handles nested filters" do | ||
config = struct(Config, opts: [filter: ~w(author.username)], view: MyView) | ||
filter = parse_filter(config, %{"author.username" => "jason"}).filter | ||
assert filter[:author][:username] == "jason" | ||
end | ||
|
||
test "parse_filter/2 handles nested filters two deep" do | ||
config = struct(Config, opts: [filter: ~w(author.top_posts.text)], view: MyView) | ||
filter = parse_filter(config, %{"author.top_posts.text" => "some post"}).filter | ||
assert filter[:author][:top_posts][:text] == "some post" | ||
end | ||
|
||
test "parse_filter/2 handles nested filters with overlap" do | ||
config = struct(Config, opts: [filter: ~w(author.username author.id)], view: MyView) | ||
filter = parse_filter(config, %{"author.username" => "jason", "author.id" => "123"}).filter | ||
assert filter[:author][:username] == "jason" | ||
assert filter[:author][:id] == "123" | ||
end | ||
mattpolzin marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about whether Config could come with a filter other than |
||
|
||
test "parse_filter/2 raises on invalid filters" do | ||
config = struct(Config, opts: [], view: MyView) | ||
|
||
|
@@ -84,11 +103,24 @@ defmodule JSONAPI.QueryParserTest do | |
assert parse_include(config, "author").include == [:author] | ||
assert parse_include(config, "comments,author").include == [:comments, :author] | ||
assert parse_include(config, "comments.user").include == [comments: :user] | ||
assert parse_include(config, "comments.user.top_posts").include == [comments: [user: :top_posts]] | ||
assert parse_include(config, "best_friends").include == [:best_friends] | ||
assert parse_include(config, "author.top-posts").include == [author: :top_posts] | ||
assert parse_include(config, "").include == [] | ||
end | ||
|
||
test "parse_include/2 succeds given valid nested include specified in allowed list" do | ||
config = struct(Config, view: MyView, opts: [include: ~w(comments.user)]) | ||
|
||
assert parse_include(config, "comments.user").include == [comments: :user] | ||
end | ||
mattpolzin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
test "parse_include/2 succeds given valid twice-nested include specified in allowed list" do | ||
config = struct(Config, view: MyView, opts: [include: ~w(comments.user.top_posts)]) | ||
|
||
assert parse_include(config, "comments.user.top_posts").include == [comments: [user: :top_posts]] | ||
end | ||
|
||
test "parse_include/2 errors with invalid includes" do | ||
config = struct(Config, view: MyView) | ||
|
||
|
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
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.
I wonder if we need to do some defensive programming here.
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.
I think I've addressed concerns over robustness with my latest commit. Please take a look and let me know what you think.
I've protected the first case by ensuring we have more keyword list to go before deep merging.
I've simplified the second case to always take the new value on conflict or when the remaining value is not a list.