|
15 | 15 |
|
16 | 16 | use ApiPlatform\JsonApi\JsonSchema\SchemaFactory; |
17 | 17 | use ApiPlatform\JsonApi\Tests\Fixtures\Dummy; |
| 18 | +use ApiPlatform\JsonApi\Tests\Fixtures\OtherRelatedDummy; |
18 | 19 | use ApiPlatform\JsonApi\Tests\Fixtures\RelatedDummy; |
19 | 20 | use ApiPlatform\JsonSchema\DefinitionNameFactory; |
20 | 21 | use ApiPlatform\JsonSchema\Schema; |
@@ -230,6 +231,110 @@ public function testRelationIsExcludedFromAttributes(): void |
230 | 231 | $this->assertArrayHasKey('relatedDummy', $dataProperties['relationships']['properties']); |
231 | 232 | } |
232 | 233 |
|
| 234 | + public function testRelationshipLinkageRequiresTypeAndId(): void |
| 235 | + { |
| 236 | + $schemaFactory = $this->buildSchemaFactoryWithRelation(); |
| 237 | + $resultSchema = $schemaFactory->buildSchema(Dummy::class, 'jsonapi'); |
| 238 | + |
| 239 | + $definitions = $resultSchema->getDefinitions(); |
| 240 | + $rootDefinitionKey = $resultSchema->getRootDefinitionKey(); |
| 241 | + $dataProperties = $definitions[$rootDefinitionKey]['properties']['data']['properties']; |
| 242 | + |
| 243 | + // a resource identifier object MUST contain type and id, @see https://jsonapi.org/format/#document-resource-identifier-objects |
| 244 | + $linkage = $dataProperties['relationships']['properties']['relatedDummy']['properties']['data']['oneOf'][1]; |
| 245 | + $this->assertSame('object', $linkage['type']); |
| 246 | + $this->assertSame(['type', 'id'], $linkage['required']); |
| 247 | + } |
| 248 | + |
| 249 | + public function testIncludedListsAllPolymorphicRelationTargets(): void |
| 250 | + { |
| 251 | + $schemaFactory = $this->buildSchemaFactoryWithPolymorphicRelation(); |
| 252 | + $resultSchema = $schemaFactory->buildSchema(Dummy::class, 'jsonapi'); |
| 253 | + |
| 254 | + $definitions = $resultSchema->getDefinitions(); |
| 255 | + $rootDefinitionKey = $resultSchema->getRootDefinitionKey(); |
| 256 | + $included = $definitions[$rootDefinitionKey]['properties']['included']; |
| 257 | + |
| 258 | + $refs = array_column($included['items']['anyOf'], '$ref'); |
| 259 | + $this->assertContains('#/definitions/RelatedDummy.jsonapi', $refs); |
| 260 | + $this->assertContains('#/definitions/OtherRelatedDummy.jsonapi', $refs, 'every target of a polymorphic relation must be listed in included'); |
| 261 | + } |
| 262 | + |
| 263 | + private function buildSchemaFactoryWithPolymorphicRelation(): SchemaFactory |
| 264 | + { |
| 265 | + $dummyOperation = (new Get())->withName('get')->withShortName('Dummy'); |
| 266 | + $relatedOperation = (new Get())->withName('get')->withShortName('RelatedDummy'); |
| 267 | + $otherRelatedOperation = (new Get())->withName('get')->withShortName('OtherRelatedDummy'); |
| 268 | + |
| 269 | + $resourceMetadataFactory = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class); |
| 270 | + $resourceMetadataFactory->create(Dummy::class)->willReturn( |
| 271 | + new ResourceMetadataCollection(Dummy::class, [ |
| 272 | + (new ApiResource())->withOperations(new Operations(['get' => $dummyOperation])), |
| 273 | + ]) |
| 274 | + ); |
| 275 | + $resourceMetadataFactory->create(RelatedDummy::class)->willReturn( |
| 276 | + new ResourceMetadataCollection(RelatedDummy::class, [ |
| 277 | + (new ApiResource())->withOperations(new Operations(['get' => $relatedOperation])), |
| 278 | + ]) |
| 279 | + ); |
| 280 | + $resourceMetadataFactory->create(OtherRelatedDummy::class)->willReturn( |
| 281 | + new ResourceMetadataCollection(OtherRelatedDummy::class, [ |
| 282 | + (new ApiResource())->withOperations(new Operations(['get' => $otherRelatedOperation])), |
| 283 | + ]) |
| 284 | + ); |
| 285 | + |
| 286 | + $propertyNameCollectionFactory = $this->prophesize(PropertyNameCollectionFactoryInterface::class); |
| 287 | + $propertyNameCollectionFactory->create(Dummy::class, Argument::type('array'))->willReturn(new PropertyNameCollection(['id', 'name', 'relatedDummy'])); |
| 288 | + $propertyNameCollectionFactory->create(RelatedDummy::class, Argument::type('array'))->willReturn(new PropertyNameCollection(['id', 'name'])); |
| 289 | + $propertyNameCollectionFactory->create(OtherRelatedDummy::class, Argument::type('array'))->willReturn(new PropertyNameCollection(['id', 'label'])); |
| 290 | + |
| 291 | + $propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class); |
| 292 | + $propertyMetadataFactory->create(Dummy::class, 'id', Argument::type('array'))->willReturn( |
| 293 | + (new ApiProperty())->withNativeType(Type::int())->withReadable(true)->withSchema(['type' => 'integer']) |
| 294 | + ); |
| 295 | + $propertyMetadataFactory->create(Dummy::class, 'name', Argument::type('array'))->willReturn( |
| 296 | + (new ApiProperty())->withNativeType(Type::string())->withReadable(true)->withSchema(['type' => 'string']) |
| 297 | + ); |
| 298 | + $propertyMetadataFactory->create(Dummy::class, 'relatedDummy', Argument::type('array'))->willReturn( |
| 299 | + (new ApiProperty())->withNativeType(Type::union(Type::object(RelatedDummy::class), Type::object(OtherRelatedDummy::class)))->withReadable(true)->withSchema(['type' => Schema::UNKNOWN_TYPE]) |
| 300 | + ); |
| 301 | + $propertyMetadataFactory->create(RelatedDummy::class, 'id', Argument::type('array'))->willReturn( |
| 302 | + (new ApiProperty())->withNativeType(Type::int())->withReadable(true)->withSchema(['type' => 'integer']) |
| 303 | + ); |
| 304 | + $propertyMetadataFactory->create(RelatedDummy::class, 'name', Argument::type('array'))->willReturn( |
| 305 | + (new ApiProperty())->withNativeType(Type::string())->withReadable(true)->withSchema(['type' => 'string']) |
| 306 | + ); |
| 307 | + $propertyMetadataFactory->create(OtherRelatedDummy::class, 'id', Argument::type('array'))->willReturn( |
| 308 | + (new ApiProperty())->withNativeType(Type::int())->withReadable(true)->withSchema(['type' => 'integer']) |
| 309 | + ); |
| 310 | + $propertyMetadataFactory->create(OtherRelatedDummy::class, 'label', Argument::type('array'))->willReturn( |
| 311 | + (new ApiProperty())->withNativeType(Type::string())->withReadable(true)->withSchema(['type' => 'string']) |
| 312 | + ); |
| 313 | + |
| 314 | + $resourceClassResolver = $this->prophesize(ResourceClassResolverInterface::class); |
| 315 | + $resourceClassResolver->isResourceClass(Dummy::class)->willReturn(true); |
| 316 | + $resourceClassResolver->isResourceClass(RelatedDummy::class)->willReturn(true); |
| 317 | + $resourceClassResolver->isResourceClass(OtherRelatedDummy::class)->willReturn(true); |
| 318 | + |
| 319 | + $definitionNameFactory = new DefinitionNameFactory(null); |
| 320 | + |
| 321 | + $baseSchemaFactory = new BaseSchemaFactory( |
| 322 | + resourceMetadataFactory: $resourceMetadataFactory->reveal(), |
| 323 | + propertyNameCollectionFactory: $propertyNameCollectionFactory->reveal(), |
| 324 | + propertyMetadataFactory: $propertyMetadataFactory->reveal(), |
| 325 | + resourceClassResolver: $resourceClassResolver->reveal(), |
| 326 | + definitionNameFactory: $definitionNameFactory, |
| 327 | + ); |
| 328 | + |
| 329 | + return new SchemaFactory( |
| 330 | + schemaFactory: $baseSchemaFactory, |
| 331 | + propertyMetadataFactory: $propertyMetadataFactory->reveal(), |
| 332 | + resourceClassResolver: $resourceClassResolver->reveal(), |
| 333 | + resourceMetadataFactory: $resourceMetadataFactory->reveal(), |
| 334 | + definitionNameFactory: $definitionNameFactory, |
| 335 | + ); |
| 336 | + } |
| 337 | + |
233 | 338 | private function buildSchemaFactoryWithRelation(): SchemaFactory |
234 | 339 | { |
235 | 340 | $dummyOperation = (new Get())->withName('get')->withShortName('Dummy'); |
|
0 commit comments