Skip to content

Commit 7110dc6

Browse files
committed
fix: hydra prefix with default context
1 parent 8884bd9 commit 7110dc6

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/Hydra/Serializer/HydraPrefixNameConverter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@
1717
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
1818
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
1919

20-
final class HydraPrefixNameConverter implements NameConverterInterface, AdvancedNameConverterInterface
20+
final readonly class HydraPrefixNameConverter implements NameConverterInterface, AdvancedNameConverterInterface
2121
{
22-
public function __construct(private readonly NameConverterInterface $nameConverter)
22+
/**
23+
* @param array<string,mixed> $defaultContext
24+
*/
25+
public function __construct(private NameConverterInterface $nameConverter, private array $defaultContext = [])
2326
{
2427
}
2528

2629
public function normalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string
2730
{
31+
$context += $this->defaultContext;
2832
$name = $this->nameConverter->normalize($propertyName, $class, $format, $context);
2933

3034
if (true === ($context[ContextBuilder::HYDRA_CONTEXT_HAS_PREFIX] ?? true)) {

src/Symfony/Bundle/Resources/config/hydra.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868

6969
<service id="api_platform.hydra.name_converter.hydra_prefix" class="ApiPlatform\Hydra\Serializer\HydraPrefixNameConverter" decorates="api_platform.name_converter">
7070
<argument type="service" id="api_platform.hydra.name_converter.hydra_prefix.inner" />
71+
<argument on-invalid="ignore">%api_platform.serializer.default_context%</argument>
7172
</service>
7273
</services>
7374

0 commit comments

Comments
 (0)