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
8 changes: 4 additions & 4 deletions src/JsonApi/Serializer/ItemNormalizerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
}
}

$dataToDenormalize = array_merge(
$data['data']['attributes'] ?? [],
$data['data']['relationships'] ?? []
);
$dataToDenormalize = $data['data']['attributes'] ?? [];
foreach ($data['data']['relationships'] ?? [] as $name => $relationship) {
$dataToDenormalize[$name] = \is_array($relationship) && \array_key_exists('data', $relationship) ? $relationship['data'] : $relationship;
}

// Surface the client-generated id so the entity setter receives it.
if ($isPostOperation && $allowClientGeneratedId && isset($data['data']['id'])) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue8702;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;

#[ApiResource(
shortName: 'Issue8702Author',
formats: ['jsonapi' => ['application/vnd.api+json']],
operations: [new Get(uriTemplate: '/issue8702_authors/{id}', provider: [self::class, 'provide'])],
)]
final class Issue8702Author
{
public function __construct(
#[ApiProperty(identifier: true)]
public int $id = 1,
public string $name = 'Frank Herbert',
) {
}

public static function provide(): self
{
return new self();
}
}
53 changes: 53 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/Issue8702/Issue8702Book.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue8702;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Post;

#[ApiResource(
shortName: 'Issue8702Book',
formats: ['jsonapi' => ['application/vnd.api+json']],
operations: [
new Get(uriTemplate: '/issue8702_books/{id}', provider: [self::class, 'provide']),
new Post(uriTemplate: '/issue8702_books', processor: [self::class, 'process']),
],
)]
final class Issue8702Book
{
/**
* @param list<Issue8702Author> $coAuthors
*/
public function __construct(
#[ApiProperty(identifier: true)]
public int $id = 1,
public string $title = 'Dune',
public ?Issue8702Author $author = null,
public array $coAuthors = [],
public array $payload = [],
) {
}

public static function provide(): null
{
return null;
}

public static function process(self $data): self
{
return $data;
}
}
75 changes: 75 additions & 0 deletions tests/Functional/JsonApi/ConstructorRelationshipLinkageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Functional\JsonApi;

use ApiPlatform\Test\ApiTestCase;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue8702\Issue8702Author;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue8702\Issue8702Book;
use ApiPlatform\Tests\SetupClassResourcesTrait;
use Symfony\Contracts\HttpClient\ResponseInterface;

final class ConstructorRelationshipLinkageTest extends ApiTestCase
{
use SetupClassResourcesTrait;

protected static ?bool $alwaysBootKernel = false;

/**
* @return class-string[]
*/
public static function getResources(): array
{
return [Issue8702Author::class, Issue8702Book::class];
}

public function testToOneResourceLinkage(): void
{
$response = $this->post(['author' => ['data' => ['type' => 'Issue8702Author', 'id' => '/issue8702_authors/1']]]);

$this->assertResponseStatusCodeSame(201);
$this->assertSame('/issue8702_authors/1', $response->toArray()['data']['relationships']['author']['data']['id']);
}

public function testToOneNullLinkage(): void
{
$response = $this->post(['author' => ['data' => null]]);

$this->assertResponseStatusCodeSame(201);
$this->assertNull($response->toArray()['data']['relationships']['author']['data']);
}

public function testToManyResourceLinkage(): void
{
$response = $this->post(['coAuthors' => ['data' => [['type' => 'Issue8702Author', 'id' => '/issue8702_authors/1']]]]);

$this->assertResponseStatusCodeSame(201);
$this->assertSame('/issue8702_authors/1', $response->toArray()['data']['relationships']['coAuthors']['data'][0]['id']);
}

public function testAttributeWithDataKeyIsNotUnwrapped(): void
{
$response = $this->post([], ['payload' => ['data' => [1, 2], 'x' => 3]]);

$this->assertResponseStatusCodeSame(201);
$this->assertSame(['data' => [1, 2], 'x' => 3], $response->toArray()['data']['attributes']['payload']);
}

private function post(array $relationships, array $attributes = []): ResponseInterface
{
return self::createClient()->request('POST', '/issue8702_books', [
'headers' => ['Accept' => 'application/vnd.api+json', 'Content-Type' => 'application/vnd.api+json'],
'json' => ['data' => ['type' => 'Issue8702Book', 'attributes' => ['title' => 'A'] + $attributes, 'relationships' => $relationships]],
]);
}
}
Loading