Facing issue in schema type conversion during upgrade

We are changing the leaf type from “string” to union of “enumeration” and “string”. We are configuring the value as “-1” in the old schema and when this schema cdb is converted (through upgrade) into new schema, the value is reading as enum type and value as -1. But when we configure the old schema value as string “1” and when this is converted into new schema, the value is being read as type C_BUF and the value as “1”. Why the behaviour is inconsistent when the value is “-1” and “1”?

We are expecting the value string “1” also should be read as enum type during conversion into new schema.

Old Schema leaf:

leaf expiry-date {
  type string;
  default "-1";
}

New Schema Leaf:

typedef user-acc-status-config {
    type enumeration {
        enum enabled {
            value -1;
        }
        enum locked {
            value 1;
        }
    }
}

leaf expiry-date {
    type union {
        type user-acc-status-config;
        type string;
    }
    default enabled;
}

Did you set the expiry-date value to “-1”? It looks like you left it unset and had it use the default value “-1”.
When upgrading, there is no expiry-date configuration data to upgrade, and the expiry-data default will be “enabled”.
You can try setting the expiry-date value to “-1” before upgrading. It will then remain a “-1” string after the upgrade.

From the ConfD UG chapter “Automatic schema upgrades and downgrades”

Default values
If a leaf has a default value, which has not been changed from its default, then the automatic upgrade will use the new default value (if any). If the leaf value has been changed from the old default, then that value will be kept.

In addition, if you set the value of the expiry-data leaf, the “-1” or “1” strings will not match the “enabled” or “locked” enum values and will remain strings after the upgrade. Had you set the expiry-date value to “enabled” or “locked” before upgrading, the value would have matched the enum value.

After setting the value as ‘-1’, it’s being read as type string and value as -1 as said.

But with the same schema, if I try to downgrade from new schema with the expiry-date value set as ‘enabled’ which is of type enum and value as -1 , after downgrade observing the value for this leaf is being read as type string with value as enabled instead of enum value -1 as string.

Why isn’t the value for the enum being converted to string?
Is there a way to read the value of this leaf as -1 instead of enabled as string after downgrade?

Why isn’t the value for the enum being converted to string?

From RFC 7950: The YANG 1.1 Data Modeling Language

The enumeration built-in type represents values from a set of assigned names.

The assigned name is converted to a string when you change the YANG type from enum to string. Vice versa, if you change the type from string to enum, the string is converted to a name if it matches one of the enum assigned names.
See the ConfD UG chapter “Automatic schema upgrades and downgrades”

Is there a way to read the value of this leaf as -1 instead of enabled as string after downgrade?

You would need an helper application that does the conversion from the enabled enum name to a “-1” string.
See the ConfD UG chapter “Using MAAPI to modify CDB during upgrade” and the examples.confd/cdb_upgrade/using_maapi + examples.confd/in_service_upgrade/with_helper examples from the ConfD example set.