-
Notifications
You must be signed in to change notification settings - Fork 1.4k
WIP - Deserializer implementation #950
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
Closed
joaomdmoura
wants to merge
57
commits into
rails-api:master
from
joaomdmoura:deserializer-implementation
Closed
Changes from all commits
Commits
Show all changes
57 commits
Select commit
Hold shift + click to select a range
2a3c0a5
starting deserializer architecture
joaomdmoura 71b8419
requiring deserializer
joaomdmoura 4b490e4
removing useless autoload for now
joaomdmoura f082e82
Adding initial implementation, including wrap_parameters
joaomdmoura fb08977
starting to update readme
joaomdmoura e700a29
starting deserializer parsing based on serializer defined attributes
joaomdmoura c081b60
updating README to support the new implementation
joaomdmoura 64dd2ed
updating deserialization implementation to not use module but a new s…
joaomdmoura ed856d3
updating generator and root_name parsing
joaomdmoura 2bd9687
updating tests to work with the new Serialisation convention
joaomdmoura dd18f7f
partially updating README
joaomdmoura 1a124c2
adding attribute as default params value
joaomdmoura 387f2a5
implementing strong parameters logic to work with adapters
joaomdmoura a2e91f2
updating code formatting and conventions
joaomdmoura 8a63c85
updating permit and deserialize method
joaomdmoura ce1534d
starting adapter update to support deserialization
joaomdmoura 622a1ce
updatign some tests after rebasing with master
joaomdmoura 101cd1e
TYPO
joaomdmoura 4090a96
starting adapters param's parser
joaomdmoura 260cafd
renaming serializer files in order to fix tests
1c207dd
refactoring deserialization implementation to focus on permit attributes
1abd56c
adding support to relationships
e108572
starting deserialization integration tests structure
64e810d
addnig first json api deserialization test
ec88329
rebasing with master and fixing conflits to make it work
f8d2098
adding singularize when parsing relationships
f1524f0
Add relationship information to PORO models.
beauby 489e317
Reorganize code.
beauby 7ad34f0
updating new json-api test and related structure
5bc262f
rolling back serializer -> serialization name change
6f2acde
Clean rewrite of deserializers, following the various points discussed.
beauby 38f9b03
Add tests for the parsing part of deserialization.
beauby 43ca7e8
Rename misleading method params into params_whitelist.
beauby 55a0bd1
Allow scalar values as resource identifiers in order to handle nil.
beauby 83713d2
Fix @_params never getting set after initialization.
beauby 529508f
Add tests for deserializers' sanitize_params.
beauby 6a3d7a5
Make style consistent.
beauby 2e1e6ae
Revert "Reorganize code."
beauby 042ac69
Revert "Add relationship information to PORO models."
beauby 41bfb36
Clean up.
beauby e4c571c
Add whitelist to attributes/relationships + tests.
beauby 1223b43
Handle id in whitelist.
beauby 3c0c68b
Add test for deserialization into actual ActiveRecord model.
beauby c307ed5
Clean up.
beauby 5f36bf0
Fix Gemfile for jruby.
beauby c29de5a
adding back params method after rebase
7448e2a
removing unecessary test
c754a19
Fix previous merge.
beauby c8d2fc1
Merge pull request #7 from beauby/joao-deserializer2
joaomdmoura 7691298
Namespace AR models + integration test.
beauby 89902b5
Fix extra spaces.
beauby d1aaa37
Merge pull request #9 from beauby/joao-deserializer4
joaomdmoura 169424f
Minor code improvement.
beauby 4cb08e3
Merge pull request #10 from beauby/joao-deserializer5
joaomdmoura 0d2ef9a
Revert Serialization renaming.
beauby 48412a6
Merge pull request #11 from beauby/joao-deserializer6
joaomdmoura cd48fca
Merge pull request #8 from beauby/joao-deserializer3
joaomdmoura 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
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,76 @@ | ||
require 'test_helper' | ||
|
||
module ActionController | ||
module Serializer | ||
class ImplicitDeserializerTest < ActionController::TestCase | ||
class ImplicitDeserializerTestController < ActionController::Base | ||
def create_resource | ||
with_adapter :json_api do | ||
@post = ARModels::Post.create(create_params) | ||
render json: @post, status: :created | ||
end | ||
end | ||
|
||
private | ||
|
||
def create_params | ||
ARModels::PostSerializer.deserialize(params) | ||
end | ||
|
||
def with_adapter(adapter) | ||
old_adapter = ActiveModel::Serializer.config.adapter | ||
# JSON-API adapter sets root by default | ||
ActiveModel::Serializer.config.adapter = adapter | ||
yield | ||
ensure | ||
ActiveModel::Serializer.config.adapter = old_adapter | ||
end | ||
end | ||
|
||
tests ImplicitDeserializerTestController | ||
|
||
def test_json_api_deserialization_on_create | ||
payload = { | ||
data: { | ||
type: 'posts', | ||
attributes: { | ||
title: 'Title 1', | ||
body: 'Body 1' | ||
} | ||
} | ||
} | ||
|
||
post :create_resource, payload | ||
new_post = JSON.parse(@response.body) | ||
|
||
assert_equal payload[:data][:attributes][:title], new_post['data']['attributes']['title'] | ||
assert_equal payload[:data][:attributes][:body], new_post['data']['attributes']['body'] | ||
end | ||
|
||
def test_json_api_deserialization_on_create_with_associations | ||
comment = ARModels::Comment.create(contents: "Comment 1") | ||
payload = { | ||
data: { | ||
type: 'posts', | ||
attributes: { | ||
title: 'Title 1', | ||
body: 'Body 1' | ||
}, | ||
relationships: { | ||
comments: { | ||
data: [{ id: comment.id, type: 'comments' }] | ||
} | ||
} | ||
} | ||
} | ||
|
||
post :create_resource, payload | ||
new_post = JSON.parse(@response.body) | ||
|
||
assert_equal 1, new_post['data']['relationships']['comments']['data'].length | ||
assert_equal "#{comment.id}", new_post['data']['relationships']['comments']['data'][0]['id'] | ||
assert_equal 'ar_models_comments', new_post['data']['relationships']['comments']['data'][0]['type'] | ||
end | ||
end | ||
end | ||
end |
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 |
---|---|---|
|
@@ -34,4 +34,3 @@ def test_fragment_fetch_with_namespaced_object | |
end | ||
end | ||
end | ||
|
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,49 @@ | ||
require 'test_helper' | ||
|
||
module ActiveModel | ||
class Serializer | ||
class DeserializeTest < Minitest::Test | ||
def with_adapter(adapter) | ||
old_adapter = ActiveModel::Serializer.config.adapter | ||
# JSON-API adapter sets root by default | ||
ActiveModel::Serializer.config.adapter = adapter | ||
yield | ||
ensure | ||
ActiveModel::Serializer.config.adapter = old_adapter | ||
end | ||
|
||
def test_json_api_deserialize_on_create | ||
author = ARModels::Author.create(name: "Author 1") | ||
comment1 = ARModels::Comment.create(contents: "Comment 1", author: author) | ||
comment2 = ARModels::Comment.create(contents: "Comment 2", author: author) | ||
payload = { | ||
data: { | ||
type: 'posts', | ||
attributes: { | ||
title: 'Title 1', | ||
body: 'Body 1' | ||
}, | ||
relationships: { | ||
author: { | ||
data: { id: author.id, type: 'authors'} | ||
}, | ||
comments: { | ||
data: [{ id: comment1.id, type: 'comments'}, | ||
{ id: comment2.id, type: 'comments'}] | ||
} | ||
} | ||
} | ||
} | ||
|
||
post = with_adapter :json_api do | ||
ARModels::Post.create(ARModels::PostSerializer.deserialize(ActionController::Parameters.new(payload))) | ||
end | ||
|
||
assert_equal('Title 1', post.title) | ||
assert_equal('Body 1', post.body) | ||
assert_equal(2, post.comments.count) | ||
assert_equal('Author 1', post.author.name) | ||
end | ||
end | ||
end | ||
end |
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.
These being test dependencies should be in the gemspec, no?
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 would agree, but since "minitest" was there already, I did as the Romans do.
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.
😂
Yup.