Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -62,8 +62,17 @@ public T GetResult<T>(MessagePayloadDataConverter messageDataConverter, MessageP
// do a best-effort attempt at deserializing this exception
try
{
var type = Type.GetType(this.ExceptionType, true);
e = (Exception)errorDataConverter.Deserialize(this.Result, type);
Type type = Type.GetType(this.ExceptionType, true);
if (typeof(Exception).IsAssignableFrom(type))
{
JsonSerializer serializer = JsonSerializer.Create(errorDataConverter.JsonSettings);
serializer.TypeNameHandling = TypeNameHandling.None;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we document that we are deserializing using TypeNameHandling.None and serializing with TypeNameHandling.Objects?

using (var stringReader = new System.IO.StringReader(this.Result))
using (var jsonReader = new JsonTextReader(stringReader) { DateParseHandling = serializer.DateParseHandling })
{
e = (Exception)JToken.Load(jsonReader).ToObject(type, serializer);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we use serializer.Deserialize(jsonReader, type) here instead of JToken.Load().ToObject()?

}
}
}
catch
{
Expand Down
102 changes: 102 additions & 0 deletions test/FunctionsV2/Tests/Unit/ResponseMessageTests.cs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we already have a happy path test somewhere? Seems like the three new tests cover blocked types, blocked $type metadata, and DateParseHandling.

Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Xunit;

namespace Microsoft.Azure.WebJobs.Extensions.DurableTask.Tests
{
public class ResponseMessageTests
{
[Fact]
[Trait("Category", PlatformSpecificHelpers.TestCategory)]
public void GetResult_DoesNotDeserializeNonExceptionType()
{
var response = new ResponseMessage
{
ExceptionType = typeof(DeserializationCallback).AssemblyQualifiedName,
Result = "{}",
};

DeserializationCallback.WasCreated = false;
MessagePayloadDataConverter dataConverter = CreateDataConverter();

Assert.Throws<FunctionFailedException>(() => response.GetResult<object>(dataConverter, dataConverter));
Assert.False(DeserializationCallback.WasCreated);
}

[Fact]
[Trait("Category", PlatformSpecificHelpers.TestCategory)]
public void GetResult_IgnoresNestedTypeMetadataInException()
{
var result = new JObject
{
[nameof(ExceptionWithPayload.Payload)] = new JObject
{
["$type"] = typeof(DeserializationCallback).AssemblyQualifiedName,
},
};
var response = new ResponseMessage
{
ExceptionType = typeof(ExceptionWithPayload).AssemblyQualifiedName,
Result = result.ToString(Formatting.None),
};

DeserializationCallback.WasCreated = false;
MessagePayloadDataConverter dataConverter = CreateDataConverter();

Assert.Throws<ExceptionWithPayload>(() => response.GetResult<object>(dataConverter, dataConverter));
Assert.False(DeserializationCallback.WasCreated);
}

[Fact]
[Trait("Category", PlatformSpecificHelpers.TestCategory)]
public void GetResult_HonorsConfiguredDateParseHandling()
{
const string Timestamp = "2026-08-14T12:34:56Z";
var response = new ResponseMessage
{
ExceptionType = typeof(ExceptionWithPayload).AssemblyQualifiedName,
Result = new JObject
{
[nameof(ExceptionWithPayload.Payload)] = Timestamp,
}.ToString(Formatting.None),
};
MessagePayloadDataConverter dataConverter = CreateDataConverter(DateParseHandling.None);

ExceptionWithPayload exception = Assert.Throws<ExceptionWithPayload>(
() => response.GetResult<object>(dataConverter, dataConverter));

Assert.Equal(Timestamp, Assert.IsType<string>(exception.Payload));
}

private static MessagePayloadDataConverter CreateDataConverter(
DateParseHandling dateParseHandling = DateParseHandling.DateTime)
{
return new MessagePayloadDataConverter(
new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects,
DateParseHandling = dateParseHandling,
},
isDefault: true);
}

public class DeserializationCallback
{
public DeserializationCallback()
{
WasCreated = true;
}

public static bool WasCreated { get; set; }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

WasCreated is a shared static field so if xUnit parallelism is ever enabled for this class (e.g., via [assembly: CollectionBehavior(MaxParallelThreads = N)]), concurrent tests will silently corrupt this assertion. Consider using an instance counter with Interlocked.Increment instead, or at minimum add a comment noting that these tests must run single-threaded.

}

public class ExceptionWithPayload : Exception
{
public object Payload { get; set; }
}
}
}
Loading