Skip to content

Commit fecd2e7

Browse files
authored
Handle DockSize enum changes (#47212)
### ⚠️ REVIEWERS TO DECIDE. MERGE THIS PR OR GO FOR: #47214⚠️ ### Root cause PR #46699 (compact mode) replaced the original `DockSize` enum (`Small`/`Medium`/`Large`) with a new one (Default/Compact). Existing users have `DockSize`: `Small` (or `Medium`/`Large`) in their `settings.json`, which the source-generated EnumConverter can't parse → the entire settings file fails to load. ### Fix Added a custom `JsonConverter<DockSize>` in _src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Settings/DockSettings.cs_ and applied it via `[JsonConverter]` on the enum. The converter: - Accepts current values (`Default`, `Compact`) — case-insensitive. - Maps any unknown legacy string (e.g. `Small`/`Medium`/`Large`) to `DockSize.Default`, preserving the user's existing visual experience. - Tolerates numeric values too, falling back to `Default` if out of range. - Writes as a string so output stays consistent with the source-gen `UseStringEnumConverter` setting.
1 parent fde1599 commit fecd2e7

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Settings/DockSettings.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Collections.Immutable;
6+
using System.Text.Json;
67
using System.Text.Json.Serialization;
78
using Windows.UI;
89

@@ -135,12 +136,52 @@ public enum DockSide
135136
Bottom = 3,
136137
}
137138

139+
[JsonConverter(typeof(DockSizeJsonConverter))]
138140
public enum DockSize
139141
{
140142
Default,
141143
Compact,
142144
}
143145

146+
/// <summary>
147+
/// Custom converter for <see cref="DockSize"/> that preserves backward
148+
/// compatibility with previously-persisted values. Earlier builds shipped a
149+
/// <c>Small</c>/<c>Medium</c>/<c>Large</c> enum; those values are migrated to
150+
/// <see cref="DockSize.Default"/> so existing settings.json files continue to
151+
/// load instead of failing the entire deserialization.
152+
/// </summary>
153+
internal sealed class DockSizeJsonConverter : JsonConverter<DockSize>
154+
{
155+
public override DockSize Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
156+
{
157+
if (reader.TokenType == JsonTokenType.String)
158+
{
159+
var value = reader.GetString();
160+
if (Enum.TryParse<DockSize>(value, ignoreCase: true, out var parsed))
161+
{
162+
return parsed;
163+
}
164+
165+
// Legacy values from the original Small/Medium/Large enum, or any
166+
// other unknown string — fall back to Default so the user's
167+
// settings file remains loadable after upgrading.
168+
return DockSize.Default;
169+
}
170+
171+
if (reader.TokenType == JsonTokenType.Number && reader.TryGetInt32(out var number))
172+
{
173+
return Enum.IsDefined(typeof(DockSize), number) ? (DockSize)number : DockSize.Default;
174+
}
175+
176+
return DockSize.Default;
177+
}
178+
179+
public override void Write(Utf8JsonWriter writer, DockSize value, JsonSerializerOptions options)
180+
{
181+
writer.WriteStringValue(value.ToString());
182+
}
183+
}
184+
144185
public enum DockBackdrop
145186
{
146187
Transparent,

0 commit comments

Comments
 (0)