0

I already reported/asked about a similar issue with extended Keyword Models here:

Does DXA support Extended Keyword Models?

The same problem occurs if we try to extend from Sdl.Web.Common.Models.RichTextand use that sub class in our own models, the DefaultModelBuilder mapping fails.

1
  • Can you elaborate on why you want to subclass RichText in the first place? Aug 29, 2019 at 19:58

1 Answer 1

0

This is how I see a possible solution and will be what I will add to my pull request. These methods are the ones causing issues:

protected virtual object MapString(string stringValue, Type targetType, string pageContextId, Localization localization) 
...
if (targetType == typeof(RichText))
{
    return new RichText(stringValue);
}

and

protected virtual object MapRichText(RichTextData richTextData, Type targetType, Localization localization)
...
if (targetType == typeof(RichText))
{
    return new RichText(fragments);
}

After looking deeper at the code, a potential solution would be to replace those if's with these respectively:

On the MapString method:

if (typeof(RichText).IsAssignableFrom(targetType))
{
    var richText = targetType.CreateInstance(stringValue);
    return richText;
}

On the MapRichText method:

if (typeof(RichText).IsAssignableFrom(targetType))
{
    var customRichText = targetType.CreateInstance(fragments);
    return customRichText;
}
RichText richText = new RichText(fragments);
return richText.ToString();

So unless anybody has a reason for this not to be feasible, i.e. extending RichText models, I will go ahead and create a new (issue) and Pull Request to try to get my code in place.

Let me know your thoughts.

**Update: Issue has been reported and pull request submitted: ** https://github.com/sdl/dxa-web-application-dotnet/issues/78

9
  • This same issue can potentiall occur with Link sub-classes and Tag sub-classes Aug 29, 2019 at 13:52
  • And MultimediaItem iirc
    – Quirijn
    Aug 29, 2019 at 14:18
  • The Tag class is legacy from early DXA versions. Class KeywordModel was introduced later as a (concrete) base class. Class MediaItem is an abstract base class, so you must use subclasses. Aug 29, 2019 at 20:07
  • Can you elaborate on why you want to suclass Link? Aug 29, 2019 at 20:08
  • Just a thought - could you have done this by deriving from DefaultModelBuilder into your own Model Builder class and providing your own implementations of those methods in the derived class? I assume that's was the intention behind marking them as virtual.
    – Neil
    Aug 30, 2019 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.