Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class ScalarToEnumConverter implements Converter
*/
public function convert($source, Type $sourceType, MediaType $sourceMediaType, Type $targetType, MediaType $targetMediaType)
{
if ($targetType instanceof Type\UnionType) {
$targetType = $targetType->withoutNull();
}

$ref = new ReflectionEnum($targetType->toString());

if ($ref->isBacked()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace Test\Ecotone\Messaging\Unit\Handler;

use Ecotone\Lite\EcotoneLite;
use Ecotone\Messaging\Attribute\Parameter\Header;
use Ecotone\Messaging\Channel\SimpleMessageChannelBuilder;
use Ecotone\Messaging\Config\ServiceConfiguration;
use Ecotone\Messaging\Conversion\MediaType;
use Ecotone\Modelling\Attribute\CommandHandler;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid;
Expand Down Expand Up @@ -104,6 +106,31 @@ public function test_using_fallback_conversion_to_json(ServiceConfiguration $ser
);
}

public function test_converting_scalar_header_to_nullable_enum(): void
{
$handler = new class () {
public ?DeliveryMethod $deliveryMethod = null;

#[CommandHandler('withNullableEnumConversion', endpointId: 'withNullableEnumConversionEndpoint')]
public function handle(
#[Header('deliveryMethod')] ?DeliveryMethod $deliveryMethod = null
): void {
$this->deliveryMethod = $deliveryMethod;
}
};

$ecotoneLite = EcotoneLite::bootstrapFlowTesting(
[$handler::class],
[$handler],
);

$ecotoneLite->sendCommandWithRoutingKey('withNullableEnumConversion', metadata: [
'deliveryMethod' => DeliveryMethod::Email->value,
]);

self::assertSame(DeliveryMethod::Email, $handler->deliveryMethod);
}

/**
* This will change nothing, as it's used for payload, not header conversion.
* However to be sure that it's not affecting header conversion, it's part of the test scenario
Expand All @@ -120,3 +147,8 @@ public static function differentDefaultSerializations(): iterable
];
}
}

enum DeliveryMethod: string
{
case Email = 'email';
}
Loading