Fix PolymorphicObjectConverter handling of primitive values#7207
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a serialization crash in PolymorphicObjectConverter when handling types with custom converters that serialize to primitive values (e.g., numbers, booleans). Previously, the converter assumed all serialized values would be objects or arrays and attempted to enumerate them, causing an InvalidOperationException when encountering primitives. The fix adds a defensive check to detect and handle primitive JsonElement values by writing them directly to the output.
Changes:
- Added a check in
PolymorphicObjectConverter.Writeto detect non-object, non-array JSON values and write them directly instead of attempting to enumerate properties - Resolves issue #7178 where custom converters that serialize to primitive values would crash during serialization
src/modules/Elsa.Workflows.Core/Serialization/Converters/PolymorphicObjectConverter.cs
Show resolved
Hide resolved
|
Thanks for the review. I’ve added an integration test in Serialization/Polymorphism/Tests.cs that serializes a type with a custom converter producing a primitive value and verifies it succeeds and outputs the expected primitive JSON. This directly covers the scenario described in #7178. cc @sfmskywalker |
|
@sfmskywalker Hey, can you please let me know if any other changes are required or we can check this in |
|
@VedikaGupt Looks good to me - thanks for the fix! |
Fixes #7178
This PR fixes a serialization failure in PolymorphicObjectConverter.Write when a value is serialized as a primitive by another converter.
Previously, the converter assumed the serialized JsonElement would always be an object or array and attempted to enumerate it as such. When the value was a primitive (e.g. number), this resulted in a runtime exception during serialization.
The fix adds a defensive check to detect non-object and non-array JsonElement values and writes them directly to the output instead of assuming an object structure.