Skip to content

Commit 6aa63fa

Browse files
myieyehahn-kev
andauthored
Handle setting RichMultiString keys to null (#1791)
* Handle setting RichMultiString keys to null * create a helper method to get a WsId from an object * Create WsId from key, not value --------- Co-authored-by: Kevin Hahn <kevin_hahn@sil.org>
1 parent e1ce751 commit 6aa63fa

4 files changed

Lines changed: 30 additions & 40 deletions

File tree

backend/FwLite/MiniLcm.Tests/RichMultiStringTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,16 @@ public void JsonPatchCanUpdateRichMultiStringWhenValueIsString()
223223
ms["en"].Should().BeEquivalentTo(new RichString("updated", "en"));
224224
}
225225

226+
[Fact]
227+
public void JsonPatchCanUpdateRichMultiStringWhenValueIsEmptyString()
228+
{
229+
var ms = new RichMultiString() { { "en", new RichString("test") } };
230+
var patch = new JsonPatchDocument<RichMultiString>();
231+
patch.Operations.Add(new Operation<RichMultiString>("replace", "/en", null, ""));
232+
patch.ApplyTo(ms);
233+
ms.Should().BeEmpty();
234+
}
235+
226236
[Fact]
227237
public void JsonPatchCanRemoveRichMultiString()
228238
{

backend/FwLite/MiniLcm/Models/MultiString.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,7 @@ void IDictionary.Add(object key, object? value)
7575
{
7676
var valStr = value as string ??
7777
throw new ArgumentException($"unable to convert value {value?.GetType().Name ?? "null"} to string", nameof(value));
78-
if (key is WritingSystemId keyWs)
79-
{
80-
Add(keyWs, valStr);
81-
}
82-
else if (key is string keyStr)
83-
{
84-
Add(keyStr, valStr);
85-
}
86-
else
87-
{
88-
throw new ArgumentException("unable to convert key to writing system id", nameof(key));
89-
}
78+
Add(WritingSystemId.FromUnknown(key), valStr);
9079
}
9180
}
9281

backend/FwLite/MiniLcm/Models/RichMultiString.cs

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,7 @@ void IDictionary.Add(object key, object? value)
4141
var valStr = value as RichString ??
4242
throw new ArgumentException($"unable to convert value {value?.GetType().Name ?? "null"} to RichString",
4343
nameof(value));
44-
if (key is WritingSystemId keyWs)
45-
{
46-
Add(keyWs, valStr);
47-
}
48-
else if (key is string keyStr)
49-
{
50-
Add(keyStr, valStr);
51-
}
52-
else
53-
{
54-
throw new ArgumentException("unable to convert key to writing system id", nameof(key));
55-
}
44+
Add(WritingSystemId.FromUnknown(key), valStr);
5645
}
5746

5847
public void Add(WritingSystemId key, RichString value)
@@ -152,28 +141,22 @@ void IDictionary.Remove(object key)
152141

153142
object? IDictionary.this[object key]
154143
{
155-
get =>
156-
key switch
157-
{
158-
WritingSystemId keyWs => this[keyWs],
159-
string keyStr => this[keyStr],
160-
_ => throw new ArgumentException("unable to convert key to writing system id", nameof(key))
161-
};
144+
get => this[WritingSystemId.FromUnknown(key)];
162145
set
163146
{
164-
var valStr = value as RichString ??
165-
throw new ArgumentException("unable to convert value to string", nameof(value));
166-
if (key is WritingSystemId keyWs)
167-
{
168-
this[keyWs] = valStr;
169-
}
170-
else if (key is string keyStr)
147+
// value will be null if an empty string was deserialized as a RichString (e.g. from a JsonPatch operation).
148+
// Usually that's what we want, because when deserializing a whole RichMultiString it will result in the key being dropped.
149+
// So we mimic that behaviour.
150+
var wsId = WritingSystemId.FromUnknown(key);
151+
if (value is null)
171152
{
172-
this[keyStr] = valStr;
153+
Remove(wsId);
173154
}
174155
else
175156
{
176-
throw new ArgumentException("unable to convert key to writing system id", nameof(key));
157+
var valStr = value as RichString ??
158+
throw new ArgumentException("unable to convert value to string", nameof(value));
159+
this[wsId] = valStr;
177160
}
178161
}
179162
}

backend/FwLite/MiniLcm/Models/WritingSystemId.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics.CodeAnalysis;
12
using System.Text.Json;
23
using System.Text.Json.Serialization;
34
using SIL.WritingSystems;
@@ -37,6 +38,13 @@ public override void WriteAsPropertyName(Utf8JsonWriter writer, WritingSystemId
3738

3839
public static readonly WritingSystemId Default = "default";
3940

41+
public static WritingSystemId FromUnknown(object? obj)
42+
{
43+
if (obj is WritingSystemId wsId) return wsId;
44+
if (obj is string str) return new WritingSystemId(str);
45+
throw new ArgumentException($"Unable to convert object: '{obj}' to writing system id", nameof(obj));
46+
}
47+
4048
public WritingSystemId(string code)
4149
{
4250
//__key is used by the LfClassicMiniLcmApi to smuggle non guid ids with possibilitie lists

0 commit comments

Comments
 (0)