Skip to content
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
Expand Up @@ -3,30 +3,32 @@
using System.Globalization;
using System.Reflection;
using System.Runtime.Serialization;
using Elasticsearch.Net.Extensions;
using Elasticsearch.Net.Utf8Json;

namespace Elasticsearch.Net
{
internal class ExceptionFormatterResolver : IJsonFormatterResolver
{
public static ExceptionFormatterResolver Instance = new ExceptionFormatterResolver();
public static readonly ExceptionFormatterResolver Instance = new ExceptionFormatterResolver();

private ExceptionFormatterResolver() { }

private static readonly ExceptionFormatter ExceptionFormatter = new ExceptionFormatter();

public IJsonFormatter<T> GetFormatter<T>()
{
if (typeof(Exception).IsAssignableFrom(typeof(T)))
return (IJsonFormatter<T>)ExceptionFormatter;
{
var type = typeof(ExceptionFormatter<>).MakeGenericType(typeof(T));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.CreateGenericInstance()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CreateGenericInstance exists in Nest, but ExceptionFormatter is in Elasticsearch.Net. Can move the extension method to Elasticsearch.Net?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can, we can also follow up in a new issue.

return (IJsonFormatter<T>)type.CreateInstance();
}

return null;
}
}

internal class ExceptionFormatter : IJsonFormatter<Exception>
internal class ExceptionFormatter<TException> : IJsonFormatter<TException> where TException : Exception
{
private List<Dictionary<string, object>> FlattenExceptions(Exception e)
private static List<Dictionary<string, object>> FlattenExceptions(Exception e)
{
var maxExceptions = 20;
var exceptions = new List<Dictionary<string, object>>(maxExceptions);
Expand Down Expand Up @@ -100,7 +102,7 @@ private static void WriteStructuredExceptionMethod(Dictionary<string,object> o,
o.Add("ExceptionMethod", exceptionMethod);
}

public void Serialize(ref JsonWriter writer, Exception value, IJsonFormatterResolver formatterResolver)
public void Serialize(ref JsonWriter writer, TException value, IJsonFormatterResolver formatterResolver)
{
if (value == null)
{
Expand Down Expand Up @@ -134,7 +136,7 @@ public void Serialize(ref JsonWriter writer, Exception value, IJsonFormatterReso
writer.WriteEndArray();
}

public Exception Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) =>
public TException Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) =>
throw new NotSupportedException();
}
}
28 changes: 28 additions & 0 deletions tests/Tests.Reproduce/GitHubIssue4294.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.IO;
using Elastic.Xunit.XunitPlumbing;
using Elasticsearch.Net;
using FluentAssertions;

namespace Tests.Reproduce
{
public class GitHubIssue4294
{
[U] public void CanSerializeNullReferenceException()
{
var settings = new ConnectionConfiguration();

var dic = new Dictionary<string, object>()
{
["Exception"] = new NullReferenceException(),
};

var data = PostData.Serializable(dic);
using var ms = new MemoryStream();
Action write = () => data.Write(ms, settings);

write.Should().NotThrow();
}
}
}