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 @@ -18,6 +18,8 @@

### Bug Fixes

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

### 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());
}
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;
}
}
Loading
Loading