objects in array/list are cast back to base class on editor activity

so i have a Scriptable object containing different kind of nodes in a list, like so:

using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/ExampleSO", order = 1)]
public class ExampleSO : ScriptableObject
{
    public List<Node> nodes;

    [System.Serializable]
    public class Node
    {
        public int id;
    }

    [System.Serializable]
    public class ValueNode : Node
    {
        public float value;
    }
}

and a custom editor like so:

using System;
using System.Linq;
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEditorInternal;
using System.Collections.Generic;


public class ExampleEditorWindow : EditorWindow
{
    private ExampleSO mExampleSO = null;
    
    
    [MenuItem("Window/ExampleSOEditor")]
    public static ExampleEditorWindow OpenWindow()
    {
        return EditorWindow.GetWindow<ExampleEditorWindow>("ExampleSO Editor");
    }
    
    [UnityEditor.Callbacks.OnOpenAsset(1)]
    public static bool OnOpenDatabase(int instanceID, int line)
    {
        ExampleSO generator = EditorUtility.InstanceIDToObject(instanceID) as ExampleSO;
        if(generator != null)
        {
            ExampleEditorWindow generatorWindow = OpenWindow();
            generatorWindow.mExampleSO = generator;
            return true;
        }
        return false;
    }

    private void OnGUI()
    {
        foreach(var e in mExampleSO.nodes)
        {
            EditorGUILayout.LabelField((e is ExampleSO.ValueNode)? "value node" : "normal node");
        }
        if(GUILayout.Button("add value node"))
        {
            mExampleSO.nodes.Add(new ExampleSO.ValueNode());
        }
    }
    
}

now, if i create an exampleSO (Project->RMB->create->ScriptableObject->ExampleSO) and double click it, to use with the custom editor and i fill it with ValueNodes, they all show correctly as value nodes.
151173-exapmleso.jpg
but, somehow, when i edit the list in the unity inspector (remove array element or duplicate array element), or recompile, all nodes in the node list suddenly are only ‘normal nodes’.
151174-exapmleso2.jpg

how come this behaviour occurs and is there any way to avoid this, and maintain the derived class ValueNode in the List?

And once again…

Unity’s serialization system does not support polymorphism for custom serializable classes. Those classes behave like structs. No type information is serialized. The type of such a field is always the field type. That might sound strange but it’s just as it is. You may want to have a look at how the serialized data actually looks like (just open your scene / scriptable object asset in a text editor). In the YAML format you will notice that only the data is serialized and no type information is stored.

If you need polymorphism you have to use ScriptableObjects or MonoBehaviour instances. These do not behave like structs. They are not serialized inline like custom serializable structs. Fields of such types are actually serialized as a “serialized reference” to another asset.

Finally do not confuse casting with conversion. A cast can always be reverted. A conversion can not. When your object tree is serialized the serializer will serialize those custom classes based on the field type. The field type in your case is a List of "Node"s. Therefore all instances in your list will be treated like Nodes. Of course when you deserialize them you get a List with just Node instances.