Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The CloudEvent source gets overwritten by the emulator #106

Open
dzhemaptula-tomtom opened this issue Jan 21, 2025 · 7 comments
Open

The CloudEvent source gets overwritten by the emulator #106

dzhemaptula-tomtom opened this issue Jan 21, 2025 · 7 comments

Comments

@dzhemaptula-tomtom
Copy link
Contributor

Describe the bug
The original source of the CloudEvent gets overwritten by the topic.

To Reproduce
Steps to reproduce the behavior:

  1. Send any event with a custom source set on the cloud event
  2. Read event

Expected behavior
The source should stay the same as received originally.

Additional context
Permalink to where this issue happens

As described above. In azure the original source from the cloud event is kept when forwarding it to the subscribed endpoints.

@jochenhebbrecht
Copy link

jochenhebbrecht commented Jan 21, 2025

Hi,

I stumbled into the same problem as the topic poster today.

The CloudEvent I'm posting towards the EventGrid topic follows this format:

{
    "id": "5e3ab450-f4a5-40cc-b1ec-819b0bd1bc26",
👉  "source": "/my-source-identifier",
    "data": {
        "..."
    },
    "type": "my.type.of.event",
    "time": "2025-01-17T14:07:03.303787590Z",
    "specversion": "1.0",
    "datacontenttype": "application/json"
}

On the subscription endpoint, I suddenly see this event:

{
    "id": "5e3ab450-f4a5-40cc-b1ec-819b0bd1bc26",
👉  "source": "/subscriptions/00000000-0000-0000-0000-00000000/resourceGroups/my-resource-group/providers/Microsoft.EventGrid/topics/my-event-grid-topic",
    "data": {
        "..."
    },
    "type": "my.type.of.event",
    "time": "2025-01-17T14:07:03.303787590Z",
    "specversion": "1.0",
    "datacontenttype": "application/json"
}

On the subscription endpoint, we actually rely on the value in the source field. On Azure itself, the source remains the same and stable. The behaviour of the emulator is in this way unexpected and potentially wrong. A potential (quick?) fix would be to not override the source field if it's already there. An example patch:

diff --git a/src/EventGridEmulator/EventHandling/CloudEventHttpContextHandler.cs b/src/EventGridEmulator/EventHandling/CloudEventHttpContextHandler.cs
index 44bef91..22ef190 100644
--- a/src/EventGridEmulator/EventHandling/CloudEventHttpContextHandler.cs
+++ b/src/EventGridEmulator/EventHandling/CloudEventHttpContextHandler.cs
@@ -21,7 +21,10 @@ internal sealed class CloudEventHttpContextHandler : BaseEventHttpContextHandler
     {
         foreach (var @event in cloudEvents)
         {
-            @event.Source = $"{SubscriberConstants.DefaultTopicValue}{topicName}";
+            if (String.IsNullOrEmpty(@event.Source))
+            {
+                @event.Source = $"{SubscriberConstants.DefaultTopicValue}{topicName}";
+            }
         }
     }

@meziantou
Copy link
Contributor

If the emulator behavior differ from EventGrid, then it should be fixed. Would you like to create a PR with the suggested fix?

@jochenhebbrecht
Copy link

jochenhebbrecht commented Jan 22, 2025

Thanks for your reply @meziantou! According to the contributing guidelines on this repository, I noticed you don't accept external pull requests (yet). But I assume you allow a PR from a forked repository?

@meziantou
Copy link
Contributor

You can create a PR from your fork. To accept the PR, and because of our compliance process, we'll create a new PR with your commits (you will still be author of the commit in the git history).

@dzhemaptula-tomtom
Copy link
Contributor Author

Hey that's great! I already have a fork, I'll get to it soon. Thanks both for the updates!

@dzhemaptula-tomtom
Copy link
Contributor Author

Opened #107 as a fix

@jochenhebbrecht
Copy link

Happy to announce that with version 0.4.5, I have no longer have the issue in which I lose the contents of the source field in the CloudEvent message 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants