SaaS CMS has officially launched! Learn more now.

Giang Nguyen
Nov 7, 2019
  3668
(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 SaaS CMS Concepts and Terminologies

Whether you're a new user of Optimizely CMS or a veteran who have been through the evolution of it, the SaaS CMS is bringing some new concepts and...

Patrick Lam | Jul 15, 2024

How to have a link plugin with extra link id attribute in TinyMce

Introduce Optimizely CMS Editing is using TinyMce for editing rich-text content. We need to use this control a lot in CMS site for kind of WYSWYG...

Binh Nguyen Thi | Jul 13, 2024

Create your first demo site with Optimizely SaaS/Visual Builder

Hello everyone, We are very excited about the launch of our SaaS CMS and the new Visual Builder that comes with it. Since it is the first time you'...

Patrick Lam | Jul 11, 2024

Integrate a CMP workflow step with CMS

As you might know Optimizely has an integration where you can create and edit pages in the CMS directly from the CMP. One of the benefits of this i...

Marcus Hoffmann | Jul 10, 2024

GetNextSegment with empty Remaining causing fuzzes

Optimizely CMS offers you to create partial routers. This concept allows you display content differently depending on the routed content in the URL...

David Drouin-Prince | Jul 8, 2024 | Syndicated blog

Product Listing Page - using Graph

Optimizely Graph makes it possible to query your data in an advanced way, by using GraphQL. Querying data, using facets and search phrases, is very...

Jonas Bergqvist | Jul 5, 2024