Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow duplicate evidence value keys #139

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions Dan.Core/Extensions/HttpExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Specialized;
using System.Net;
using Newtonsoft.Json;
using System.Net.Http.Headers;
Expand All @@ -10,6 +9,7 @@
using Dan.Common.Models;
using Dan.Core.Exceptions;
using Dan.Core.Helpers;
using Dan.Core.Helpers.JsonConverters;
using Dan.Core.Middleware;
using Microsoft.Azure.Functions.Worker.Http;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -266,8 +266,12 @@ public static async Task SetUnenvelopedEvidenceValuesAsync(this HttpResponseData
string jsonResult;
if (evidenceValues.Count > 1)
{
var asHashTable = ConvertToHashtable(evidenceValues);
jsonResult = JsonConvert.SerializeObject(asHashTable, new JsonSerializerSettings { ContractResolver = new HiddenPropertyContractResolver() });
var kvpList = ConvertToKvpList(evidenceValues);
jsonResult = JsonConvert.SerializeObject(kvpList, new JsonSerializerSettings
{
ContractResolver = new HiddenPropertyContractResolver(),
Converters = [new KeyValueListAsObjectConverter<object?>()]
});
JmesPathTransfomer.Apply(jmesExpression, ref jsonResult);
await response.WriteStringAsync(jsonResult);
response.Headers.Add("Content-Type", "application/json");
Expand Down Expand Up @@ -316,12 +320,10 @@ public static async Task SetEvidenceAsync(this HttpResponseData response, Eviden
response.Headers.Add("X-Signature-JWT", Jwt.GetDigestJwt(jsonResult));
}

private static Hashtable ConvertToHashtable(List<EvidenceValue> evidenceValues)
private static List<KeyValuePair<string, object?>> ConvertToKvpList(List<EvidenceValue> evidenceValues)
{
var hashTable = new Hashtable();
foreach (var evidenceValue in evidenceValues)
hashTable.Add(evidenceValue.EvidenceValueName, evidenceValue.Value);

return hashTable;
return evidenceValues
.Select(evidenceValue => new KeyValuePair<string, object?>(evidenceValue.EvidenceValueName, evidenceValue.Value))
.ToList();
}
}
29 changes: 29 additions & 0 deletions Dan.Core/Helpers/JsonConverters/KeyValueListAsObjectConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Newtonsoft.Json;

namespace Dan.Core.Helpers.JsonConverters;

// Solution found here: https://stackoverflow.com/questions/68069271/how-to-serialize-and-deserialize-a-json-object-with-duplicate-property-names-in
public class KeyValueListAsObjectConverter<TValue> : JsonConverter<List<KeyValuePair<string, TValue>>>
{
public override List<KeyValuePair<string, TValue>> ReadJson(
JsonReader reader,
Type objectType,
List<KeyValuePair<string, TValue>>? existingValue,
bool hasExistingValue,
JsonSerializer serializer)
{
// We don't need to read this format
throw new NotImplementedException();
}

public override void WriteJson(JsonWriter writer, List<KeyValuePair<string, TValue>>? value, JsonSerializer serializer)
{
writer.WriteStartObject();
foreach (var pair in value ?? [])
{
writer.WritePropertyName(pair.Key);
serializer.Serialize(writer, pair.Value);
}
writer.WriteEndObject();
}
}
Loading