Try our conversational search powered by Generative AI!

Giang Nguyen
Nov 7, 2019
  3624
(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 Data Platform Visitor Groups now supports multiple instances

The module V2.0 now supports multiple Optimizely Data Platform instances, allowing personalized content based on real-time segments and profile dat...

Andrew Markham | May 15, 2024 | Syndicated blog

IP block for edit and admin in DXP

Why IP-blocking edit/admin? A hacker can try to guess user names and passwords to gain access to your site. This risk can be minimized in a couple ...

Daniel Ovaska | May 15, 2024

Do not upgrade to EPiServer.CMS.Core 12.21.4 without reading this!

Todays update of EPiServer.CMS.Core 12.21.4 alters default sort order in an unexpected way, when you are working with muligple languages and have...

Tomas Hensrud Gulla | May 14, 2024 | Syndicated blog

Upgrade Optimizely CMS From v11 to v12

Why Upgrade? There are many improvements implemented in version 12, but most importantly is that the whole framework is migrated from .Net Framewor...

MilosR | May 13, 2024

Configured Commerce - Infrastructure Updates Ahoy!

I'm very happy to share an important milestone - we no longer have any customers in our legacy v1 environment!  This means that the Configured...

John McCarroll | May 10, 2024

A day in the life of an Optimizely Developer - Enabling Opti ID within your application

Hello and welcome to another instalment of A Day In The Life Of An Optimizely developer, in this blog post I will provide details on Optimizely's...

Graham Carr | May 9, 2024