Skip to content
Open
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
2 changes: 2 additions & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

### Bug Fixes

- Support Polymorphic input payload deserialization (https://github.com/Azure/azure-functions-durable-extension/pull/3250)

Comment on lines +23 to +24
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

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

The PR description checklist indicates that "My changes should not be added to the release notes for the next release" is checked, suggesting this change should not appear in the release notes. However, a release note entry has been added. Either the release note should be removed, or the PR description checklist should be updated to reflect that release notes are needed.

Suggested change
- Support Polymorphic input payload deserialization (https://github.com/Azure/azure-functions-durable-extension/pull/3250)

Copilot uses AI. Check for mistakes.
### Breaking Changes

### Dependency Updates
Expand Down
66 changes: 65 additions & 1 deletion src/Worker.Extensions.DurableTask/ObjectConverterShim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.Json.Serialization;
using Azure.Core.Serialization;
using Microsoft.DurableTask;

Expand Down Expand Up @@ -39,7 +41,69 @@ public ObjectConverterShim(ObjectSerializer serializer)
return null;
}

BinaryData data = this.serializer.Serialize(value, value.GetType(), default);
Type valueType = value.GetType();

// Special handling for object[] arrays - DTFx wraps activity inputs in object[]
// When System.Text.Json serializes object[], it treats elements as type 'object' (the static array element type),
// not as their runtime concrete type, which prevents polymorphic type discriminators from being added.
// We must serialize each element individually with its polymorphic base type and build the JSON array manually.
if (valueType == typeof(object[]))
{
object[] array = (object[])value;
var jsonBuilder = new StringBuilder();
jsonBuilder.Append('[');

for (int i = 0; i < array.Length; i++)
{
if (i > 0)
{
jsonBuilder.Append(',');
}

object? element = array[i];
if (element != null)
{
// Serialize each element with its polymorphic base type to include type discriminators
Type elementType = element.GetType();
Type serializeAs = GetPolymorphicBaseType(elementType) ?? elementType;

BinaryData elementData = this.serializer.Serialize(element, serializeAs, default);
jsonBuilder.Append(elementData.ToString());
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

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

Redundant call to 'ToString' on a String object.

Suggested change
jsonBuilder.Append(elementData.ToString());
jsonBuilder.Append(elementData);

Copilot uses AI. Check for mistakes.
}
else
{
jsonBuilder.Append("null");
}
}

jsonBuilder.Append(']');
return jsonBuilder.ToString();
}

// For non-array values (or non-object[] arrays), serialize as polymorphic base type
Type typeToSerialize = GetPolymorphicBaseType(valueType) ?? valueType;

BinaryData data = this.serializer.Serialize(value, typeToSerialize, default);
return data.ToString();
}

/// <summary>
/// Finds the polymorphic base type for a given type by walking up the inheritance chain.
/// Returns the first base type decorated with <see cref="JsonPolymorphicAttribute"/>.
/// </summary>
/// <param name="concreteType">The concrete type to check.</param>
/// <returns>The polymorphic base type if found, otherwise null.</returns>
private static Type? GetPolymorphicBaseType(Type concreteType)
{
Type? current = concreteType.BaseType;
while (current != null && current != typeof(object))
{
if (current.GetCustomAttribute<JsonPolymorphicAttribute>() != null)
{
return current;
}
current = current.BaseType;
}
return null;
}
Comment on lines +96 to +108
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

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

The GetPolymorphicBaseType method only searches for JsonPolymorphicAttribute on base classes using the inheritance chain (BaseType property), but does not check implemented interfaces. According to issue #3182, users need to support polymorphic deserialization for interface-typed activity inputs (e.g., IVehicle). Consider also checking the type's implemented interfaces using GetInterfaces() to find interfaces decorated with JsonPolymorphicAttribute.

Copilot uses AI. Check for mistakes.
Comment on lines +96 to +108
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

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

The implementation only supports System.Text.Json's JsonPolymorphicAttribute, but the original issue #3182 specifically mentions using Newtonsoft.Json with TypeNameHandling.All. The current implementation will not detect polymorphic types when using Newtonsoft.Json, as it won't find the attribute. Consider either documenting that this feature only works with System.Text.Json, or extend the implementation to also support Newtonsoft.Json's type handling mechanisms (e.g., checking for $type property in the serialized JSON).

Copilot uses AI. Check for mistakes.
}
Loading
Loading