|
| 1 | +<?php |
| 2 | +// --- |
| 3 | +// slug: return-the-iri-of-your-resources-relations |
| 4 | +// name: How to return an IRI instead of an object for your resources relations ? |
| 5 | +// executable: true |
| 6 | +// tags: serialization |
| 7 | +// --- |
| 8 | + |
| 9 | +// This guide shows you how to expose the IRI of a related (sub)ressource relation instead of an object. |
| 10 | + |
| 11 | +namespace App\ApiResource { |
| 12 | + use ApiPlatform\Metadata\ApiProperty; |
| 13 | + use ApiPlatform\Metadata\ApiResource; |
| 14 | + use ApiPlatform\Metadata\Get; |
| 15 | + use ApiPlatform\Metadata\GetCollection; |
| 16 | + use ApiPlatform\Metadata\Link; |
| 17 | + use ApiPlatform\Metadata\Operation; |
| 18 | + |
| 19 | + #[ApiResource( |
| 20 | + operations: [ |
| 21 | + new Get(provider: Brand::class.'::provide'), |
| 22 | + ], |
| 23 | + )] |
| 24 | + class Brand |
| 25 | + { |
| 26 | + public function __construct( |
| 27 | + #[ApiProperty(identifier: true)] |
| 28 | + public readonly int $id = 1, |
| 29 | + |
| 30 | + public readonly string $name = 'Anon', |
| 31 | + |
| 32 | + // Setting uriTemplate on a relation with a resource collection will try to find the related operation. |
| 33 | + // It is based on the uriTemplate set on the operation defined on the Car resource (see below). |
| 34 | + /** |
| 35 | + * @var array<int, Car> $cars |
| 36 | + */ |
| 37 | + #[ApiProperty(uriTemplate: '/brands/{brandId}/cars')] |
| 38 | + private array $cars = [], |
| 39 | + |
| 40 | + // Setting uriTemplate on a relation with a resource item will try to find the related operation. |
| 41 | + // It is based on the uriTemplate set on the operation defined on the Address resource (see below). |
| 42 | + #[ApiProperty(uriTemplate: '/brands/{brandId}/addresses/{id}')] |
| 43 | + private ?Address $headQuarters = null |
| 44 | + ) |
| 45 | + { |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @return array<int, Car> |
| 50 | + */ |
| 51 | + public function getCars(): array |
| 52 | + { |
| 53 | + return $this->cars; |
| 54 | + } |
| 55 | + |
| 56 | + public function addCar(Car $car): self |
| 57 | + { |
| 58 | + $car->setBrand($this); |
| 59 | + $this->cars[] = $car; |
| 60 | + |
| 61 | + return $this; |
| 62 | + } |
| 63 | + |
| 64 | + public function getHeadQuarters(): ?Address |
| 65 | + { |
| 66 | + return $this->headQuarters; |
| 67 | + } |
| 68 | + |
| 69 | + public function setHeadQuarters(?Address $headQuarters): self |
| 70 | + { |
| 71 | + $headQuarters?->setBrand($this); |
| 72 | + $this->headQuarters = $headQuarters; |
| 73 | + |
| 74 | + return $this; |
| 75 | + } |
| 76 | + |
| 77 | + public static function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null |
| 78 | + { |
| 79 | + return (new Brand(1, 'Ford')) |
| 80 | + ->setHeadQuarters(new Address(1, 'One American Road near Michigan Avenue, Dearborn, Michigan')) |
| 81 | + ->addCar(new Car(1, 'Torpedo Roadster')); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + #[ApiResource( |
| 86 | + operations: [ |
| 87 | + new Get, |
| 88 | + // Without the use of uriTemplate on the property this would be used coming from the Brand resource, but not anymore. |
| 89 | + new GetCollection(uriTemplate: '/cars'), |
| 90 | + // This operation will be used to create the IRI instead since the uriTemplate matches. |
| 91 | + new GetCollection( |
| 92 | + uriTemplate: '/brands/{brandId}/cars', |
| 93 | + uriVariables: [ |
| 94 | + 'brandId' => new Link(toProperty: 'brand', fromClass: Brand::class), |
| 95 | + ] |
| 96 | + ), |
| 97 | + ], |
| 98 | + )] |
| 99 | + class Car |
| 100 | + { |
| 101 | + public function __construct( |
| 102 | + #[ApiProperty(identifier: true)] |
| 103 | + public readonly int $id = 1, |
| 104 | + public readonly string $name = 'Anon', |
| 105 | + private ?Brand $brand = null |
| 106 | + ) |
| 107 | + { |
| 108 | + } |
| 109 | + |
| 110 | + public function getBrand(): Brand |
| 111 | + { |
| 112 | + return $this->brand; |
| 113 | + } |
| 114 | + |
| 115 | + public function setBrand(Brand $brand): void |
| 116 | + { |
| 117 | + $this->brand = $brand; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + #[ApiResource( |
| 122 | + operations: [ |
| 123 | + // Without the use of uriTemplate on the property this would be used coming from the Brand resource, but not anymore. |
| 124 | + new Get(uriTemplate: '/addresses/{id}'), |
| 125 | + // This operation will be used to create the IRI instead since the uriTemplate matches. |
| 126 | + new Get( |
| 127 | + uriTemplate: '/brands/{brandId}/addresses/{id}', |
| 128 | + uriVariables: [ |
| 129 | + 'brandId' => new Link(toProperty: 'brand', fromClass: Brand::class), |
| 130 | + 'id' => new Link(fromClass: Address::class), |
| 131 | + ] |
| 132 | + ) |
| 133 | + ], |
| 134 | + )] |
| 135 | + class Address |
| 136 | + { |
| 137 | + public function __construct( |
| 138 | + #[ApiProperty(identifier: true)] |
| 139 | + public readonly int $id = 1, |
| 140 | + public readonly string $name = 'Anon', |
| 141 | + private ?Brand $brand = null |
| 142 | + ) |
| 143 | + { |
| 144 | + } |
| 145 | + |
| 146 | + public function getBrand(): Brand |
| 147 | + { |
| 148 | + return $this->brand; |
| 149 | + } |
| 150 | + |
| 151 | + public function setBrand(Brand $brand): void |
| 152 | + { |
| 153 | + $this->brand = $brand; |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +// If API Platform does not find any `GetCollection` operation on the target resource, it will result in a `NotFoundException`. |
| 159 | +// |
| 160 | +// The **OpenAPI** documentation will set the properties as `read-only` of type `string` in the format `iri-reference` for `JSON-LD`, `JSON:API` and `HAL` formats. |
| 161 | +// |
| 162 | +// The **Hydra** documentation will set the properties as `hydra:Link` with the right domain, with `hydra:readable` to `true` but `hydra:writable` to `false`. |
| 163 | +// |
| 164 | +// When using JSON:API or HAL formats, the IRI will be used and set links, embedded and relationship. |
| 165 | +// |
| 166 | +// *Additional Note:* If you are using the default doctrine provider, this will prevent unnecessary sql join and related processing. |
| 167 | + |
| 168 | +namespace App\Playground { |
| 169 | + use Symfony\Component\HttpFoundation\Request; |
| 170 | + |
| 171 | + function request(): Request |
| 172 | + { |
| 173 | + return Request::create(uri: '/brands/1', method: 'GET', server: ['HTTP_ACCEPT' => 'application/ld+json']); |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | + |
| 178 | +namespace App\Tests { |
| 179 | + use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; |
| 180 | + use App\ApiResource\Brand; |
| 181 | + |
| 182 | + final class BrandTest extends ApiTestCase |
| 183 | + { |
| 184 | + |
| 185 | + public function testResourceExposeIRI(): void |
| 186 | + { |
| 187 | + static::createClient()->request('GET', '/brands/1', ['headers' => [ |
| 188 | + 'Accept: application/ld+json' |
| 189 | + ]]); |
| 190 | + |
| 191 | + $this->assertResponseIsSuccessful(); |
| 192 | + $this->assertMatchesResourceCollectionJsonSchema(Brand::class, '_api_/brands/{id}{._format}_get'); |
| 193 | + $this->assertJsonContains([ |
| 194 | + "@context" => "/contexts/Brand", |
| 195 | + "@id" => "/brands/1", |
| 196 | + "@type" => "Brand", |
| 197 | + "name"=> "Ford", |
| 198 | + "cars" => "/brands/1/cars", |
| 199 | + "headQuarters" => "/brands/1/addresses/1" |
| 200 | + ]); |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments