cloudevents / sdk-csharp

CSharp SDK for CloudEvents
Apache License 2.0
280 stars 81 forks source link

CloudEvent Source type #315

Closed glucaci closed 18 hours ago

glucaci commented 2 days ago

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!

jskeet commented 2 days ago

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.

glucaci commented 19 hours ago

Thanks for the clarification, but then why Microsoft decided to use a string type on the Source property is a good question.

jskeet commented 18 hours ago

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.