Skip to content

Commit 129c144

Browse files
committed
Build no derived operations when the id source field is absent
`Operation::Update.operations_for` fetched derived document ids with `fetch_leaf_values_at_path` and no default, so a record that omitted the id source field (or the parent object of a nested id source) raised `KeyError` and failed the whole event. A record with the field present but `nil` already produced no derived operations, so the two shapes behaved differently. Treat an absent key the same as a null value: produce no derived operation. A parent that is present but not a hash still raises, since that indicates a malformed record rather than a missing value. The factory spec's "malformed event" example relied on an omitted `cost` field raising; it now uses a scalar `cost` where an object is expected.
1 parent 99aafbb commit 129c144

4 files changed

Lines changed: 33 additions & 8 deletions

File tree

‎elasticgraph-indexer/lib/elastic_graph/indexer/operation/factory.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def build_failed_result(event, validation_target, validation_message)
112112
# for real on these operations, anyway.
113113
#
114114
# Building operations for an event we already know is malformed can itself fail--for example, when the
115-
# record omits a field an update target derives its id from. Reporting what was malformed matters more
115+
# record uses a scalar where an update target expects an object. Reporting what was malformed matters more
116116
# than reporting the operations we would have run, and `FailedEventError#operations` is documented to
117117
# sometimes be empty for exactly this reason, so we fall back to no operations rather than let a second
118118
# failure mask the first.

‎elasticgraph-indexer/lib/elastic_graph/indexer/operation/update.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def self.operations_for(
3535
)
3636

3737
Support::HashUtil
38-
.fetch_leaf_values_at_path(prepared_record, update_target.id_source.split("."))
38+
.fetch_leaf_values_at_path(prepared_record, update_target.id_source.split(".")) { [] }
3939
.reject { |id| id.to_s.strip.empty? }
4040
.uniq
4141
.map { |doc_id| new(event, prepared_record, destination_index_def, update_target, doc_id, destination_index_mapping, conflict_retries) }

‎elasticgraph-indexer/spec/unit/elastic_graph/indexer/operation/factory_spec.rb‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,14 @@ module Operation
273273
end
274274

275275
context "when an event is malformed in a way that also breaks building its operations", :expect_warning_logging do
276-
# `Widget` requires `cost`, and its derived `WidgetCurrency` update target sources its id
277-
# from `cost.currency`, so a `Widget` with no `cost` both fails validation and breaks
276+
# `Widget` requires an object for `cost`, and its derived `WidgetCurrency` update target
277+
# sources its id from `cost.currency`, so a scalar `cost` both fails validation and breaks
278278
# building the operations we attach to the `FailedEventError`. Reporting the malformation
279279
# matters more than reporting operations we are never going to run, and
280280
# `FailedEventError#operations` is documented as sometimes being empty for this reason.
281281
let(:event) do
282282
event = build_upsert_event(:widget, id: "1", __version: 1)
283-
event.with(record: event.record.except("cost"))
283+
event.with(record: event.record.merge("cost" => "not an object"))
284284
end
285285

286286
it "still reports what was malformed instead of letting the second failure mask the first" do

‎elasticgraph-indexer/spec/unit/elastic_graph/indexer/operation/update_spec.rb‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,39 @@ module Operation
161161
]
162162
end
163163

164-
it "returns no datastore bulk actions if the source document lacks the id field of the update target" do
164+
it "returns no datastore bulk actions if the source document omits or nulls the id field of the update target" do
165165
indexer = indexer_with_widget_workspace_index_definition do |index|
166166
# no customization
167167
end
168168

169-
operations = operations_for_indexer(indexer, event: event.with(record: event.record.merge("workspace_id" => nil)))
169+
[event.record.except("workspace_id"), event.record.merge("workspace_id" => nil)].each do |record|
170+
expect(operations_for_indexer(indexer, event: event.with(record: record))).to eq []
171+
end
172+
end
170173

171-
expect(operations).to eq []
174+
it "returns no datastore bulk actions if a nested id's parent or leaf is absent or null" do
175+
indexer = indexer_with_widget_workspace_index_definition(id_source: "embedded_values.workspace_id") do |index|
176+
# no customization
177+
end
178+
records = [
179+
event.record.except("embedded_values"),
180+
event.record.merge("embedded_values" => nil),
181+
event.record.merge("embedded_values" => {"name" => "embedded_name"}),
182+
event.record.merge("embedded_values" => {"workspace_id" => nil})
183+
]
184+
185+
records.each do |record|
186+
expect(operations_for_indexer(indexer, event: event.with(record: record))).to eq []
187+
end
188+
end
189+
190+
it "still raises if a nested id's parent has a malformed non-object shape" do
191+
indexer = indexer_with_widget_workspace_index_definition(id_source: "embedded_values.workspace_id") do |index|
192+
# no customization
193+
end
194+
malformed = event.with(record: event.record.merge("embedded_values" => "not an object"))
195+
196+
expect { operations_for_indexer(indexer, event: malformed) }.to raise_error(KeyError, /not a `Hash`/)
172197
end
173198

174199
it "returns no datastore bulk actions if the source document has an empty string for the id field of the update target" do

0 commit comments

Comments
 (0)