|
16 | 16 | use ApiPlatform\JsonSchema\DefinitionNameFactory; |
17 | 17 | use ApiPlatform\JsonSchema\Schema; |
18 | 18 | use ApiPlatform\JsonSchema\SchemaFactory; |
| 19 | +use ApiPlatform\JsonSchema\Tests\Fixtures\ApiResource\ChildAttributeDummy; |
19 | 20 | use ApiPlatform\JsonSchema\Tests\Fixtures\ApiResource\OverriddenOperationDummy; |
| 21 | +use ApiPlatform\JsonSchema\Tests\Fixtures\ApiResource\ParentAttributeDummy; |
20 | 22 | use ApiPlatform\JsonSchema\Tests\Fixtures\DummyResourceInterface; |
21 | 23 | use ApiPlatform\JsonSchema\Tests\Fixtures\Enum\GenderTypeEnum; |
22 | 24 | use ApiPlatform\JsonSchema\Tests\Fixtures\GenericChild; |
|
25 | 27 | use ApiPlatform\JsonSchema\Tests\Fixtures\Serializable; |
26 | 28 | use ApiPlatform\Metadata\ApiProperty; |
27 | 29 | use ApiPlatform\Metadata\ApiResource; |
| 30 | +use ApiPlatform\Metadata\Get; |
28 | 31 | use ApiPlatform\Metadata\Operations; |
29 | 32 | use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
30 | 33 | use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; |
@@ -531,6 +534,95 @@ public function testBuildSchemaWithSerializerGroups(): void |
531 | 534 | $this->assertSame('object', $definitions[$rootDefinitionKey]['properties']['genderType']['type']); |
532 | 535 | } |
533 | 536 |
|
| 537 | + public function testBuildSchemaWithSerializerAttributes(): void |
| 538 | + { |
| 539 | + $shortName = (new \ReflectionClass(ParentAttributeDummy::class))->getShortName(); |
| 540 | + $childShortName = (new \ReflectionClass(ChildAttributeDummy::class))->getShortName(); |
| 541 | + $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class); |
| 542 | + $serializerAttributes = ['title', 'child' => ['name']]; |
| 543 | + $operation = (new Get())->withName('get')->withNormalizationContext([ |
| 544 | + 'attributes' => $serializerAttributes, |
| 545 | + AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false, |
| 546 | + ])->withShortName($shortName); |
| 547 | + $resourceMetadataFactoryProphecy->create(ParentAttributeDummy::class) |
| 548 | + ->willReturn( |
| 549 | + new ResourceMetadataCollection(ParentAttributeDummy::class, [ |
| 550 | + (new ApiResource())->withOperations(new Operations(['get' => $operation])), |
| 551 | + ]) |
| 552 | + ); |
| 553 | + $childOperation = (new Get())->withName('get')->withShortName($childShortName); |
| 554 | + $resourceMetadataFactoryProphecy->create(ChildAttributeDummy::class) |
| 555 | + ->willReturn( |
| 556 | + new ResourceMetadataCollection(ChildAttributeDummy::class, [ |
| 557 | + (new ApiResource())->withOperations(new Operations(['get' => $childOperation])), |
| 558 | + ]) |
| 559 | + ); |
| 560 | + |
| 561 | + $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class); |
| 562 | + $propertyNameCollectionFactoryProphecy->create(ParentAttributeDummy::class, Argument::type('array'))->willReturn(new PropertyNameCollection(['title', 'child'])); |
| 563 | + $propertyNameCollectionFactoryProphecy->create(ChildAttributeDummy::class, Argument::type('array'))->willReturn(new PropertyNameCollection(['name'])); |
| 564 | + |
| 565 | + $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class); |
| 566 | + $propertyMetadataFactoryProphecy->create(ParentAttributeDummy::class, 'title', Argument::type('array'))->willReturn( |
| 567 | + (new ApiProperty()) |
| 568 | + ->withNativeType(Type::string()) |
| 569 | + ->withReadable(true) |
| 570 | + ->withSchema(['type' => 'string']) |
| 571 | + ); |
| 572 | + $propertyMetadataFactoryProphecy->create(ParentAttributeDummy::class, 'child', Argument::type('array'))->willReturn( |
| 573 | + (new ApiProperty()) |
| 574 | + ->withNativeType(Type::object(ChildAttributeDummy::class)) |
| 575 | + ->withReadable(true) |
| 576 | + ->withSchema(['type' => Schema::UNKNOWN_TYPE]) |
| 577 | + ); |
| 578 | + $propertyMetadataFactoryProphecy->create(ChildAttributeDummy::class, 'name', Argument::type('array'))->willReturn( |
| 579 | + (new ApiProperty()) |
| 580 | + ->withNativeType(Type::string()) |
| 581 | + ->withReadable(true) |
| 582 | + ->withSchema(['type' => 'string']) |
| 583 | + ); |
| 584 | + $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); |
| 585 | + $resourceClassResolverProphecy->isResourceClass(ParentAttributeDummy::class)->willReturn(true); |
| 586 | + $resourceClassResolverProphecy->isResourceClass(ChildAttributeDummy::class)->willReturn(true); |
| 587 | + |
| 588 | + $definitionNameFactory = new DefinitionNameFactory(); |
| 589 | + |
| 590 | + $schemaFactory = new SchemaFactory( |
| 591 | + resourceMetadataFactory: $resourceMetadataFactoryProphecy->reveal(), |
| 592 | + propertyNameCollectionFactory: $propertyNameCollectionFactoryProphecy->reveal(), |
| 593 | + propertyMetadataFactory: $propertyMetadataFactoryProphecy->reveal(), |
| 594 | + resourceClassResolver: $resourceClassResolverProphecy->reveal(), |
| 595 | + definitionNameFactory: $definitionNameFactory, |
| 596 | + ); |
| 597 | + $resultSchema = $schemaFactory->buildSchema(ParentAttributeDummy::class, 'json', Schema::TYPE_OUTPUT, null, null, ['attributes' => $serializerAttributes, AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false]); |
| 598 | + |
| 599 | + $rootDefinitionKey = $resultSchema->getRootDefinitionKey(); |
| 600 | + $definitions = $resultSchema->getDefinitions(); |
| 601 | + |
| 602 | + $this->assertSame((new \ReflectionClass(ParentAttributeDummy::class))->getShortName().'-title_child.name', $rootDefinitionKey); |
| 603 | + $this->assertTrue(isset($definitions[$rootDefinitionKey])); |
| 604 | + $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]); |
| 605 | + $this->assertSame('object', $definitions[$rootDefinitionKey]['type']); |
| 606 | + $this->assertFalse($definitions[$rootDefinitionKey]['additionalProperties']); |
| 607 | + $this->assertArrayHasKey('properties', $definitions[$rootDefinitionKey]); |
| 608 | + $this->assertArrayHasKey('title', $definitions[$rootDefinitionKey]['properties']); |
| 609 | + $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['title']); |
| 610 | + $this->assertSame('string', $definitions[$rootDefinitionKey]['properties']['title']['type']); |
| 611 | + $this->assertArrayHasKey('child', $definitions[$rootDefinitionKey]['properties']); |
| 612 | + $this->assertArrayHasKey('$ref', $definitions[$rootDefinitionKey]['properties']['child']); |
| 613 | + $this->assertSame('#/definitions/ChildAttributeDummy-name', $definitions[$rootDefinitionKey]['properties']['child']['$ref']); |
| 614 | + |
| 615 | + $childDefinitionKey = 'ChildAttributeDummy-name'; |
| 616 | + $this->assertTrue(isset($definitions[$childDefinitionKey])); |
| 617 | + $this->assertArrayHasKey('type', $definitions[$childDefinitionKey]); |
| 618 | + $this->assertSame('object', $definitions[$childDefinitionKey]['type']); |
| 619 | + $this->assertFalse($definitions[$childDefinitionKey]['additionalProperties']); |
| 620 | + $this->assertArrayHasKey('properties', $definitions[$childDefinitionKey]); |
| 621 | + $this->assertArrayHasKey('name', $definitions[$childDefinitionKey]['properties']); |
| 622 | + $this->assertArrayHasKey('type', $definitions[$childDefinitionKey]['properties']['name']); |
| 623 | + $this->assertSame('string', $definitions[$childDefinitionKey]['properties']['name']['type']); |
| 624 | + } |
| 625 | + |
534 | 626 | #[IgnoreDeprecations] |
535 | 627 | public function testBuildSchemaForAssociativeArrayLegacy(): void |
536 | 628 | { |
|
0 commit comments