Skip to content

Commit f588665

Browse files
Attach metadata to each individual message in bulkpublish, if it exists (#1437)
* Instead of just attaching metadata to the full envelope, the metadata is attached to each individual message as well, if it exists at all. Signed-off-by: Chris Walsh <[email protected]> Co-authored-by: Whit Waldo <[email protected]>
1 parent d1c820f commit f588665

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/Dapr.Client/DaprClientGrpc.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,12 @@ events[counter] is CloudEvent
215215
? Constants.ContentTypeCloudEvent
216216
: Constants.ContentTypeApplicationJson,
217217
};
218+
219+
if (metadata != null)
220+
{
221+
entry.Metadata.Add(metadata);
222+
}
223+
218224
envelope.Entries.Add(entry);
219225
entryMap.Add(counter.ToString(), new BulkPublishEntry<TValue>(
220226
entry.EntryId, events[counter], entry.ContentType, entry.Metadata));

test/Dapr.Client.Test/BulkPublishEventApiTest.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,30 +87,40 @@ await daprClient.BulkPublishEventAsync(TestPubsubName, TestTopicName, bulkPublis
8787
request.Dismiss();
8888

8989
var envelope = await request.GetRequestEnvelopeAsync<Autogenerated.BulkPublishRequest>();
90-
90+
9191
envelope.Entries.Count.ShouldBe(2);
9292
envelope.PubsubName.ShouldBe(TestPubsubName);
9393
envelope.Topic.ShouldBe(TestTopicName);
9494

95+
//Validate that the metadata is on the envelope
9596
envelope.Metadata.Count.ShouldBe(2);
9697
envelope.Metadata.Keys.Contains("key1").ShouldBeTrue();
9798
envelope.Metadata.Keys.Contains("key2").ShouldBeTrue();
9899
envelope.Metadata["key1"].ShouldBe("value1");
99100
envelope.Metadata["key2"].ShouldBe("value2");
100101

102+
//Validate that the metadata is also on each entry
101103
var firstEntry = envelope.Entries[0];
102104

103105
firstEntry.EntryId.ShouldBe("0");
104106
firstEntry.ContentType.ShouldBe(TestContentType);
105107
firstEntry.Event.ToStringUtf8().ShouldBe(JsonSerializer.Serialize(bulkPublishData[0], client.InnerClient.JsonSerializerOptions));
106-
firstEntry.Metadata.ShouldBeEmpty();
108+
firstEntry.Metadata.Keys.Count.ShouldBe(2);
109+
firstEntry.Metadata.ContainsKey("key1").ShouldBeTrue();
110+
firstEntry.Metadata["key1"].ShouldBe("value1");
111+
firstEntry.Metadata.ContainsKey("key2").ShouldBeTrue();
112+
firstEntry.Metadata["key2"].ShouldBe("value2");
107113

108114
var secondEntry = envelope.Entries[1];
109115

110116
secondEntry.EntryId.ShouldBe("1");
111117
secondEntry.ContentType.ShouldBe(TestContentType);
112118
secondEntry.Event.ToStringUtf8().ShouldBe(JsonSerializer.Serialize(bulkPublishData[1], client.InnerClient.JsonSerializerOptions));
113-
secondEntry.Metadata.ShouldBeEmpty();
119+
secondEntry.Metadata.Keys.Count.ShouldBe(2);
120+
secondEntry.Metadata.ContainsKey("key1").ShouldBeTrue();
121+
secondEntry.Metadata["key1"].ShouldBe("value1");
122+
secondEntry.Metadata.ContainsKey("key2").ShouldBeTrue();
123+
secondEntry.Metadata["key2"].ShouldBe("value2");
114124

115125
// Create Response & Respond
116126
var response = new Autogenerated.BulkPublishResponse
@@ -183,25 +193,35 @@ await daprClient.BulkPublishEventAsync(TestPubsubName, TestTopicName, bulkPublis
183193
envelope.PubsubName.ShouldBe(TestPubsubName);
184194
envelope.Topic.ShouldBe(TestTopicName);
185195

196+
//Validate the metadata on the envelope
186197
envelope.Metadata.Count.ShouldBe(2);
187198
envelope.Metadata.Keys.Contains("key1").ShouldBeTrue();
188199
envelope.Metadata.Keys.Contains("key2").ShouldBeTrue();
189200
envelope.Metadata["key1"].ShouldBe("value1");
190201
envelope.Metadata["key2"].ShouldBe("value2");
191202

203+
//Validate the metadata on each entry
192204
var firstEntry = envelope.Entries[0];
193205

194206
firstEntry.EntryId.ShouldBe("0");
195207
firstEntry.ContentType.ShouldBe(TestContentType);
196208
firstEntry.Event.ToStringUtf8().ShouldBe(JsonSerializer.Serialize(bulkPublishData[0], client.InnerClient.JsonSerializerOptions));
197-
firstEntry.Metadata.ShouldBeEmpty();
209+
firstEntry.Metadata.Keys.Count.ShouldBe(2);
210+
firstEntry.Metadata.ContainsKey("key1").ShouldBeTrue();
211+
firstEntry.Metadata["key1"].ShouldBe("value1");
212+
firstEntry.Metadata.ContainsKey("key2").ShouldBeTrue();
213+
firstEntry.Metadata["key2"].ShouldBe("value2");
198214

199215
var secondEntry = envelope.Entries[1];
200216

201217
secondEntry.EntryId.ShouldBe("1");
202218
secondEntry.ContentType.ShouldBe(TestContentType);
203219
secondEntry.Event.ToStringUtf8().ShouldBe(JsonSerializer.Serialize(bulkPublishData[1], client.InnerClient.JsonSerializerOptions));
204-
secondEntry.Metadata.ShouldBeEmpty();
220+
secondEntry.Metadata.Keys.Count.ShouldBe(2);
221+
secondEntry.Metadata.ContainsKey("key1").ShouldBeTrue();
222+
secondEntry.Metadata["key1"].ShouldBe("value1");
223+
secondEntry.Metadata.ContainsKey("key2").ShouldBeTrue();
224+
secondEntry.Metadata["key2"].ShouldBe("value2");
205225

206226
// Create Response & Respond
207227
var response = new Autogenerated.BulkPublishResponse
@@ -336,4 +356,4 @@ private class Widget
336356
public string Size { get; set; }
337357
public string Color { get; set; }
338358
}
339-
}
359+
}

0 commit comments

Comments
 (0)