|
15 | 15 |
|
16 | 16 | use ApiPlatform\JsonApi\JsonSchema\SchemaFactory; |
17 | 17 | use ApiPlatform\JsonApi\Tests\Fixtures\Dummy; |
| 18 | +use ApiPlatform\JsonApi\Tests\Fixtures\RelatedDummy; |
18 | 19 | use ApiPlatform\JsonSchema\DefinitionNameFactory; |
19 | 20 | use ApiPlatform\JsonSchema\Schema; |
20 | 21 | use ApiPlatform\JsonSchema\SchemaFactory as BaseSchemaFactory; |
| 22 | +use ApiPlatform\Metadata\ApiProperty; |
21 | 23 | use ApiPlatform\Metadata\ApiResource; |
22 | 24 | use ApiPlatform\Metadata\Get; |
23 | 25 | use ApiPlatform\Metadata\GetCollection; |
|
31 | 33 | use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
32 | 34 | use ApiPlatform\Metadata\ResourceClassResolverInterface; |
33 | 35 | use PHPUnit\Framework\TestCase; |
| 36 | +use Prophecy\Argument; |
34 | 37 | use Prophecy\PhpUnit\ProphecyTrait; |
| 38 | +use Symfony\Component\TypeInfo\Type; |
35 | 39 |
|
36 | 40 | class SchemaFactoryTest extends TestCase |
37 | 41 | { |
@@ -112,7 +116,8 @@ public function testHasRootDefinitionKeyBuildSchema(): void |
112 | 116 | 'type' => 'string', |
113 | 117 | ], |
114 | 118 | 'attributes' => [ |
115 | | - '$ref' => '#/definitions/Dummy', |
| 119 | + 'type' => 'object', |
| 120 | + 'properties' => [], |
116 | 121 | ], |
117 | 122 | ], |
118 | 123 | 'required' => [ |
@@ -155,7 +160,8 @@ public function testSchemaTypeBuildSchema(): void |
155 | 160 | $this->assertArrayHasKey('data', $objectSchema['properties']); |
156 | 161 |
|
157 | 162 | $this->assertArrayHasKey('items', $objectSchema['properties']['data']); |
158 | | - $this->assertArrayHasKey('$ref', $objectSchema['properties']['data']['items']['properties']['attributes']); |
| 163 | + $this->assertArrayNotHasKey('$ref', $objectSchema['properties']['data']['items']['properties']['attributes']); |
| 164 | + $this->assertSame('object', $objectSchema['properties']['data']['items']['properties']['attributes']['type']); |
159 | 165 |
|
160 | 166 | $properties = $objectSchema['properties']; |
161 | 167 | $this->assertArrayHasKey('data', $properties); |
@@ -199,4 +205,89 @@ public function testPostOutputSchemaRequiresId(): void |
199 | 205 | $data = $definitions[$rootDefinitionKey]['properties']['data']; |
200 | 206 | $this->assertSame(['type', 'id'], $data['required']); |
201 | 207 | } |
| 208 | + |
| 209 | + public function testRelationIsExcludedFromAttributes(): void |
| 210 | + { |
| 211 | + $schemaFactory = $this->buildSchemaFactoryWithRelation(); |
| 212 | + $resultSchema = $schemaFactory->buildSchema(Dummy::class, 'jsonapi'); |
| 213 | + |
| 214 | + $definitions = $resultSchema->getDefinitions(); |
| 215 | + $rootDefinitionKey = $resultSchema->getRootDefinitionKey(); |
| 216 | + $dataProperties = $definitions[$rootDefinitionKey]['properties']['data']['properties']; |
| 217 | + |
| 218 | + $this->assertArrayHasKey('attributes', $dataProperties); |
| 219 | + $this->assertArrayNotHasKey('$ref', $dataProperties['attributes']); |
| 220 | + $this->assertSame('object', $dataProperties['attributes']['type']); |
| 221 | + |
| 222 | + $attributes = $dataProperties['attributes']['properties']; |
| 223 | + $this->assertArrayHasKey('name', $attributes); |
| 224 | + $this->assertArrayNotHasKey('relatedDummy', $attributes, 'relations must not be documented as attributes'); |
| 225 | + |
| 226 | + $this->assertArrayHasKey('_id', $attributes, 'id is exposed as _id in the JSON:API attributes'); |
| 227 | + $this->assertArrayNotHasKey('id', $attributes); |
| 228 | + |
| 229 | + $this->assertArrayHasKey('relationships', $dataProperties); |
| 230 | + $this->assertArrayHasKey('relatedDummy', $dataProperties['relationships']['properties']); |
| 231 | + } |
| 232 | + |
| 233 | + private function buildSchemaFactoryWithRelation(): SchemaFactory |
| 234 | + { |
| 235 | + $dummyOperation = (new Get())->withName('get')->withShortName('Dummy'); |
| 236 | + $relatedOperation = (new Get())->withName('get')->withShortName('RelatedDummy'); |
| 237 | + |
| 238 | + $resourceMetadataFactory = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class); |
| 239 | + $resourceMetadataFactory->create(Dummy::class)->willReturn( |
| 240 | + new ResourceMetadataCollection(Dummy::class, [ |
| 241 | + (new ApiResource())->withOperations(new Operations(['get' => $dummyOperation])), |
| 242 | + ]) |
| 243 | + ); |
| 244 | + $resourceMetadataFactory->create(RelatedDummy::class)->willReturn( |
| 245 | + new ResourceMetadataCollection(RelatedDummy::class, [ |
| 246 | + (new ApiResource())->withOperations(new Operations(['get' => $relatedOperation])), |
| 247 | + ]) |
| 248 | + ); |
| 249 | + |
| 250 | + $propertyNameCollectionFactory = $this->prophesize(PropertyNameCollectionFactoryInterface::class); |
| 251 | + $propertyNameCollectionFactory->create(Dummy::class, Argument::type('array'))->willReturn(new PropertyNameCollection(['id', 'name', 'relatedDummy'])); |
| 252 | + $propertyNameCollectionFactory->create(RelatedDummy::class, Argument::type('array'))->willReturn(new PropertyNameCollection(['id', 'name'])); |
| 253 | + |
| 254 | + $propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class); |
| 255 | + $propertyMetadataFactory->create(Dummy::class, 'id', Argument::type('array'))->willReturn( |
| 256 | + (new ApiProperty())->withNativeType(Type::int())->withReadable(true)->withSchema(['type' => 'integer']) |
| 257 | + ); |
| 258 | + $propertyMetadataFactory->create(Dummy::class, 'name', Argument::type('array'))->willReturn( |
| 259 | + (new ApiProperty())->withNativeType(Type::string())->withReadable(true)->withSchema(['type' => 'string']) |
| 260 | + ); |
| 261 | + $propertyMetadataFactory->create(Dummy::class, 'relatedDummy', Argument::type('array'))->willReturn( |
| 262 | + (new ApiProperty())->withNativeType(Type::object(RelatedDummy::class))->withReadable(true)->withSchema(['type' => Schema::UNKNOWN_TYPE]) |
| 263 | + ); |
| 264 | + $propertyMetadataFactory->create(RelatedDummy::class, 'id', Argument::type('array'))->willReturn( |
| 265 | + (new ApiProperty())->withNativeType(Type::int())->withReadable(true)->withSchema(['type' => 'integer']) |
| 266 | + ); |
| 267 | + $propertyMetadataFactory->create(RelatedDummy::class, 'name', Argument::type('array'))->willReturn( |
| 268 | + (new ApiProperty())->withNativeType(Type::string())->withReadable(true)->withSchema(['type' => 'string']) |
| 269 | + ); |
| 270 | + |
| 271 | + $resourceClassResolver = $this->prophesize(ResourceClassResolverInterface::class); |
| 272 | + $resourceClassResolver->isResourceClass(Dummy::class)->willReturn(true); |
| 273 | + $resourceClassResolver->isResourceClass(RelatedDummy::class)->willReturn(true); |
| 274 | + |
| 275 | + $definitionNameFactory = new DefinitionNameFactory(null); |
| 276 | + |
| 277 | + $baseSchemaFactory = new BaseSchemaFactory( |
| 278 | + resourceMetadataFactory: $resourceMetadataFactory->reveal(), |
| 279 | + propertyNameCollectionFactory: $propertyNameCollectionFactory->reveal(), |
| 280 | + propertyMetadataFactory: $propertyMetadataFactory->reveal(), |
| 281 | + resourceClassResolver: $resourceClassResolver->reveal(), |
| 282 | + definitionNameFactory: $definitionNameFactory, |
| 283 | + ); |
| 284 | + |
| 285 | + return new SchemaFactory( |
| 286 | + schemaFactory: $baseSchemaFactory, |
| 287 | + propertyMetadataFactory: $propertyMetadataFactory->reveal(), |
| 288 | + resourceClassResolver: $resourceClassResolver->reveal(), |
| 289 | + resourceMetadataFactory: $resourceMetadataFactory->reveal(), |
| 290 | + definitionNameFactory: $definitionNameFactory, |
| 291 | + ); |
| 292 | + } |
202 | 293 | } |
0 commit comments