sdk-csharp icon indicating copy to clipboard operation
sdk-csharp copied to clipboard

Support text/plain Media Type in CloudEventJsonInputFormatter

Open mustafacagataytulun opened this issue 4 years ago • 2 comments

I don't know that this could be considered as an issue or not, but I want to ask/suggest it anyway.

We are trying to use cloud events on OpenShift platform. Our problem is the system sends events to our ASP.NET Core application with text/plain Content-Type. We solved this by copying source code of CloudEventJsonInputFormatter to our project, changing its name to CloudEventTextInputFormatter, and changing SupportedMediaTypes to text/plain like this:

SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("text/plain"));

Since the underlying ReadCloudEventAsync method does not make any transformation while reading the request body, it works like a charm. Here is the code for assigning Data field.

cloudEvent.Data = await new StreamReader(httpRequest.Body, Encoding.UTF8).ReadToEndAsync();

What do you think? Could it be integrated into CloudEventJsonInputFormatter? All it needs is the SupportedMediaTypes line I shared above. I could be like this:

SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/json"));
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/cloudevents+json"));
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("text/plain"));

mustafacagataytulun avatar Oct 30 '20 14:10 mustafacagataytulun

Sorry for not looking at this issue before.

It seems to me that if the HTTP request has a content type of text/plain, and doesn't have a ce-specversion header, it shouldn't be recognized as a CloudEvent. Or are you saying that it does have a ce-specversion header? If you could give a sample request, that would really help.

While it's very likely that the CloudEventJsonInputFormatter will be revised before the 2.0 release, I wouldn't expect it to handle requests that don't conform to the CloudEvents spec... and if it's a binary mode event that has a text/plain payload, I suspect you'd need the a CloudEventPlainTextInputFormatter or equivalent... a class that is designed specifically for JSON has no business handling a request that doesn't use JSON, IMO. (We may well make it easier to create a flexible TextInputFormatter so that you can pass in any CloudEventFormatter and go from there - the event formatter design is still somewhat fluid.)

jskeet avatar Feb 12 '21 15:02 jskeet

This is now no longer blocking the 2.0 release, as we've moved it into the ASP.NET Core sample. However, that was basically because there's more to do to it than we have time to do right now. We should have a think about what we want it to look like for 2.1.

jskeet avatar Jun 03 '21 17:06 jskeet