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

Fixes #97 for outputting JSON metadata properties on child objects #122

Merged
merged 3 commits into from
Apr 18, 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Cosmos.DataTransfer.Interfaces;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Cosmos.DataTransfer.CosmosExtension
Expand Down Expand Up @@ -31,11 +32,13 @@ public IEnumerable<string> GetFieldNames()
{
if (value is JObject element)
{
return new CosmosDictionaryDataItem(element.ToObject<IDictionary<string, object?>>().ToDictionary(k => k.Key, v => v.Value));
return new CosmosDictionaryDataItem(element.ToObject<IDictionary<string, object?>>(JsonSerializer.Create(RawJsonCosmosSerializer.GetDefaultSettings()))
.ToDictionary(k => k.Key, v => v.Value));
}
if (value is JArray array)
{
return array.ToObject<List<object?>>().Select(GetChildObject).ToList();
return array.ToObject<List<object?>>(JsonSerializer.Create(RawJsonCosmosSerializer.GetDefaultSettings()))
.Select(GetChildObject).ToList();
}

return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ public static CosmosClient CreateClient(CosmosSettingsBase settings, string disp
{
string userAgentString = CreateUserAgentString(displayName, sourceDisplayName);

var cosmosSerializer = new RawJsonCosmosSerializer();
if (settings is CosmosSinkSettings sinkSettings)
{
cosmosSerializer.SerializerSettings.NullValueHandling = sinkSettings.IgnoreNullValues
? Newtonsoft.Json.NullValueHandling.Ignore
: Newtonsoft.Json.NullValueHandling.Include;
}

var clientOptions = new CosmosClientOptions
{
ConnectionMode = settings.ConnectionMode,
ApplicationName = userAgentString,
AllowBulkExecution = true,
EnableContentResponseOnWrite = false,
Serializer = cosmosSerializer,
};

if (settings is CosmosSinkSettings sinkSettings)
{
clientOptions.SerializerOptions = new CosmosSerializationOptions
{
IgnoreNullValues = sinkSettings.IgnoreNullValues
};
}

CosmosClient? cosmosClient;
if (settings.UseRbacAuth)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Microsoft.Azure.Cosmos;
using Newtonsoft.Json;
using System.Text;

namespace Cosmos.DataTransfer.CosmosExtension;

/// <summary>
/// Serializer for Cosmos allowing access to internal JsonSerializer settings.
/// </summary>
/// <remarks>
/// Defaults to disabling metadata handling to allow passthrough of recognized properties like "$type".
/// </remarks>
public class RawJsonCosmosSerializer : CosmosSerializer
{
private static readonly Encoding DefaultEncoding = new UTF8Encoding(false, true);

public static JsonSerializerSettings GetDefaultSettings() =>
new()
{
DateParseHandling = DateParseHandling.None,
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
ContractResolver = null,
MaxDepth = 64,
};

public JsonSerializerSettings SerializerSettings { get; } = GetDefaultSettings();

public override T FromStream<T>(Stream stream)
{
using (stream)
{
if (typeof(Stream).IsAssignableFrom(typeof(T)))
{
return (T)(object)stream;
}

using var streamReader = new StreamReader(stream);
using var jsonReader = new JsonTextReader(streamReader);
var serializer = JsonSerializer.Create(SerializerSettings);
return serializer.Deserialize<T>(jsonReader);
}
}

public override Stream ToStream<T>(T input)
{
var memoryStream = new MemoryStream();
using (var streamWriter = new StreamWriter(memoryStream, DefaultEncoding, bufferSize: 1024, leaveOpen: true))
{
using (var jsonWriter = new JsonTextWriter(streamWriter))
{
jsonWriter.Formatting = Formatting.None;
var serializer = JsonSerializer.Create(SerializerSettings);
serializer.Serialize(jsonWriter, input);
jsonWriter.Flush();
streamWriter.Flush();
}
}

memoryStream.Position = 0;
return memoryStream;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,56 @@ public async Task ReadAsync_WithFlatObjects_ReadsValuesFromUrl()
}
}

[TestMethod]
public async Task ReadAsync_WithTypeHintFields_IncludesAllInOutput()
{
var json = @"[
{
""id"": 1,
""name"": ""One"",
""$type"": ""Number"",
""data"": {
""$type"": ""Object"",
""name"": ""A""
}
},
{
""id"": 2,
""name"": ""Two"",
""$type"": ""Digit"",
""data"": {
""$type"": ""String"",
""name"": ""B""
}
}
]";
var filePath = Path.Combine(Path.GetTempPath(), "TypeHintFields.json");

await File.WriteAllTextAsync(filePath, json);

var extension = new JsonFileSource();
var config = TestHelpers.CreateConfig(new Dictionary<string, string>
{
{ "FilePath", filePath }
});

int counter = 0;
await foreach (var dataItem in extension.ReadAsync(config, NullLogger.Instance))
{
counter++;
var fields = dataItem.GetFieldNames().ToArray();
CollectionAssert.AreEquivalent(new[] { "id", "name", "$type", "data" }, fields);
Assert.IsNotNull(dataItem.GetValue("id"));
Assert.IsNotNull(dataItem.GetValue("name"));
Assert.IsNotNull(dataItem.GetValue("$type"));
var child = dataItem.GetValue("data") as JsonDictionaryDataItem;
Assert.IsNotNull(child);
CollectionAssert.AreEquivalent(new[] { "$type", "name" }, child.GetFieldNames().ToArray());
Assert.IsNotNull(child.GetValue("$type"));
Assert.IsNotNull(child.GetValue("name"));
}

Assert.AreEqual(2, counter);
}
}
}
Loading