contentful.net icon indicating copy to clipboard operation
contentful.net copied to clipboard

Null reference exception when rendering rich text content with a couple of content elements sharing the same asset

Open Pliuxus opened this issue 3 years ago • 3 comments

Hi,

I have a content item that has a rich text field into which I'm embedding another content item (different type) having a media field, populated by a draft image. Then in the same rich text field I've embedded the same image as an asset. With this setup, when I'm rendering this content on a page using <contentful-rich-text document/> tag, I'm getting a null reference exception here:

Contentful.Core.Models.AssetRenderer.RenderAsync(IContent content) in Authoring.cs if (nodeType != "asset-hyperlink" && asset.File?.ContentType != null && asset.File.ContentType.ToLower().Contains("image"))

Here's the content setup (an embedded entry and an embedded asset): image

Pliuxus avatar Dec 17 '21 14:12 Pliuxus

Hi @Pliuxus thanks for the report. This sounds interesting. Not sure if using ResolveEntriesSelectivley=true solves this, but it's almost certainly related to what i describe here: https://robertlinde.se/posts/why-is-my-item-null-contentful-net/

If setting ResolveEntriesSelectivley=true doesn't solve this problem for you I'll look into setting up a similar setup and investigate.

Roblinde avatar Dec 17 '21 15:12 Roblinde

Thanks, @Roblinde. Unfortunately it did not help, still getting the same null reference exception right here: image

Pliuxus avatar Dec 21 '21 09:12 Pliuxus

@Pliuxus sorry for taking some time to get around to this. I have investigated now and been able to reproduce the problem. I will fix so that this does not throw an exception in an upcoming fix. However, the problem is related to the blog post I alluded to earlier. What you need to do here is to instruct the serializer through a ContentTypeResolver how you want to resolve the entry-reference that you are inserting. (See https://www.contentful.com/developers/docs/net/tutorials/using-net-cda-sdk/#get-entries-of-multiple-types-or-by-interface for an example implementation.)

Once you have instructed it how to serialize the entry the asset should also be able to resolve without a problem.

Let me know if you need any help with getting it working.

Roblinde avatar Feb 18 '22 15:02 Roblinde