Try our conversational search powered by Generative AI!

Giang Nguyen
Nov 7, 2019
  3656
(1 votes)

Problematic serialization of EPiServer.Url class in CMS 11

What happened?

Recently, I found out some problem with EPiServer.Url objects, mostly because of its serialization.

Let's start with a demonstration like this:

// The page model
[ContentType GUID="..."]
public class MyPage : PageData
{
    [EditorDescriptor(EditorDescriptorType = typeof(CollectionEditorDescriptor<MyType>))]
    public virtual IList<MyType> MyTypeList { get; set; }
}

// The MyType class
[Serializable]
public class MyType
{
    public string WorksNormal { get; set; }
    public EPiServer.Url WontWork { get; set; }
}

// The definition so it won't cause problem with the editor
[PropertyDefinitionTypePlugIn]
public class DecisionItemListProperty : PropertyList<MyType>
{
}

The innocent-look code above seems to be fine and quite simple to most developer. However, there are 2 dreadful error happen in CMS 11 (while it's totally fine with version 10)!

Firstly, the CMS editor's behavior becomes strange when I tried to add MyType objects to the list. Everything is okay in the editor from the popup, workflow, UI. Surprisingly, after saving the page, regardless it's published or saved as draft, every data of MyType.WontWork is gone while everything is saved as expected. No warning or error or stack trace in log file!

Secondly, Find failed indexing the page, every traces point to MyType.WontWork object. I doubt that Newtonsoft.Json seems unable to serialize the Url class (it works okay if [JsonIgnore] is present).

How come?

What I found regards to this is here https://world.episerver.com/features/episerver-features---november-2017/, in PropertyList<T> improvements (Beta removal) section. However, thing there point out directly if the Url class is affected or not, no warn or suggestion for code changes.

I am also awaiting a comment here to help me figure out the root cause of this.

How to fix?

Easy peasy. Just add some annotation to the Url property, so it will look like this:

[JsonProperty]
[JsonConverter(typeof(UrlConverter))]
public EPiServer.Url WontWork { get; set; }

Just that! It would be fine.

Nov 07, 2019

Comments

Please login to comment.
Latest blogs
Optimizely Search and Navigation - Part 2 - Filter Tips

Introduction Continuing from Part 1 – Search Tips , today I will share the next part – filter tips. The platform versions used for this article are...

Binh Nguyen Thi | Jul 1, 2024

Integrating HubSpot CRM without the MA Connector

Have HubSpot CRM? Want to push user data into it from Optimizely? Don’t have any personalisation requirements with that data? Don’t want to pay $80...

Matt Pallatt | Jun 27, 2024

Keeping the website secure by updating external packages

Did you see the latest warning from Optimizely to update this package with a critical security warning? https://world.optimizely.com/documentation/...

Daniel Ovaska | Jun 27, 2024

Optimizely CMS image anonymization now available for Linux!

The famous image anonymization add-on for Optimizely CMS, with at least 5 downloads, is now finally available for use on Linux. Supports simultaneo...

Tomas Hensrud Gulla | Jun 25, 2024 | Syndicated blog