sdk-php icon indicating copy to clipboard operation
sdk-php copied to clipboard

[Bug] Wrong activity response type

Open xepozz opened this issue 4 years ago • 4 comments

Describe the bug I send an array from my activity as a result, but in promise step then I get stdClass instead of array.

To Reproduce Steps to reproduce the behavior:

  1. Create activity that will send an associative array.
  2. Create workflow where you call activity and add post-processing in then step.
  3. Try to get value from result as if it array.

Expected behavior I get value

Expected behavior I get an error PanicError: Cannot use object of type stdClass as array (type: Error, retryable: true)

Screenshots/Terminal ouput image

You can use this snippet:

#[\Temporal\Activity\ActivityInterface(prefix: "app.")]
class CommonActivity
{
    public function slow(string $name): array
    {
        return [
            'result' => 'Hello, ' . $name,
        ];
    }
}
#[\Temporal\Workflow\WorkflowInterface]
final class FastWorkflow
{
    #[\Temporal\Workflow\WorkflowMethod("wf")]
    public function run(string $name, int $count): \Generator
    {
        $activity = Workflow::newActivityStub(
            CommonActivity::class
        );

        $activity->slow($name)->then(function ($result) {
            $result['result'];
        });

        return 0;
    }
}

As I understand the problem is locating here. But I don't know how to solve it.

xepozz avatar Jul 10 '21 19:07 xepozz

Hello! This is not a bug (More precisely, this is documented behavior). Please use DTO instead. Related issue https://github.com/temporalio/sdk-php/issues/93#issuecomment-804008399

SerafimArts avatar Jul 10 '21 20:07 SerafimArts

Then can you make behaviour more predictable: if you return ['key' => 'test'] it will be converted to an object, but if you return ['test'] it will be converted to an array.

xepozz avatar Jul 10 '21 20:07 xepozz

if you return ['key' => 'test'] it will be converted to an object, but if you return ['test'] it will be converted to an array.

Now that's how it works, isn't it?

SerafimArts avatar Jul 10 '21 20:07 SerafimArts

Yes, it is. But it isn't clear.

xepozz avatar Jul 10 '21 21:07 xepozz