|
15 | 15 |
|
16 | 16 | use ApiPlatform\JsonApi\JsonSchema\SchemaFactory; |
17 | 17 | use ApiPlatform\JsonApi\Tests\Fixtures\Dummy; |
| 18 | +use ApiPlatform\JsonApi\Tests\Fixtures\OtherRelatedDummy; |
| 19 | +use ApiPlatform\JsonApi\Tests\Fixtures\RelatedDummy; |
18 | 20 | use ApiPlatform\JsonSchema\DefinitionNameFactory; |
19 | 21 | use ApiPlatform\JsonSchema\Schema; |
20 | 22 | use ApiPlatform\JsonSchema\SchemaFactory as BaseSchemaFactory; |
| 23 | +use ApiPlatform\Metadata\ApiProperty; |
21 | 24 | use ApiPlatform\Metadata\ApiResource; |
22 | 25 | use ApiPlatform\Metadata\Get; |
23 | 26 | use ApiPlatform\Metadata\GetCollection; |
|
31 | 34 | use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
32 | 35 | use ApiPlatform\Metadata\ResourceClassResolverInterface; |
33 | 36 | use PHPUnit\Framework\TestCase; |
| 37 | +use Prophecy\Argument; |
34 | 38 | use Prophecy\PhpUnit\ProphecyTrait; |
| 39 | +use Symfony\Component\TypeInfo\Type; |
35 | 40 |
|
36 | 41 | class SchemaFactoryTest extends TestCase |
37 | 42 | { |
@@ -112,7 +117,8 @@ public function testHasRootDefinitionKeyBuildSchema(): void |
112 | 117 | 'type' => 'string', |
113 | 118 | ], |
114 | 119 | 'attributes' => [ |
115 | | - '$ref' => '#/definitions/Dummy', |
| 120 | + 'type' => 'object', |
| 121 | + 'properties' => [], |
116 | 122 | ], |
117 | 123 | ], |
118 | 124 | 'required' => [ |
@@ -155,7 +161,8 @@ public function testSchemaTypeBuildSchema(): void |
155 | 161 | $this->assertArrayHasKey('data', $objectSchema['properties']); |
156 | 162 |
|
157 | 163 | $this->assertArrayHasKey('items', $objectSchema['properties']['data']); |
158 | | - $this->assertArrayHasKey('$ref', $objectSchema['properties']['data']['items']['properties']['attributes']); |
| 164 | + $this->assertArrayNotHasKey('$ref', $objectSchema['properties']['data']['items']['properties']['attributes']); |
| 165 | + $this->assertSame('object', $objectSchema['properties']['data']['items']['properties']['attributes']['type']); |
159 | 166 |
|
160 | 167 | $properties = $objectSchema['properties']; |
161 | 168 | $this->assertArrayHasKey('data', $properties); |
@@ -199,4 +206,193 @@ public function testPostOutputSchemaRequiresId(): void |
199 | 206 | $data = $definitions[$rootDefinitionKey]['properties']['data']; |
200 | 207 | $this->assertSame(['type', 'id'], $data['required']); |
201 | 208 | } |
| 209 | + |
| 210 | + public function testRelationIsExcludedFromAttributes(): void |
| 211 | + { |
| 212 | + $schemaFactory = $this->buildSchemaFactoryWithRelation(); |
| 213 | + $resultSchema = $schemaFactory->buildSchema(Dummy::class, 'jsonapi'); |
| 214 | + |
| 215 | + $definitions = $resultSchema->getDefinitions(); |
| 216 | + $rootDefinitionKey = $resultSchema->getRootDefinitionKey(); |
| 217 | + $dataProperties = $definitions[$rootDefinitionKey]['properties']['data']['properties']; |
| 218 | + |
| 219 | + $this->assertArrayHasKey('attributes', $dataProperties); |
| 220 | + $this->assertArrayNotHasKey('$ref', $dataProperties['attributes']); |
| 221 | + $this->assertSame('object', $dataProperties['attributes']['type']); |
| 222 | + |
| 223 | + $attributes = $dataProperties['attributes']['properties']; |
| 224 | + $this->assertArrayHasKey('name', $attributes); |
| 225 | + $this->assertArrayNotHasKey('relatedDummy', $attributes, 'relations must not be documented as attributes'); |
| 226 | + |
| 227 | + $this->assertArrayHasKey('_id', $attributes, 'id is exposed as _id in the JSON:API attributes'); |
| 228 | + $this->assertArrayNotHasKey('id', $attributes); |
| 229 | + |
| 230 | + $this->assertArrayHasKey('relationships', $dataProperties); |
| 231 | + $this->assertArrayHasKey('relatedDummy', $dataProperties['relationships']['properties']); |
| 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 | + |
| 338 | + private function buildSchemaFactoryWithRelation(): SchemaFactory |
| 339 | + { |
| 340 | + $dummyOperation = (new Get())->withName('get')->withShortName('Dummy'); |
| 341 | + $relatedOperation = (new Get())->withName('get')->withShortName('RelatedDummy'); |
| 342 | + |
| 343 | + $resourceMetadataFactory = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class); |
| 344 | + $resourceMetadataFactory->create(Dummy::class)->willReturn( |
| 345 | + new ResourceMetadataCollection(Dummy::class, [ |
| 346 | + (new ApiResource())->withOperations(new Operations(['get' => $dummyOperation])), |
| 347 | + ]) |
| 348 | + ); |
| 349 | + $resourceMetadataFactory->create(RelatedDummy::class)->willReturn( |
| 350 | + new ResourceMetadataCollection(RelatedDummy::class, [ |
| 351 | + (new ApiResource())->withOperations(new Operations(['get' => $relatedOperation])), |
| 352 | + ]) |
| 353 | + ); |
| 354 | + |
| 355 | + $propertyNameCollectionFactory = $this->prophesize(PropertyNameCollectionFactoryInterface::class); |
| 356 | + $propertyNameCollectionFactory->create(Dummy::class, Argument::type('array'))->willReturn(new PropertyNameCollection(['id', 'name', 'relatedDummy'])); |
| 357 | + $propertyNameCollectionFactory->create(RelatedDummy::class, Argument::type('array'))->willReturn(new PropertyNameCollection(['id', 'name'])); |
| 358 | + |
| 359 | + $propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class); |
| 360 | + $propertyMetadataFactory->create(Dummy::class, 'id', Argument::type('array'))->willReturn( |
| 361 | + (new ApiProperty())->withNativeType(Type::int())->withReadable(true)->withSchema(['type' => 'integer']) |
| 362 | + ); |
| 363 | + $propertyMetadataFactory->create(Dummy::class, 'name', Argument::type('array'))->willReturn( |
| 364 | + (new ApiProperty())->withNativeType(Type::string())->withReadable(true)->withSchema(['type' => 'string']) |
| 365 | + ); |
| 366 | + $propertyMetadataFactory->create(Dummy::class, 'relatedDummy', Argument::type('array'))->willReturn( |
| 367 | + (new ApiProperty())->withNativeType(Type::object(RelatedDummy::class))->withReadable(true)->withSchema(['type' => Schema::UNKNOWN_TYPE]) |
| 368 | + ); |
| 369 | + $propertyMetadataFactory->create(RelatedDummy::class, 'id', Argument::type('array'))->willReturn( |
| 370 | + (new ApiProperty())->withNativeType(Type::int())->withReadable(true)->withSchema(['type' => 'integer']) |
| 371 | + ); |
| 372 | + $propertyMetadataFactory->create(RelatedDummy::class, 'name', Argument::type('array'))->willReturn( |
| 373 | + (new ApiProperty())->withNativeType(Type::string())->withReadable(true)->withSchema(['type' => 'string']) |
| 374 | + ); |
| 375 | + |
| 376 | + $resourceClassResolver = $this->prophesize(ResourceClassResolverInterface::class); |
| 377 | + $resourceClassResolver->isResourceClass(Dummy::class)->willReturn(true); |
| 378 | + $resourceClassResolver->isResourceClass(RelatedDummy::class)->willReturn(true); |
| 379 | + |
| 380 | + $definitionNameFactory = new DefinitionNameFactory(null); |
| 381 | + |
| 382 | + $baseSchemaFactory = new BaseSchemaFactory( |
| 383 | + resourceMetadataFactory: $resourceMetadataFactory->reveal(), |
| 384 | + propertyNameCollectionFactory: $propertyNameCollectionFactory->reveal(), |
| 385 | + propertyMetadataFactory: $propertyMetadataFactory->reveal(), |
| 386 | + resourceClassResolver: $resourceClassResolver->reveal(), |
| 387 | + definitionNameFactory: $definitionNameFactory, |
| 388 | + ); |
| 389 | + |
| 390 | + return new SchemaFactory( |
| 391 | + schemaFactory: $baseSchemaFactory, |
| 392 | + propertyMetadataFactory: $propertyMetadataFactory->reveal(), |
| 393 | + resourceClassResolver: $resourceClassResolver->reveal(), |
| 394 | + resourceMetadataFactory: $resourceMetadataFactory->reveal(), |
| 395 | + definitionNameFactory: $definitionNameFactory, |
| 396 | + ); |
| 397 | + } |
202 | 398 | } |
0 commit comments