Skip to content

Commit 1c58b52

Browse files
committed
Ensure nested documents multiple levels are included in json. [ fix #2187 ]
1 parent 5fab4e0 commit 1c58b52

File tree

3 files changed

+25
-43
lines changed

3 files changed

+25
-43
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ For instructions on upgrading to newer versions, visit
77

88
### Resolved Issues
99

10+
* \#2187 Ensure all levels of nested documents are serialized in json.
11+
1012
* \#2184 Allow names of relations that conflict with ruby core kernel
1113
methods to pass existence checks.
1214

lib/mongoid/serialization.rb

+8-43
Original file line numberDiff line numberDiff line change
@@ -31,56 +31,21 @@ def serializable_hash(options = nil)
3131

3232
names = field_names(options)
3333

34-
_serializing do
35-
method_names = Array.wrap(options[:methods]).map do |name|
36-
name.to_s if respond_to?(name)
37-
end.compact
34+
method_names = Array.wrap(options[:methods]).map do |name|
35+
name.to_s if respond_to?(name)
36+
end.compact
3837

39-
(names + method_names).each do |name|
40-
without_autobuild do
41-
serialize_attribute(attrs, name, names, options)
42-
end
38+
(names + method_names).each do |name|
39+
without_autobuild do
40+
serialize_attribute(attrs, name, names, options)
4341
end
44-
serialize_relations(attrs, options) if options[:include]
4542
end
43+
serialize_relations(attrs, options) if options[:include]
4644
attrs
4745
end
4846

4947
private
5048

51-
# Enter the serialization block.
52-
#
53-
# @api private
54-
#
55-
# @example Begin serialization.
56-
# document._serializing do
57-
# end
58-
#
59-
# @return [ Object ] The result of the yield.
60-
#
61-
# @since 3.0.0
62-
def _serializing
63-
Threaded.begin("serialization")
64-
yield
65-
ensure
66-
Threaded.exit("serialization")
67-
end
68-
69-
# Are we in a serialization block? We use this to protect multiple
70-
# unnecessary calls to #as_document.
71-
#
72-
# @api private
73-
#
74-
# @example Are we in serialization?
75-
# document._serializing?
76-
#
77-
# @return [ true, false ] If we are serializing.
78-
#
79-
# @since 3.0.0
80-
def _serializing?
81-
Threaded.executing?("serialization")
82-
end
83-
8449
# Get the names of all fields that will be serialized.
8550
#
8651
# @api private
@@ -92,7 +57,7 @@ def _serializing?
9257
#
9358
# @since 3.0.0
9459
def field_names(options)
95-
names = (_serializing? ? attribute_names : as_document.keys + attribute_names).uniq.sort
60+
names = (as_document.keys + attribute_names).uniq.sort
9661

9762
only = Array.wrap(options[:only]).map(&:to_s)
9863
except = Array.wrap(options[:except]).map(&:to_s)

spec/mongoid/serialization_spec.rb

+15
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@
8484
address_attributes["id"].should eq(address.id)
8585
end
8686
end
87+
88+
context "when nested multiple levels" do
89+
90+
let!(:location) do
91+
address.locations.build(name: "home")
92+
end
93+
94+
let(:attributes) do
95+
person.serializable_hash
96+
end
97+
98+
it "includes the deeply nested document" do
99+
attributes["addresses"][0]["locations"].should_not be_empty
100+
end
101+
end
87102
end
88103

89104
context "when the model has attributes that need conversion" do

0 commit comments

Comments
 (0)