Skip to content

Commit 6b80c30

Browse files
Fix duplicate derived collection items when modifying source collection (#116)
A derived collection is dependent on a collection field, e.g. `trustsSearch-field-flow`. When a user adds a collection item, the `sourceJson` for the derived collection contains a value with that key. When a user modifies the collection item, an additional key is added to the `sourceJson` (in the case of the example above, `Data[trustsSearch-field-flow]`). This causes the item to be duplicated in the returned list, so keys of this format need to be ignored.
1 parent df9a3a5 commit 6b80c30

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/DfE.ExternalApplications.Infrastructure/Services/DerivedCollectionFlowService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ private List<DerivedCollectionItem> ProcessCollectionSource(string sourceJson, D
211211
foreach (var kvp in item)
212212
{
213213
// Skip metadata fields
214-
if (kvp.Key == "id" || kvp.Key == "_metadata") continue;
214+
if (kvp.Key == "id" || kvp.Key == "_metadata" || kvp.Key.StartsWith("Data[") || kvp.Key.StartsWith("Data_")) continue;
215215

216216
// Try to extract autocomplete data from the field value
217217
if (kvp.Value != null)

src/Tests/DfE.ExternalApplications.Infrastructure.UnitTests/Services/DerivedCollectionFlowServiceTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,34 @@ public void GenerateItemsFromSourceField_for_collection_correctly_html_escapes_s
6262
Assert.Equal("some foo", item.DisplayName);
6363
Assert.Equal("Not signed yet", item.Status);
6464
}
65+
66+
[Theory]
67+
[InlineData("[{\"foo\":\"{&quot;id&quot;:&quot;123456&quot;,&quot;name&quot;:&quot;some foo&quot;}\",\"bar\":123,\"Data[foo]\":\"{\\\"id\\\":\\\"456789\\\",\\\"name\\\":\\\"another foo\\\"}\"}]")]
68+
[InlineData("[{\"foo\":\"{&quot;id&quot;:&quot;123456&quot;,&quot;name&quot;:&quot;some foo&quot;}\",\"bar\":123,\"Data_foo\":\"{\\\"id\\\":\\\"456789\\\",\\\"name\\\":\\\"another foo\\\"}\"}]")]
69+
public void GenerateItemsFromSourceField_does_not_return_duplicate_entries_when_source_collection_is_modified(string sourceJson)
70+
{
71+
// A derived collection is dependent on a collection field, e.g. `trustsSearch-field-flow`.
72+
// When a user adds a collection item, the `sourceJson` for the derived collection contains a value with that
73+
// key.
74+
// When a user modifies the collection item, an additional key is added to the `sourceJson` (in the case of the
75+
// example above, `Data[trustsSearch-field-flow]` or `Data_trustsSearch-field-flow`).
76+
// This causes the item to be duplicated in the returned list, so keys of these formats need to be ignored.
77+
78+
var fieldId = _fixture.Create<string>();
79+
var formData = new Dictionary<string, object>
80+
{
81+
[fieldId] = sourceJson,
82+
};
83+
var config = _fixture.Build<DerivedCollectionFlowConfiguration>()
84+
.With(c => c.SourceType, "collection")
85+
.Create();
86+
87+
var result = _service.GenerateItemsFromSourceField(fieldId, formData, config);
88+
89+
Assert.NotEmpty(result);
90+
var item = Assert.Single(result);
91+
Assert.Equal("123456", item.Id);
92+
Assert.Equal("some foo", item.DisplayName);
93+
Assert.Equal("Not signed yet", item.Status);
94+
}
6595
}

0 commit comments

Comments
 (0)