sdk-csharp
sdk-csharp copied to clipboard
CloudEvent Source type
Hi,
I wanted to write a mapping between the Azure.Messaging.CloudEvent which is JSON spec conform and the CloudNative.CloudEvents.CloudEvent and realized the difference on the Source Property type.
There is any reason why in CloudNative.CloudEvents.CloudEvent the Source property is Uri because in Azure.Messaging.CloudEvent it's a string.
Also taking a look of the spec examples and taking the value 1-555-123-4567, can be seen that this is not a valid value for an Uri type.
Thanks for clarification!
1-555-123-4567 is a valid URI reference. It's unusual for sure, but it's valid.
Here's some sample code showing it in action:
using CloudNative.CloudEvents;
using CloudNative.CloudEvents.SystemTextJson;
using System.Text;
var evt = new CloudEvent
{
Id = "my-id",
Source = new Uri("1-555-123-4567", UriKind.RelativeOrAbsolute),
Type = "my-type"
};
evt.Validate();
CloudEventFormatter formatter = new JsonEventFormatter();
var bytes = formatter.EncodeStructuredModeMessage(evt, out _);
var json = Encoding.UTF8.GetString(bytes.Span);
Console.WriteLine(json);
Output:
{"specversion":"1.0","id":"my-id","source":"1-555-123-4567","type":"my-type"}
Fundamentally, the Source property is a Uri because the type of the source attribute (see the spec examples link you had before) is URI-reference.
Thanks for the clarification, but then why Microsoft decided to use a string type on the Source property is a good question.
I can't answer that, I'm afraid. I'm going to close this issue now as I believe I've answered it as thoroughly as I can.