contentful / contentful.net

.NET Library for Contentful's Content Delivery and Management API
MIT License
95 stars 53 forks source link

The same instance of multiple entries in a rich text field are not mapped in a Document #333

Closed peereflits closed 1 month ago

peereflits commented 4 months ago

Scenario:

Given a rich text field that has one entry "X" and that appears multiple times, once as inline and once as embedded block, When it is serialized as a Document in the SDK It should have a Data.Target of the IContent nodes of entry "X"

Actual: The first IContent node has a Data.Target instance of JObject that can be serialized as 'entry "X"' The second IContent node has a Data.Target property of null

Expected: Both IContent nodes hava a Data.Target instance of JObject that can be serialized as 'entry "X"'

Detail information / steps to reproduce

Given Contentfull content type:

{
  "name": "Rich Text example",
  "description": "Demo content model to generate a rich text example.",
  "displayField": "internalTitle",
  "fields": [
    {
      "id": "internalTitle",
      "name": "Internal title",
      "type": "Symbol",
      // other omitted for brevity
    },
    {
      "id": "text",
      "name": "Text",
      "type": "RichText",
      "localized": false,
      "required": true,
      "validations": [
        {
          "enabledMarks": [
            // all, omitted for brevity
          ],
          "message": "Only bold, italic, underline, code, superscript, and subscript marks are allowed"
        },
        {
          "enabledNodeTypes": [
            // all other node types enabled, omitted for brevity
            "embedded-entry-block",
            "entry-hyperlink",
            "embedded-entry-inline"
          ],
          "message": "a custom error message"
        },
        {
          "nodes": {}
        }
      ],
      "disabled": false,
      "omitted": false
    }
  ],
  "sys": {
    // omitted for brevity
    },
    "id": "richTextDemo",
    "type": "ContentType",
    // Other properties omitted for brevity

    "urn": "crn:contentful:::content:spaces/{spaceId}/environments/develop/content_types/richTextDemo"
  }
}

This type can be serialized as dotnet type:

internal record RichTextDemo
{
    public required SystemProperties Sys { get; set; }
    public string? InternalTitle { get; set; }
    public required Document Text { get; set; }
}

In Contentful prepare an instance of richTextTyDemo and add an "embedded-entry-inline" entry that references an other content type instance and add an "embedded-entry-block" that references the same content type instance.

When execute a query with a valid entryId like this:

private async Task<RichTextDemo> GetDemoDocument(string entryId)
{
    IContentfulClient client = CreateClient();

    var builder = new QueryBuilder<RichTextDemo>()
        .ContentTypeIs("richTextDemo")
        .FieldEquals(f => f.Sys.Id, entryId)
        .LocaleIs("en")
        .Limit(1)
        .Include(10);

    var result = (await client.GetEntries(builder)).Single();
    return result;
}

The result of this query should be:

  1. result is not null and of type RichTextDemo => true
  2. result has a property Text of type Document and is not null => true
  3. The Text has two IContent nodes with a Data.Target propoerty that can be serialized to that referenced instance => false

The first IContent node has a serializable Data.Target.

The second IContent node has a Data.Target that is null.

I suspect that the error is in the ContentfulClient between Line 233 and Line 271.

Roblinde commented 4 months ago

hi @peereflits

Have you had a look at this blog post I wrote a while back? https://robertlinde.se/posts/why-is-my-item-null-contentful-net/

Not sure if this is the root cause in this particular scenario, but setting a content type resolver and also potentially ResolveEntriesSelectively might resolve the issue.

peereflits commented 4 months ago

Hi @Roblinde , Thanks for the feedback. I'll look into it.

peereflits commented 1 month ago

@Roblinde : I didn't have the time/opportunities to dive into your sugestion. My bet is that https://github.com/contentful/contentful.net/pull/343 will solve it. (@kheurterincentro & I where on the same project where the issue ocurred.)

Roblinde commented 1 month ago

@peereflits great, I'll look at that PR and get it packaged into a version shortly.

Roblinde commented 1 month ago

@peereflits this should now be resolved in version 8.0.0