CustomContentLoaderService disables variants from being returned from content delivery api

Vote:
 

Hi all,

We have created an enhanced content loader service that enables draft content to be returned from the content delivery api. The enhanced content loader is working fine as expected. However, we noticed that when we inject the service to be used in our project it breaks the ability for variants to be retrieved from the commerce side of things using /children by only recieving an empty array as a response. Ex request url:

https://localhost:5000/catalog/category/product/children

We used this link as a reference to build our service:
https://docs.developers.optimizely.com/content-management-system/v1.5.0-content-delivery-api/docs/customizing-content-delivery-api-for-edit-view#return-draft-and-expired-content

here is the code for our enhanced content loader service:

public class EnhancedContentLoaderService(
    IContentLoader contentLoader,
    IHttpContextAccessor httpContextAccessor,
    IPublishedStateAssessor publishedStateAssessor,
    IPermanentLinkMapper permanentLinkMapper,
    IContentProviderManager providerManager
        ) : ContentLoaderService(
            contentLoader,
            permanentLinkMapper,
            providerManager,
            publishedStateAssessor)
{
    protected override bool ShouldContentBeExposed(IContent content)
    {
        // In EditMode, unpublished or expired content is still returned
        return GetContextMode() == ContextMode.Edit || base.ShouldContentBeExposed(content);
    }

    private ContextMode GetContextMode()
    {
        var httpCtx = httpContextAccessor.HttpContext;
        if (httpCtx?.Request is null || !httpCtx.Request.Query.ContainsKey(PageEditing.EpiEditMode))
        {
            return ContextMode.Default;
        }

        if (bool.TryParse(httpCtx.Request.Query[PageEditing.EpiEditMode], out var editMode))
        {
            return editMode ? ContextMode.Edit : ContextMode.Preview;
        }

        return ContextMode.Undefined;
    }
}

The service is being injected like this:

        context.ConfigurationComplete += (o, e) =>
        {
            context.Services.AddSingleton<ContentLoaderService, EnhancedContentLoaderService>();
        };


Any ideas on what could be the issue?
Thanks in advance

#332081
Oct 28, 2024 8:51
Vote:
 

Could you temporarily remove GetContextMode() == ContextMode.Edit || and check if the right list returns ? 

#332084
Oct 28, 2024 16:24
Taher.elhares - Oct 29, 2024 8:05
Please check my post below
Vote:
 

I am still facing the same issue even with this:

    protected override bool ShouldContentBeExposed(IContent content)
    {
        // In EditMode, unpublished or expired content is still returned
        return base.ShouldContentBeExposed(content);
    }
#332128
Oct 29, 2024 8:04
Vote:
 

It seems the problem is with something else. could you remove the registration (i.e. use the default loader) to see if it still happens?

#332130
Oct 29, 2024 8:37
Taher.elhares - Oct 29, 2024 9:46
Please check my comment below
Vote:
 

When removing this line it works again:

            context.Services.AddSingleton<ContentLoaderService, EnhancedContentLoaderService>();

Could this be a bug?

#332132
Oct 29, 2024 9:47
Quan Mai - Oct 29, 2024 11:53
yes maybe some check somewhere that will act differently if a custom implementation is registered. let me check
Vote:
 

It turns out that there is an internal implemenation of ContentLoaderService named CatalogContentLoaderService which handles the catalog content. if you register like that, it will simply override CatalogContentLoaderService which explains why you get an empty array. the right approach in this case is to use the intercept pattern, and make sure to register your interceptor after calling AddCommerceApi which registers CatalogContentLoaderService

it's fair to question why CatalogContentLoaderService is not override-able, but it is what it is, for now 

#332135
Oct 29, 2024 11:59
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.