Skip to content

Commit

Permalink
Merge branch 'root_node_type_check' of https://github.com/EasyPost/her
Browse files Browse the repository at this point in the history
…into EasyPost-root_node_type_check

Co-authored-by: Andrew Tribone <[email protected]>
  • Loading branch information
zacharywelch and Andrew Tribone committed Apr 27, 2018
2 parents 6fe0c6e + 3d2636b commit 23fdb37
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/her/model/parse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ def root_element(value = nil)

# @private
def root_element_included?(data)
data.keys.to_s.include? @_her_root_element.to_s
element = data[root_element]
element.is_a?(Hash) || element.is_a?(Array)
end

# @private
Expand Down
21 changes: 21 additions & 0 deletions spec/model/parse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -493,4 +493,25 @@ def name
expect(user.to_params).to eq(model: { name: 'foo' })
end
end

context "when attribute uses the same name as root element" do
before do
Her::API.setup url: "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
end

Her::API.default_api.connection.adapter :test do |stub|
stub.post("/users") { |env| [200, {}, { user: "foobar", id: 1, fullname: params(env)[:fullname] }.to_json] }
end

spawn_model "Foo::User"
end

it "parses as attribute instead of root element" do
user = Foo::User.create(fullname: "barfoo")
expect(user.fullname).to eq "barfoo"
expect(user.user).to eq "foobar"
end
end
end

0 comments on commit 23fdb37

Please sign in to comment.