Skip to content

Commit 6b1c9ee

Browse files
committed
progress on implementing new deserializer -- just have some behavior differences with relationships
1 parent a08f87a commit 6b1c9ee

File tree

2 files changed

+28
-117
lines changed

2 files changed

+28
-117
lines changed

lib/active_model_serializers/adapter/json_api/deserialization.rb

+28-1
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,46 @@ def parse!(document, options = {})
8484
def parse(document, options = {})
8585
# TODO: change to jsonapi-ralis to have default conversion to flat hashes
8686
result = JSONAPI::Rails::DeserializableResource.new(document, options: options).to_hash
87-
result = transform_keys(result) if options[:key_transform]
87+
result = apply_options(result, options)
8888
result
8989
rescue JSONAPI::Parser::InvalidDocument => e
9090
return {} unless block_given?
9191
yield e
9292
end
9393

94+
def apply_options(hash, options)
95+
hash = transform_keys(hash, options) if options[:key_transform]
96+
hash = hash.deep_symbolize_keys
97+
hash = filter_fields(hash, options)
98+
hash = rename_fields(hash, options)
99+
hash
100+
end
101+
94102
# TODO: transform the keys after parsing
95103
# @api private
96104
def transform_keys(hash, options)
97105
transform = options[:key_transform] || :underscore
98106
CaseTransform.send(transform, hash)
99107
end
108+
109+
# executes only
110+
# then except
111+
def filter_fields(hash, options)
112+
hash = hash.select { |k, v| options[:only].include?(k) } if options[:only]
113+
hash = hash.select { |k, v| !options[:except].include?(k) } if options[:except]
114+
hash
115+
end
116+
117+
def rename_fields(hash, options)
118+
return hash unless options[:keys]
119+
120+
keys = options[:keys]
121+
hash.each_with_object({}) do |(k, v), h|
122+
k = keys.fetch(k, k)
123+
h[k] = v
124+
h
125+
end
126+
end
100127
end
101128
end
102129
end

test/action_controller/json_api/deserialization_test.rb

-116
This file was deleted.

0 commit comments

Comments
 (0)