contentful.net
contentful.net copied to clipboard
DeserializeWebhookRequestAsync cannot deserialize a Document (rich text content)
I cannot deserialize a webhook call with rich text content. This is the error I get when calling WebhookDeserializer.DeserializeWebhookRequestAsync
:
Deserialization of interface types is not supported. Type 'Contentful.Core.Models.IContent'. Path: $.fields.content.en-US.content[0]
I can work around this by using the contentful client to retrieve the entry as it will deserialize the content properly.
Ah yes, this is sort of hard to have work directly from the SDK, I'm starting to feel like the IContent
interface might have been a mistake.
What you could do is to add the ContentJsonConverter
to your WebhookDeserializer
, this is the converter that knows how to deserialize IContent
.
Something like: WebhookDeserializer.SerializerSettings.Converters.Add(new ContentJsonConverter());
Another option is to not use the Document
class in your class that you deserialize into from the webhook. You could use a custom class, dynamic or just omitt it from deserialization, unless you need this data of course.
I ended up instantiating my own instance of the Json.NET JsonSerializer using the IContentfulClient.SerializerSettings:
_contentfulSerializer = JsonSerializer.Create(contentful.SerializerSettings);
var bodyReader = new StreamReader(Request.Body);
var json = await bodyReader.ReadToEndAsync();
var stringReader = new StringReader(json);
using var jsonReader = new JsonTextReader(bodyReader);
var localizedArticle = _contentfulSerializer.Deserialize<LocalizedArticle>(jsonReader);
And yes, I needed that many readers because JSON.NET tries to read the request body stream synchronously, and ASP.NET disabled that in asp.net 3.0.
It would be nice if you exposed the serializer used by the client publicly, through the interface, so that I could use it in my webhooks.