Skip to content

Commit

Permalink
fix(serializer): json non-resource intermittent class (HAL & JSON:API) (
Browse files Browse the repository at this point in the history
  • Loading branch information
GwendolenLynch authored Mar 19, 2024
1 parent 818b9cd commit 0073a2a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
24 changes: 24 additions & 0 deletions features/issues/5926.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,27 @@ Feature: Issue 5926
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/json; charset=utf-8"

@!mongodb
Scenario: Create and retrieve a JSON:API WriteResource
When I add "Accept" header equal to "application/vnd.api+json"
And I send a "GET" request to "/test_issue5926s/1"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/vnd.api+json; charset=utf-8"

@!mongodb
Scenario: Create and retrieve a LD+JSON WriteResource
When I add "Accept" header equal to "application/ld+json"
And I send a "GET" request to "/test_issue5926s/1"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"

@!mongodb
Scenario: Create and retrieve a HAL WriteResource
When I add "Accept" header equal to "application/hal+json"
And I send a "GET" request to "/test_issue5926s/1"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/hal+json; charset=utf-8"
5 changes: 3 additions & 2 deletions src/Hal/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ public function normalize(mixed $object, ?string $format = null, array $context
return parent::normalize($object, $format, $context);
}

if ($this->resourceClassResolver->isResourceClass($resourceClass)) {
$resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null);
$previousResourceClass = $context['resource_class'] ?? null;
if ($this->resourceClassResolver->isResourceClass($resourceClass) && (null === $previousResourceClass || $this->resourceClassResolver->isResourceClass($previousResourceClass))) {
$resourceClass = $this->resourceClassResolver->getResourceClass($object, $previousResourceClass);
}

$context = $this->initContext($resourceClass, $context);
Expand Down
5 changes: 3 additions & 2 deletions src/JsonApi/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ public function normalize(mixed $object, ?string $format = null, array $context
return parent::normalize($object, $format, $context);
}

if ($this->resourceClassResolver->isResourceClass($resourceClass)) {
$resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null);
$previousResourceClass = $context['resource_class'] ?? null;
if ($this->resourceClassResolver->isResourceClass($resourceClass) && (null === $previousResourceClass || $this->resourceClassResolver->isResourceClass($previousResourceClass))) {
$resourceClass = $this->resourceClassResolver->getResourceClass($object, $previousResourceClass);
}

if (($operation = $context['operation'] ?? null) && method_exists($operation, 'getItemUriTemplate')) {
Expand Down

0 comments on commit 0073a2a

Please sign in to comment.