2

I am using dxa 2.2 (.net) and I created a custom Keyword Model BaseKeywordModel that extends KeywordModel. When DXA tries to map a published component that has a keyword field to a model that uses the BaseKeywordModel for its keyword property it fails.

I've identified the issue. The problem occurs in the DefaultModelBuilder class when trying to do this:

mappedValues.Add(MapKeyword((KeywordModelData) fieldValues, targetType, mappingData.Localization));

The problem here is that mappedValues is a Generic List of BaseKeywordModel and DXA tries to add a KeywordModel to the list, causing an exception.

This test method causes the same issue:


[Test]
public void ModelBuilderMappedValuesTest()
{
    var mappedValues = typeof(BaseKeywordModel).CreateGenericList();
    var kwdModel = new KeywordModel
    {
        Id = "Test",
        ExtensionData = new Dictionary<string, object>()
    };
    try
    {
        mappedValues.Add(kwdModel);
        Assert.Equals(mappedValues.Count, 1);
    }
    catch
    {
        Assert.Fail("Error adding keyword model to list of extended keyword models");
    }

}

This is the BaseKeywordModel class:

[Include("Title", "Description", "Key")]
public class BaseKeywordModel: KeywordModel    {    }

I need to be able to use my own class for Keywords and was wondering whether I am doing anything wrong or this is some kind of bug in the ModelBuilder.

It seems that by adding a Metadata Schema to the Keyword, the ModelBuilder is capable of creating the right instance of the object, i.e. BaseKeywordModel.

With that said, and kinda mimicking what the code does when there's a metadata schema associated to the keyword, instead of the code the ModelBuilder uses now:

if (keywordModelData.SchemaId == null)
{
    result = new KeywordModel
    {
        ExtensionData = keywordModelData.ExtensionData
    };
}

If it would use this code:

if (keywordModelData.SchemaId == null)
{
    result = (KeywordModel) targetType.CreateInstance();
    result.ExtensionData = keywordModelData.ExtensionData;
}

We should be able to use our own Keyword Extended Models without the need of a metadata schema. That change shouldn't cause any regression issues as the code already does its due diligence check:

if (typeof(KeywordModel).IsAssignableFrom(targetType)) { ... }

Will try to open a support ticket to see if I can get a hotfix in place. Of course, if you guys know a different way or see I am doing something wrong, please let me know.

5
  • This is a beautifully detailed report. Thanks! Maybe the fastest way to get this fixed is a pull request with your GitHub issue, since you already know what needs to be fixed.
    – Jan H
    Commented Aug 20, 2019 at 6:46
  • Same issue occurs with RichText sub classes. Will be doing a separate pull request for that Commented Aug 29, 2019 at 13:22
  • RichText similar issue: tridion.stackexchange.com/questions/20327/… Commented Aug 29, 2019 at 13:54
  • The assumption was that you create a KeywordModel subclass if you want to add properties which map to Keyword metadata. Can you elaborate on why you want to create a subclass for Keywords which don’t have metadata? Commented Aug 29, 2019 at 20:03
  • @RickPannekoek, because my KeywordModel uses an custom "Include" attribute, that I use for serialization and it can potentially have other properties, for example helper methods that can do different things based on the 'Key' for example, load additional external data. Commented Sep 3, 2019 at 18:56

1 Answer 1

1

The solution I mentioned in the question seems to be the answer. I have created a pull request in order to introduce the fix and potentially get hotfix out of it.

https://github.com/sdl/dxa-web-application-dotnet/pull/77

1
  • Thanks for your contribution. Much appreciated! However, some expectation management: we strive to incorporate community-reported issues with PR in the next DXA release. Since DXA 2.2 has been released, we’re talking DXA 3.0. Commented Aug 29, 2019 at 20:23

Your Answer

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

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