Skip to content

Commit 6a0e4d7

Browse files
jeastham1993claude
andcommitted
fix: use Encoding.UTF8.GetBytes instead of JsonSerializer for CloudEvent parsing
JsonSerializer.SerializeToUtf8Bytes on an already-JSON string wraps it in quotes, breaking DecodeStructuredModeMessageAsync. Switch to Encoding.UTF8.GetBytes to pass the raw JSON bytes unchanged across Kafka, Service Bus, and Google Pub/Sub workers. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
1 parent 0232247 commit 6a0e4d7

3 files changed

Lines changed: 3 additions & 10 deletions

File tree

user-management/src/Stickerlandia.UserManagement.Agnostic/KafkaStickerPrintedWorker.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
// Catching generic exceptions is not recommended, but in this case we want to catch all exceptions so that a failure in outbox processing does not crash the application.
1212
#pragma warning disable CA1031
1313

14-
using System.Text.Json;
1514
using CloudNative.CloudEvents;
1615
using CloudNative.CloudEvents.SystemTextJson;
1716
using Confluent.Kafka;
@@ -33,7 +32,6 @@ public class KafkaStickerPrintedWorker(
3332
ProducerConfig producerConfig)
3433
: IMessagingWorker
3534
{
36-
private readonly JsonSerializerOptions _jsonSerializerOptions = new() { PropertyNameCaseInsensitive = true };
3735
private const string topic = "printJobs.completed.v1";
3836
private const string dlqTopic = "printJobs.completed.v1.dlq";
3937

@@ -46,7 +44,7 @@ private async Task<bool> ProcessMessageAsync(StickerPrintedHandler handler,
4644

4745
Log.ReceivedMessage(logger, "kafka");
4846

49-
var detailBytes = JsonSerializer.SerializeToUtf8Bytes(consumeResult.Message.Value, _jsonSerializerOptions);
47+
var detailBytes = System.Text.Encoding.UTF8.GetBytes(consumeResult.Message.Value);
5048
var formatter = new JsonEventFormatter<StickerPrintedEventV1>();
5149
var cloudEvent = await formatter.DecodeStructuredModeMessageAsync(
5250
new MemoryStream(detailBytes), null, new List<CloudEventAttribute>());

user-management/src/Stickerlandia.UserManagement.Azure/ServiceBusStickerPrintedWorker.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// This product includes software developed at Datadog (https://www.datadoghq.com/).
99
// Copyright 2025 Datadog, Inc.
1010

11-
using System.Text.Json;
1211
using Azure.Messaging.ServiceBus;
1312
using CloudNative.CloudEvents;
1413
using CloudNative.CloudEvents.SystemTextJson;
@@ -26,7 +25,6 @@ namespace Stickerlandia.UserManagement.Azure;
2625
[AsyncApi]
2726
public class ServiceBusStickerPrintedWorker : IMessagingWorker
2827
{
29-
private readonly JsonSerializerOptions _jsonSerializerOptions = new() { PropertyNameCaseInsensitive = true };
3028
private readonly IServiceScopeFactory _serviceScopeFactory;
3129
private readonly ILogger<ServiceBusStickerPrintedWorker> _logger;
3230
private readonly ServiceBusProcessor _processor;
@@ -59,7 +57,7 @@ private async Task ProcessMessageAsync(ProcessMessageEventArgs args)
5957
var messageBody = args.Message.Body.ToString();
6058
Log.ReceivedMessage(_logger, "ServiceBus");
6159

62-
var detailBytes = JsonSerializer.SerializeToUtf8Bytes(messageBody, _jsonSerializerOptions);
60+
var detailBytes = System.Text.Encoding.UTF8.GetBytes(messageBody);
6361
var formatter = new JsonEventFormatter<StickerPrintedEventV1>();
6462
var cloudEvent = await formatter.DecodeStructuredModeMessageAsync(
6563
new MemoryStream(detailBytes), null, new List<CloudEventAttribute>());

user-management/src/Stickerlandia.UserManagement.GCP/GooglePubSubStickerPrintedWorker.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// This product includes software developed at Datadog (https://www.datadoghq.com/).
99
// Copyright 2025 Datadog, Inc.
1010

11-
using System.Text.Json;
1211
using CloudNative.CloudEvents;
1312
using CloudNative.CloudEvents.SystemTextJson;
1413
using Datadog.Trace;
@@ -31,8 +30,6 @@ public class GooglePubSubStickerPrintedWorker(
3130
[FromKeyedServices("printJobs.completed.v1")]
3231
SubscriberClient subscriber) : IMessagingWorker
3332
{
34-
private readonly JsonSerializerOptions _jsonSerializerOptions = new() { PropertyNameCaseInsensitive = true };
35-
3633
private Task? _task;
3734

3835
public Task StartAsync()
@@ -84,7 +81,7 @@ public async Task StopAsync(CancellationToken cancellationToken)
8481
using var scope = serviceScopeFactory.CreateScope();
8582
var handler = scope.ServiceProvider.GetRequiredService<StickerPrintedHandler>();
8683

87-
var detailBytes = JsonSerializer.SerializeToUtf8Bytes(messageText, _jsonSerializerOptions);
84+
var detailBytes = System.Text.Encoding.UTF8.GetBytes(messageText);
8885
var formatter = new JsonEventFormatter<StickerPrintedEventV1>();
8986
var cloudEvent = await formatter.DecodeStructuredModeMessageAsync(
9087
new MemoryStream(detailBytes), null, new List<CloudEventAttribute>());

0 commit comments

Comments
 (0)