From e8fff83307601b8ca61a5685b413de0824dc2721 Mon Sep 17 00:00:00 2001 From: Carlos Zamora Date: Wed, 28 Apr 2021 10:59:05 -0700 Subject: [PATCH] Serialize stub for dynamic profiles (#9964) #9962 was caused by a serialization bug. _Technically_, `ToJson` works as intended: if the current layer has any values set, write them out to the json. However, on first load, the dynamic profile `Profile` objects are actually empty (because they inherit from base layer, then the dynamic profile generator). This means that `ToJson` writes the dynamic profiles as empty objects `{}`. Then, on reload, we see that the dynamic profiles aren't in the JSON, and we write them again. To get around this issue, we added a simple check to `Profile::ToJson`: if we have a source, make sure we write out the name, guid, hidden, and source. This is intended to align with `Profile::GenerateStub`. Closes #9962 (cherry picked from commit 8f93f76214fe5bfedea1e53db7f9fa6fa18c378c) --- src/cascadia/TerminalSettingsModel/Profile.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/cascadia/TerminalSettingsModel/Profile.cpp b/src/cascadia/TerminalSettingsModel/Profile.cpp index a539399bd7c..09309931c9f 100644 --- a/src/cascadia/TerminalSettingsModel/Profile.cpp +++ b/src/cascadia/TerminalSettingsModel/Profile.cpp @@ -485,11 +485,16 @@ Json::Value Profile::ToJson() const { Json::Value json{ Json::ValueType::objectValue }; + // GH #9962: + // If the settings.json was missing, when we load the dynamic profiles, they are completely empty. + // This caused us to serialize empty profiles "{}" on accident. + const bool writeBasicSettings{ !Source().empty() }; + // Profile-specific Settings - JsonUtils::SetValueForKey(json, NameKey, _Name); - JsonUtils::SetValueForKey(json, GuidKey, _Guid); - JsonUtils::SetValueForKey(json, HiddenKey, _Hidden); - JsonUtils::SetValueForKey(json, SourceKey, _Source); + JsonUtils::SetValueForKey(json, NameKey, writeBasicSettings ? Name() : _Name); + JsonUtils::SetValueForKey(json, GuidKey, writeBasicSettings ? Guid() : _Guid); + JsonUtils::SetValueForKey(json, HiddenKey, writeBasicSettings ? Hidden() : _Hidden); + JsonUtils::SetValueForKey(json, SourceKey, writeBasicSettings ? Source() : _Source); // Core Settings JsonUtils::SetValueForKey(json, ForegroundKey, _Foreground);