Skip to content

Commit 0d5b9a8

Browse files
committed
Fix mercure serialization context
1 parent b1713f3 commit 0d5b9a8

File tree

5 files changed

+26
-7
lines changed

5 files changed

+26
-7
lines changed

features/bootstrap/ProfilerContext.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,19 @@ public function theMercureMessageShouldContainTimestampedFields()
9898
Assert::assertArrayHasKey('modifiedAt', $messageArray);
9999
}
100100

101+
/**
102+
* @Then the Mercure message for component group should contain timestamped fields
103+
*/
104+
public function theMercureMessageForComponentGroupShouldContainTimestampedFields()
105+
{
106+
$messageObjects = $this->thereShouldBeAPublishedMercureUpdatePublished(2);
107+
$update = $messageObjects[1];
108+
$messageData = $update->getData();
109+
$messageArray = $this->jsonContext->getJsonAsArray($messageData);
110+
Assert::assertArrayHasKey('createdAt', $messageArray);
111+
Assert::assertArrayHasKey('modifiedAt', $messageArray);
112+
}
113+
101114
/**
102115
* @Then there should be :count mercure messages for draft resources
103116
*/

features/main/component_collections.feature renamed to features/main/component_groups.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Feature: ComponentGroup resource
2929
"""
3030
Then the response status code should be 201
3131
And the JSON should be valid according to the schema file "component_group.schema.json"
32+
And the JSON node "createdAt" should exist
3233

3334
@loginUser
3435
Scenario: When I delete a component collection resource, locations are also deleted but components are not

features/main/component_position.feature

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ Feature: Component positions
1313
And there is a DummyComponent
1414
When I send a "POST" request to "/_/component_positions" with data:
1515
| componentGroup | component |
16-
| resource[component_group] | resource[dummy_component] |
16+
| resource[component_group] | resource[dummy_component] |
1717
Then the response status code should be 201
1818
And the JSON should be valid according to the schema file "component_position.schema.json"
19+
And the Mercure message for component group should contain timestamped fields
1920

2021
@loginUser
2122
Scenario Outline: I can restrict which components are permitted to be inside a component collection and a component that must be specifically defined as being allowed to pass validation

src/Mercure/MercureResourcePublisher.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
3030
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
3131
use Symfony\Component\HttpFoundation\JsonResponse;
32+
use Symfony\Component\HttpFoundation\Request;
3233
use Symfony\Component\HttpFoundation\RequestStack;
3334
use Symfony\Component\Mercure\HubRegistry;
3435
use Symfony\Component\Mercure\Update;
3536
use Symfony\Component\Messenger\MessageBusInterface;
37+
use Symfony\Component\Serializer\Encoder\CsvEncoder;
3638
use Symfony\Component\Serializer\SerializerAwareInterface;
3739
use Symfony\Component\Serializer\SerializerAwareTrait;
3840

@@ -240,11 +242,13 @@ private function publishUpdate(object $object, array $objectData, string $type):
240242
} else {
241243
$resourceClass = $this->getObjectClass($object);
242244

243-
244-
$request = $this->requestStack->getCurrentRequest();
245-
$baseContext = $request ? $this->serializerContextBuilder->createFromRequest($request, true) : [];
246-
$context = $options['normalization_context'] ?? $this->resourceMetadataFactory->create($resourceClass)->getOperation()->getNormalizationContext() ?? [];
247-
$context = array_merge($baseContext, $context);
245+
$request = new Request();
246+
$attributes = [
247+
'operation' => $this->resourceMetadataFactory->create($resourceClass)->getOperation(),
248+
'resource_class' => $resourceClass
249+
];
250+
$baseContext = $this->serializerContextBuilder->createFromRequest($request, true, $attributes);
251+
$context = array_merge($baseContext, $options['normalization_context'] ?? []);
248252
try {
249253
$iri = $options['topics'] ?? $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_URL);
250254
$data = $options['data'] ?? $this->serializer->serialize($object, key($this->formats), $context);

src/Serializer/ContextBuilder/TimestampedContextBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function createFromRequest(Request $request, bool $normalization, array $
4343
} else {
4444
$context['groups'][] = sprintf('%s:%s:write', $reflectionClass->getShortName(), TimestampedLoader::GROUP_NAME);
4545
}
46-
dump($resourceClass, $context['groups']);
46+
4747
return $context;
4848
}
4949
}

0 commit comments

Comments
 (0)