Skip to content

Commit

Permalink
Fix decoding of search attributes in `Workflow::getInfo()->searchAttr…
Browse files Browse the repository at this point in the history
…ibutes`
  • Loading branch information
roxblnfk committed Oct 7, 2024
1 parent 8bc30d2 commit 301c22e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Internal/Marshaller/MarshallerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface MarshallerInterface
public function marshal(object $from): mixed;

/**
* @template T of object
* @template T
* @param array $from
* @param T $to
* @return T
Expand Down
29 changes: 27 additions & 2 deletions src/Internal/Transport/Router/StartWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Temporal\Internal\Transport\Router;

use React\Promise\Deferred;
use Temporal\Api\Common\V1\SearchAttributes;
use Temporal\DataConverter\EncodedCollection;
use Temporal\DataConverter\EncodedValues;
use Temporal\Interceptor\WorkflowInbound\WorkflowInput;
use Temporal\Interceptor\WorkflowInboundCallsInterceptor;
Expand Down Expand Up @@ -53,8 +55,13 @@ public function handle(ServerRequestInterface $request, array $headers, Deferred
$payloads = EncodedValues::sliceValues($this->services->dataConverter, $payloads, 0, $offset);
}

// Search Attributes
$searchAttributes = $this->convertSearchAttributes($options['info']['SearchAttributes'] ?? null);
$options['info']['SearchAttributes'] = $searchAttributes?->getValues();

/** @var Input $input */
$input = $this->services->marshaller->unmarshal($options, new Input());

/** @psalm-suppress InaccessibleProperty */
$input->input = $payloads;
/** @psalm-suppress InaccessibleProperty */
Expand Down Expand Up @@ -105,8 +112,8 @@ public function handle(ServerRequestInterface $request, array $headers, Deferred
/** @see WorkflowInboundCallsInterceptor::execute() */
'execute',
)(
new WorkflowInput($context->getInfo(), $context->getInput(), $context->getHeader()),
);
new WorkflowInput($context->getInfo(), $context->getInput(), $context->getHeader()),
);
}

private function findWorkflowOrFail(WorkflowInfo $info): WorkflowPrototype
Expand All @@ -115,4 +122,22 @@ private function findWorkflowOrFail(WorkflowInfo $info): WorkflowPrototype
\sprintf(self::ERROR_NOT_FOUND, $info->type->name),
);
}

private function convertSearchAttributes(?array $param): ?EncodedCollection
{
if (!\is_array($param)) {
return null;
}

$sa = (new SearchAttributes());
$sa->mergeFromJsonString(
\json_encode($param),
true,
);

return EncodedCollection::fromPayloadCollection(
$sa->getIndexedFields(),
$this->services->dataConverter,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Temporal\Tests\Acceptance\Extra\Workflow\ChildWorkflowSearchAttributes;

use PHPUnit\Framework\Attributes\CoversFunction;
use PHPUnit\Framework\Attributes\Test;
use Temporal\Client\WorkflowStubInterface;
use Temporal\Tests\Acceptance\App\Attribute\Stub;
use Temporal\Tests\Acceptance\App\TestCase;
use Temporal\Workflow;
use Temporal\Workflow\WorkflowInterface;
use Temporal\Workflow\WorkflowMethod;

#[CoversFunction('Temporal\Internal\Workflow\Process\Process::logRunningHandlers')]
class ChildWorkflowSearchAttributesTest extends TestCase
{
#[Test]
public function updateHandlersWithOneCall(
#[Stub(
'Extra_Workflow_ChildWorkflowSearchAttributes',
args: [
['foo' => 'bar'],
],
)]
WorkflowStubInterface $stub,
): void {
$result = $stub->getResult('array', timeout: 3);
$this->assertSame(['foo' => 'bar'], $result, 'Workflow result contains resolved value');
}
}

#[WorkflowInterface]
class TestWorkflow
{
#[WorkflowMethod(name: "Extra_Workflow_ChildWorkflowSearchAttributes")]
public function handle(array $searchAttributes): \Generator
{
return yield Workflow::newChildWorkflowStub(
TestWorkflowChild::class,
Workflow\ChildWorkflowOptions::new()
->withSearchAttributes($searchAttributes)
->withTaskQueue(Workflow::getInfo()->taskQueue)
)->handle();
}
}

#[WorkflowInterface]
class TestWorkflowChild
{
#[WorkflowMethod(name: "Extra_Workflow_ChildWorkflowSearchAttributes_Child")]
public function handle(): array
{
return Workflow::getInfo()->searchAttributes;
}
}

0 comments on commit 301c22e

Please sign in to comment.