• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 15040977736

15 May 2025 09:02AM UTC coverage: 21.754% (+13.3%) from 8.423%
15040977736

Pull #6960

github

web-flow
Merge 7a7a13526 into 1862d03b7
Pull Request #6960: feat(json-schema): mutualize json schema between formats

320 of 460 new or added lines in 24 files covered. (69.57%)

1863 existing lines in 109 files now uncovered.

11069 of 50882 relevant lines covered (21.75%)

29.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/JsonSchema/Tests/SchemaFactoryTest.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\JsonSchema\Tests;
15

16
use ApiPlatform\JsonSchema\DefinitionNameFactory;
17
use ApiPlatform\JsonSchema\Schema;
18
use ApiPlatform\JsonSchema\SchemaFactory;
19
use ApiPlatform\JsonSchema\Tests\Fixtures\ApiResource\OverriddenOperationDummy;
20
use ApiPlatform\JsonSchema\Tests\Fixtures\DummyResourceInterface;
21
use ApiPlatform\JsonSchema\Tests\Fixtures\Enum\GenderTypeEnum;
22
use ApiPlatform\JsonSchema\Tests\Fixtures\NotAResource;
23
use ApiPlatform\JsonSchema\Tests\Fixtures\NotAResourceWithUnionIntersectTypes;
24
use ApiPlatform\JsonSchema\Tests\Fixtures\Serializable;
25
use ApiPlatform\Metadata\ApiProperty;
26
use ApiPlatform\Metadata\ApiResource;
27
use ApiPlatform\Metadata\Operations;
28
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
29
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
30
use ApiPlatform\Metadata\Property\PropertyNameCollection;
31
use ApiPlatform\Metadata\Put;
32
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
33
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
34
use ApiPlatform\Metadata\ResourceClassResolverInterface;
35
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
36
use PHPUnit\Framework\TestCase;
37
use Prophecy\Argument;
38
use Prophecy\PhpUnit\ProphecyTrait;
39
use Symfony\Component\PropertyInfo\Type as LegacyType;
40
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
41
use Symfony\Component\TypeInfo\Type;
42

43
class SchemaFactoryTest extends TestCase
44
{
45
    use ProphecyTrait;
46

47
    #[IgnoreDeprecations]
48
    public function testBuildSchemaForNonResourceClassLegacy(): void
49
    {
50
        $this->expectUserDeprecationMessage('Since api-platform/metadata 4.2: The "ApiPlatform\Metadata\ApiProperty::withBuiltinTypes()" method is deprecated, use "ApiPlatform\Metadata\ApiProperty::withNativeType()" instead.');
×
51
        $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
52

53
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
54
        $propertyNameCollectionFactoryProphecy->create(NotAResource::class, Argument::cetera())->willReturn(new PropertyNameCollection(['foo', 'bar', 'genderType']));
×
55

56
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
57
        $propertyMetadataFactoryProphecy->create(NotAResource::class, 'foo', Argument::cetera())->willReturn(
×
58
            (new ApiProperty())
×
59
                ->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])
×
60
                ->withReadable(true)
×
61
                ->withSchema(['type' => 'string'])
×
62
        );
×
63
        $propertyMetadataFactoryProphecy->create(NotAResource::class, 'bar', Argument::cetera())->willReturn(
×
64
            (new ApiProperty())
×
65
                ->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_INT)])
×
66
                ->withReadable(true)
×
67
                ->withDefault('default_bar')
×
68
                ->withExample('example_bar')
×
69
                ->withSchema(['type' => 'integer', 'default' => 'default_bar', 'example' => 'example_bar'])
×
70
        );
×
71
        $propertyMetadataFactoryProphecy->create(NotAResource::class, 'genderType', Argument::cetera())->willReturn(
×
72
            (new ApiProperty())
×
73
                ->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT)])
×
74
                ->withReadable(true)
×
75
                ->withDefault('male')
×
76
                ->withSchema(['type' => 'object', 'default' => 'male', 'example' => 'male'])
×
77
        );
×
78

79
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
80
        $resourceClassResolverProphecy->isResourceClass(NotAResource::class)->willReturn(false);
×
81

NEW
82
        $definitionNameFactory = new DefinitionNameFactory();
×
83

84
        $schemaFactory = new SchemaFactory(
×
85
            resourceMetadataFactory: $resourceMetadataFactoryProphecy->reveal(),
×
86
            propertyNameCollectionFactory: $propertyNameCollectionFactoryProphecy->reveal(),
×
87
            propertyMetadataFactory: $propertyMetadataFactoryProphecy->reveal(),
×
88
            resourceClassResolver: $resourceClassResolverProphecy->reveal(),
×
89
            definitionNameFactory: $definitionNameFactory,
×
90
        );
×
91
        $resultSchema = $schemaFactory->buildSchema(NotAResource::class);
×
92

93
        $rootDefinitionKey = $resultSchema->getRootDefinitionKey();
×
94
        $definitions = $resultSchema->getDefinitions();
×
95

96
        $this->assertSame((new \ReflectionClass(NotAResource::class))->getShortName(), $rootDefinitionKey);
×
97
        $this->assertTrue(isset($definitions[$rootDefinitionKey]));
×
98
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]);
×
99
        $this->assertSame('object', $definitions[$rootDefinitionKey]['type']);
×
100
        $this->assertArrayNotHasKey('additionalProperties', $definitions[$rootDefinitionKey]);
×
101
        $this->assertArrayHasKey('properties', $definitions[$rootDefinitionKey]);
×
102
        $this->assertArrayHasKey('foo', $definitions[$rootDefinitionKey]['properties']);
×
103
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['foo']);
×
104
        $this->assertArrayNotHasKey('default', $definitions[$rootDefinitionKey]['properties']['foo']);
×
105
        $this->assertArrayNotHasKey('example', $definitions[$rootDefinitionKey]['properties']['foo']);
×
106
        $this->assertSame('string', $definitions[$rootDefinitionKey]['properties']['foo']['type']);
×
107
        $this->assertArrayHasKey('bar', $definitions[$rootDefinitionKey]['properties']);
×
108
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['bar']);
×
109
        $this->assertArrayHasKey('default', $definitions[$rootDefinitionKey]['properties']['bar']);
×
110
        $this->assertArrayHasKey('example', $definitions[$rootDefinitionKey]['properties']['bar']);
×
111
        $this->assertSame('integer', $definitions[$rootDefinitionKey]['properties']['bar']['type']);
×
112
        $this->assertSame('default_bar', $definitions[$rootDefinitionKey]['properties']['bar']['default']);
×
113
        $this->assertSame('example_bar', $definitions[$rootDefinitionKey]['properties']['bar']['example']);
×
114

115
        $this->assertArrayHasKey('genderType', $definitions[$rootDefinitionKey]['properties']);
×
116
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['genderType']);
×
117
        $this->assertArrayHasKey('default', $definitions[$rootDefinitionKey]['properties']['genderType']);
×
118
        $this->assertArrayHasKey('example', $definitions[$rootDefinitionKey]['properties']['genderType']);
×
119
        $this->assertSame('object', $definitions[$rootDefinitionKey]['properties']['genderType']['type']);
×
120
        $this->assertSame('male', $definitions[$rootDefinitionKey]['properties']['genderType']['default']);
×
121
        $this->assertSame('male', $definitions[$rootDefinitionKey]['properties']['genderType']['example']);
×
122
    }
123

124
    public function testBuildSchemaForNonResourceClass(): void
125
    {
126
        $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
127

128
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
129
        $propertyNameCollectionFactoryProphecy->create(NotAResource::class, Argument::cetera())->willReturn(new PropertyNameCollection(['foo', 'bar', 'genderType']));
×
130

131
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
132
        $propertyMetadataFactoryProphecy->create(NotAResource::class, 'foo', Argument::cetera())->willReturn(
×
133
            (new ApiProperty())
×
134
                ->withNativeType(Type::string())
×
135
                ->withReadable(true)
×
136
                ->withSchema(['type' => 'string'])
×
137
        );
×
138
        $propertyMetadataFactoryProphecy->create(NotAResource::class, 'bar', Argument::cetera())->willReturn(
×
139
            (new ApiProperty())
×
140
                ->withNativeType(Type::int())
×
141
                ->withReadable(true)
×
142
                ->withDefault('default_bar')
×
143
                ->withExample('example_bar')
×
144
                ->withSchema(['type' => 'integer', 'default' => 'default_bar', 'example' => 'example_bar'])
×
145
        );
×
146
        $propertyMetadataFactoryProphecy->create(NotAResource::class, 'genderType', Argument::cetera())->willReturn(
×
147
            (new ApiProperty())
×
148
                ->withNativeType(Type::object())
×
149
                ->withReadable(true)
×
150
                ->withDefault('male')
×
151
                ->withSchema(['type' => 'object', 'default' => 'male', 'example' => 'male'])
×
152
        );
×
153

154
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
155
        $resourceClassResolverProphecy->isResourceClass(NotAResource::class)->willReturn(false);
×
156

NEW
157
        $definitionNameFactory = new DefinitionNameFactory();
×
158

159
        $schemaFactory = new SchemaFactory(
×
160
            resourceMetadataFactory: $resourceMetadataFactoryProphecy->reveal(),
×
161
            propertyNameCollectionFactory: $propertyNameCollectionFactoryProphecy->reveal(),
×
162
            propertyMetadataFactory: $propertyMetadataFactoryProphecy->reveal(),
×
163
            resourceClassResolver: $resourceClassResolverProphecy->reveal(),
×
164
            definitionNameFactory: $definitionNameFactory,
×
165
        );
×
166
        $resultSchema = $schemaFactory->buildSchema(NotAResource::class);
×
167

168
        $rootDefinitionKey = $resultSchema->getRootDefinitionKey();
×
169
        $definitions = $resultSchema->getDefinitions();
×
170

171
        $this->assertSame((new \ReflectionClass(NotAResource::class))->getShortName(), $rootDefinitionKey);
×
172
        $this->assertTrue(isset($definitions[$rootDefinitionKey]));
×
173
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]);
×
174
        $this->assertSame('object', $definitions[$rootDefinitionKey]['type']);
×
175
        $this->assertArrayNotHasKey('additionalProperties', $definitions[$rootDefinitionKey]);
×
176
        $this->assertArrayHasKey('properties', $definitions[$rootDefinitionKey]);
×
177
        $this->assertArrayHasKey('foo', $definitions[$rootDefinitionKey]['properties']);
×
178
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['foo']);
×
179
        $this->assertArrayNotHasKey('default', $definitions[$rootDefinitionKey]['properties']['foo']);
×
180
        $this->assertArrayNotHasKey('example', $definitions[$rootDefinitionKey]['properties']['foo']);
×
181
        $this->assertSame('string', $definitions[$rootDefinitionKey]['properties']['foo']['type']);
×
182
        $this->assertArrayHasKey('bar', $definitions[$rootDefinitionKey]['properties']);
×
183
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['bar']);
×
184
        $this->assertArrayHasKey('default', $definitions[$rootDefinitionKey]['properties']['bar']);
×
185
        $this->assertArrayHasKey('example', $definitions[$rootDefinitionKey]['properties']['bar']);
×
186
        $this->assertSame('integer', $definitions[$rootDefinitionKey]['properties']['bar']['type']);
×
187
        $this->assertSame('default_bar', $definitions[$rootDefinitionKey]['properties']['bar']['default']);
×
188
        $this->assertSame('example_bar', $definitions[$rootDefinitionKey]['properties']['bar']['example']);
×
189

190
        $this->assertArrayHasKey('genderType', $definitions[$rootDefinitionKey]['properties']);
×
191
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['genderType']);
×
192
        $this->assertArrayHasKey('default', $definitions[$rootDefinitionKey]['properties']['genderType']);
×
193
        $this->assertArrayHasKey('example', $definitions[$rootDefinitionKey]['properties']['genderType']);
×
194
        $this->assertSame('object', $definitions[$rootDefinitionKey]['properties']['genderType']['type']);
×
195
        $this->assertSame('male', $definitions[$rootDefinitionKey]['properties']['genderType']['default']);
×
196
        $this->assertSame('male', $definitions[$rootDefinitionKey]['properties']['genderType']['example']);
×
197
    }
198

199
    #[IgnoreDeprecations]
200
    public function testBuildSchemaForNonResourceClassWithUnionIntersectTypesLegacy(): void
201
    {
202
        $this->expectUserDeprecationMessage('Since api-platform/metadata 4.2: The "ApiPlatform\Metadata\ApiProperty::withBuiltinTypes()" method is deprecated, use "ApiPlatform\Metadata\ApiProperty::withNativeType()" instead.');
×
203
        $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
204

205
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
206
        $propertyNameCollectionFactoryProphecy->create(NotAResourceWithUnionIntersectTypes::class, Argument::cetera())->willReturn(new PropertyNameCollection(['ignoredProperty', 'unionType', 'intersectType']));
×
207

208
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
209
        $propertyMetadataFactoryProphecy->create(NotAResourceWithUnionIntersectTypes::class, 'ignoredProperty', Argument::cetera())->willReturn(
×
210
            (new ApiProperty())
×
211
                ->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING, nullable: true)])
×
212
                ->withReadable(true)
×
213
                ->withSchema(['type' => ['string', 'null']])
×
214
        );
×
215
        $propertyMetadataFactoryProphecy->create(NotAResourceWithUnionIntersectTypes::class, 'unionType', Argument::cetera())->willReturn(
×
216
            (new ApiProperty())
×
217
                ->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING, nullable: true), new LegacyType(LegacyType::BUILTIN_TYPE_INT, nullable: true), new LegacyType(LegacyType::BUILTIN_TYPE_FLOAT, nullable: true)])
×
218
                ->withReadable(true)
×
219
                ->withSchema(['oneOf' => [
×
220
                    ['type' => ['string', 'null']],
×
221
                    ['type' => ['integer', 'null']],
×
222
                ]])
×
223
        );
×
224
        $propertyMetadataFactoryProphecy->create(NotAResourceWithUnionIntersectTypes::class, 'intersectType', Argument::cetera())->willReturn(
×
225
            (new ApiProperty())
×
226
                ->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, class: Serializable::class), new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, class: DummyResourceInterface::class)])
×
227
                ->withReadable(true)
×
228
                ->withSchema(['type' => 'object'])
×
229
        );
×
230

231
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
232
        $resourceClassResolverProphecy->isResourceClass(NotAResourceWithUnionIntersectTypes::class)->willReturn(false);
×
233

NEW
234
        $definitionNameFactory = new DefinitionNameFactory();
×
235

236
        $schemaFactory = new SchemaFactory(
×
237
            resourceMetadataFactory: $resourceMetadataFactoryProphecy->reveal(),
×
238
            propertyNameCollectionFactory: $propertyNameCollectionFactoryProphecy->reveal(),
×
239
            propertyMetadataFactory: $propertyMetadataFactoryProphecy->reveal(),
×
240
            resourceClassResolver: $resourceClassResolverProphecy->reveal(),
×
241
            definitionNameFactory: $definitionNameFactory,
×
242
        );
×
243
        $resultSchema = $schemaFactory->buildSchema(NotAResourceWithUnionIntersectTypes::class);
×
244

245
        $rootDefinitionKey = $resultSchema->getRootDefinitionKey();
×
246
        $definitions = $resultSchema->getDefinitions();
×
247

248
        $this->assertSame((new \ReflectionClass(NotAResourceWithUnionIntersectTypes::class))->getShortName(), $rootDefinitionKey);
×
249
        $this->assertTrue(isset($definitions[$rootDefinitionKey]));
×
250
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]);
×
251
        $this->assertSame('object', $definitions[$rootDefinitionKey]['type']);
×
252
        $this->assertArrayNotHasKey('additionalProperties', $definitions[$rootDefinitionKey]);
×
253
        $this->assertArrayHasKey('properties', $definitions[$rootDefinitionKey]);
×
254

255
        $this->assertArrayHasKey('ignoredProperty', $definitions[$rootDefinitionKey]['properties']);
×
256
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['ignoredProperty']);
×
257
        $this->assertSame(['string', 'null'], $definitions[$rootDefinitionKey]['properties']['ignoredProperty']['type']);
×
258

259
        $this->assertArrayHasKey('unionType', $definitions[$rootDefinitionKey]['properties']);
×
260
        $this->assertArrayHasKey('oneOf', $definitions[$rootDefinitionKey]['properties']['unionType']);
×
261
        $this->assertCount(2, $definitions[$rootDefinitionKey]['properties']['unionType']['oneOf']);
×
262
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['unionType']['oneOf'][0]);
×
263
        $this->assertSame(['string', 'null'], $definitions[$rootDefinitionKey]['properties']['unionType']['oneOf'][0]['type']);
×
264
        $this->assertSame(['integer', 'null'], $definitions[$rootDefinitionKey]['properties']['unionType']['oneOf'][1]['type']);
×
265

266
        $this->assertArrayHasKey('intersectType', $definitions[$rootDefinitionKey]['properties']);
×
267
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['intersectType']);
×
268
        $this->assertSame('object', $definitions[$rootDefinitionKey]['properties']['intersectType']['type']);
×
269
    }
270

271
    public function testBuildSchemaForNonResourceClassWithUnionIntersectTypes(): void
272
    {
273
        $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
274

275
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
276
        $propertyNameCollectionFactoryProphecy->create(NotAResourceWithUnionIntersectTypes::class, Argument::cetera())->willReturn(new PropertyNameCollection(['ignoredProperty', 'unionType', 'intersectType']));
×
277

278
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
279
        $propertyMetadataFactoryProphecy->create(NotAResourceWithUnionIntersectTypes::class, 'ignoredProperty', Argument::cetera())->willReturn(
×
280
            (new ApiProperty())
×
281
                ->withNativeType(Type::nullable(Type::string())) // @phpstan-ignore-line
×
282
                ->withReadable(true)
×
283
                ->withSchema(['type' => ['string', 'null']])
×
284
        );
×
285
        $propertyMetadataFactoryProphecy->create(NotAResourceWithUnionIntersectTypes::class, 'unionType', Argument::cetera())->willReturn(
×
286
            (new ApiProperty())
×
287
                ->withNativeType(Type::union(Type::string(), Type::int(), Type::float(), Type::null()))
×
288
                ->withReadable(true)
×
289
                ->withSchema(['oneOf' => [
×
290
                    ['type' => ['string', 'null']],
×
291
                    ['type' => ['integer', 'null']],
×
292
                ]])
×
293
        );
×
294
        $propertyMetadataFactoryProphecy->create(NotAResourceWithUnionIntersectTypes::class, 'intersectType', Argument::cetera())->willReturn(
×
295
            (new ApiProperty())
×
296
                ->withNativeType(Type::intersection(Type::object(Serializable::class), Type::object(DummyResourceInterface::class)))
×
297
                ->withReadable(true)
×
298
                ->withSchema(['type' => 'object'])
×
299
        );
×
300

301
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
302
        $resourceClassResolverProphecy->isResourceClass(NotAResourceWithUnionIntersectTypes::class)->willReturn(false);
×
303

NEW
304
        $definitionNameFactory = new DefinitionNameFactory();
×
305

306
        $schemaFactory = new SchemaFactory(
×
307
            resourceMetadataFactory: $resourceMetadataFactoryProphecy->reveal(),
×
308
            propertyNameCollectionFactory: $propertyNameCollectionFactoryProphecy->reveal(),
×
309
            propertyMetadataFactory: $propertyMetadataFactoryProphecy->reveal(),
×
310
            resourceClassResolver: $resourceClassResolverProphecy->reveal(),
×
311
            definitionNameFactory: $definitionNameFactory,
×
312
        );
×
313
        $resultSchema = $schemaFactory->buildSchema(NotAResourceWithUnionIntersectTypes::class);
×
314

315
        $rootDefinitionKey = $resultSchema->getRootDefinitionKey();
×
316
        $definitions = $resultSchema->getDefinitions();
×
317

318
        $this->assertSame((new \ReflectionClass(NotAResourceWithUnionIntersectTypes::class))->getShortName(), $rootDefinitionKey);
×
319
        $this->assertTrue(isset($definitions[$rootDefinitionKey]));
×
320
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]);
×
321
        $this->assertSame('object', $definitions[$rootDefinitionKey]['type']);
×
322
        $this->assertArrayNotHasKey('additionalProperties', $definitions[$rootDefinitionKey]);
×
323
        $this->assertArrayHasKey('properties', $definitions[$rootDefinitionKey]);
×
324

325
        $this->assertArrayHasKey('ignoredProperty', $definitions[$rootDefinitionKey]['properties']);
×
326
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['ignoredProperty']);
×
327
        $this->assertSame(['string', 'null'], $definitions[$rootDefinitionKey]['properties']['ignoredProperty']['type']);
×
328

329
        $this->assertArrayHasKey('unionType', $definitions[$rootDefinitionKey]['properties']);
×
330
        $this->assertArrayHasKey('oneOf', $definitions[$rootDefinitionKey]['properties']['unionType']);
×
331
        $this->assertCount(2, $definitions[$rootDefinitionKey]['properties']['unionType']['oneOf']);
×
332
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['unionType']['oneOf'][0]);
×
333
        $this->assertSame(['string', 'null'], $definitions[$rootDefinitionKey]['properties']['unionType']['oneOf'][0]['type']);
×
334
        $this->assertSame(['integer', 'null'], $definitions[$rootDefinitionKey]['properties']['unionType']['oneOf'][1]['type']);
×
335

336
        $this->assertArrayHasKey('intersectType', $definitions[$rootDefinitionKey]['properties']);
×
337
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['intersectType']);
×
338
        $this->assertSame('object', $definitions[$rootDefinitionKey]['properties']['intersectType']['type']);
×
339
    }
340

341
    #[IgnoreDeprecations]
342
    public function testBuildSchemaWithSerializerGroupsLegacy(): void
343
    {
344
        $this->expectUserDeprecationMessage('Since api-platform/metadata 4.2: The "ApiPlatform\Metadata\ApiProperty::withBuiltinTypes()" method is deprecated, use "ApiPlatform\Metadata\ApiProperty::withNativeType()" instead.');
×
345
        $shortName = (new \ReflectionClass(OverriddenOperationDummy::class))->getShortName();
×
346
        $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
347
        $operation = (new Put())->withName('put')->withNormalizationContext([
×
348
            'groups' => 'overridden_operation_dummy_put',
×
349
            AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false,
×
350
        ])->withShortName($shortName)->withValidationContext(['groups' => ['validation_groups_dummy_put']]);
×
351
        $resourceMetadataFactoryProphecy->create(OverriddenOperationDummy::class)
×
352
            ->willReturn(
×
353
                new ResourceMetadataCollection(OverriddenOperationDummy::class, [
×
354
                    (new ApiResource())->withOperations(new Operations(['put' => $operation])),
×
355
                ])
×
356
            );
×
357

358
        $serializerGroup = 'custom_operation_dummy';
×
359

360
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
361
        $propertyNameCollectionFactoryProphecy->create(OverriddenOperationDummy::class, Argument::type('array'))->willReturn(new PropertyNameCollection(['alias', 'description', 'genderType']));
×
362

363
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
364
        $propertyMetadataFactoryProphecy->create(OverriddenOperationDummy::class, 'alias', Argument::type('array'))->willReturn(
×
365
            (new ApiProperty())
×
366
                ->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])
×
367
                ->withReadable(true)
×
368
                ->withSchema(['type' => 'string'])
×
369
        );
×
370
        $propertyMetadataFactoryProphecy->create(OverriddenOperationDummy::class, 'description', Argument::type('array'))->willReturn(
×
371
            (new ApiProperty())
×
372
                ->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])
×
373
                ->withReadable(true)
×
374
                ->withSchema(['type' => 'string'])
×
375
        );
×
376
        $propertyMetadataFactoryProphecy->create(OverriddenOperationDummy::class, 'genderType', Argument::type('array'))->willReturn(
×
377
            (new ApiProperty())
×
378
                ->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, GenderTypeEnum::class)])
×
379
                ->withReadable(true)
×
380
                ->withDefault(GenderTypeEnum::MALE)
×
381
                ->withSchema(['type' => 'object'])
×
382
        );
×
383

384
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
385
        $resourceClassResolverProphecy->isResourceClass(OverriddenOperationDummy::class)->willReturn(true);
×
386
        $resourceClassResolverProphecy->isResourceClass(GenderTypeEnum::class)->willReturn(true);
×
387

NEW
388
        $definitionNameFactory = new DefinitionNameFactory();
×
389

390
        $schemaFactory = new SchemaFactory(
×
391
            resourceMetadataFactory: $resourceMetadataFactoryProphecy->reveal(),
×
392
            propertyNameCollectionFactory: $propertyNameCollectionFactoryProphecy->reveal(),
×
393
            propertyMetadataFactory: $propertyMetadataFactoryProphecy->reveal(),
×
394
            resourceClassResolver: $resourceClassResolverProphecy->reveal(),
×
395
            definitionNameFactory: $definitionNameFactory,
×
396
        );
×
397
        $resultSchema = $schemaFactory->buildSchema(OverriddenOperationDummy::class, 'json', Schema::TYPE_OUTPUT, null, null, ['groups' => $serializerGroup, AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false]);
×
398

399
        $rootDefinitionKey = $resultSchema->getRootDefinitionKey();
×
400
        $definitions = $resultSchema->getDefinitions();
×
401

402
        $this->assertSame((new \ReflectionClass(OverriddenOperationDummy::class))->getShortName().'-'.$serializerGroup, $rootDefinitionKey);
×
403
        $this->assertTrue(isset($definitions[$rootDefinitionKey]));
×
404
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]);
×
405
        $this->assertSame('object', $definitions[$rootDefinitionKey]['type']);
×
406
        $this->assertFalse($definitions[$rootDefinitionKey]['additionalProperties']);
×
407
        $this->assertArrayHasKey('properties', $definitions[$rootDefinitionKey]);
×
408
        $this->assertArrayHasKey('alias', $definitions[$rootDefinitionKey]['properties']);
×
409
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['alias']);
×
410
        $this->assertSame('string', $definitions[$rootDefinitionKey]['properties']['alias']['type']);
×
411
        $this->assertArrayHasKey('description', $definitions[$rootDefinitionKey]['properties']);
×
412
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['description']);
×
413
        $this->assertSame('string', $definitions[$rootDefinitionKey]['properties']['description']['type']);
×
414
        $this->assertArrayHasKey('genderType', $definitions[$rootDefinitionKey]['properties']);
×
415
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['genderType']);
×
416
        $this->assertArrayNotHasKey('default', $definitions[$rootDefinitionKey]['properties']['genderType']);
×
417
        $this->assertArrayNotHasKey('example', $definitions[$rootDefinitionKey]['properties']['genderType']);
×
418
        $this->assertSame('object', $definitions[$rootDefinitionKey]['properties']['genderType']['type']);
×
419
    }
420

421
    public function testBuildSchemaWithSerializerGroups(): void
422
    {
423
        $shortName = (new \ReflectionClass(OverriddenOperationDummy::class))->getShortName();
×
424
        $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
425
        $operation = (new Put())->withName('put')->withNormalizationContext([
×
426
            'groups' => 'overridden_operation_dummy_put',
×
427
            AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false,
×
428
        ])->withShortName($shortName)->withValidationContext(['groups' => ['validation_groups_dummy_put']]);
×
429
        $resourceMetadataFactoryProphecy->create(OverriddenOperationDummy::class)
×
430
            ->willReturn(
×
431
                new ResourceMetadataCollection(OverriddenOperationDummy::class, [
×
432
                    (new ApiResource())->withOperations(new Operations(['put' => $operation])),
×
433
                ])
×
434
            );
×
435

436
        $serializerGroup = 'custom_operation_dummy';
×
437

438
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
439
        $propertyNameCollectionFactoryProphecy->create(OverriddenOperationDummy::class, Argument::type('array'))->willReturn(new PropertyNameCollection(['alias', 'description', 'genderType']));
×
440

441
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
442
        $propertyMetadataFactoryProphecy->create(OverriddenOperationDummy::class, 'alias', Argument::type('array'))->willReturn(
×
443
            (new ApiProperty())
×
444
                ->withNativeType(Type::string())
×
445
                ->withReadable(true)
×
446
                ->withSchema(['type' => 'string'])
×
447
        );
×
448
        $propertyMetadataFactoryProphecy->create(OverriddenOperationDummy::class, 'description', Argument::type('array'))->willReturn(
×
449
            (new ApiProperty())
×
450
                ->withNativeType(Type::string())
×
451
                ->withReadable(true)
×
452
                ->withSchema(['type' => 'string'])
×
453
        );
×
454
        $propertyMetadataFactoryProphecy->create(OverriddenOperationDummy::class, 'genderType', Argument::type('array'))->willReturn(
×
455
            (new ApiProperty())
×
456
                ->withNativeType(Type::enum(GenderTypeEnum::class))
×
457
                ->withReadable(true)
×
458
                ->withDefault(GenderTypeEnum::MALE)
×
459
                ->withSchema(['type' => 'object'])
×
460
        );
×
461
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
462
        $resourceClassResolverProphecy->isResourceClass(OverriddenOperationDummy::class)->willReturn(true);
×
463
        $resourceClassResolverProphecy->isResourceClass(GenderTypeEnum::class)->willReturn(true);
×
464

NEW
465
        $definitionNameFactory = new DefinitionNameFactory();
×
466

467
        $schemaFactory = new SchemaFactory(
×
468
            resourceMetadataFactory: $resourceMetadataFactoryProphecy->reveal(),
×
469
            propertyNameCollectionFactory: $propertyNameCollectionFactoryProphecy->reveal(),
×
470
            propertyMetadataFactory: $propertyMetadataFactoryProphecy->reveal(),
×
471
            resourceClassResolver: $resourceClassResolverProphecy->reveal(),
×
472
            definitionNameFactory: $definitionNameFactory,
×
473
        );
×
474
        $resultSchema = $schemaFactory->buildSchema(OverriddenOperationDummy::class, 'json', Schema::TYPE_OUTPUT, null, null, ['groups' => $serializerGroup, AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false]);
×
475

476
        $rootDefinitionKey = $resultSchema->getRootDefinitionKey();
×
477
        $definitions = $resultSchema->getDefinitions();
×
478

479
        $this->assertSame((new \ReflectionClass(OverriddenOperationDummy::class))->getShortName().'-'.$serializerGroup, $rootDefinitionKey);
×
480
        $this->assertTrue(isset($definitions[$rootDefinitionKey]));
×
481
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]);
×
482
        $this->assertSame('object', $definitions[$rootDefinitionKey]['type']);
×
483
        $this->assertFalse($definitions[$rootDefinitionKey]['additionalProperties']);
×
484
        $this->assertArrayHasKey('properties', $definitions[$rootDefinitionKey]);
×
485
        $this->assertArrayHasKey('alias', $definitions[$rootDefinitionKey]['properties']);
×
486
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['alias']);
×
487
        $this->assertSame('string', $definitions[$rootDefinitionKey]['properties']['alias']['type']);
×
488
        $this->assertArrayHasKey('description', $definitions[$rootDefinitionKey]['properties']);
×
489
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['description']);
×
490
        $this->assertSame('string', $definitions[$rootDefinitionKey]['properties']['description']['type']);
×
491
        $this->assertArrayHasKey('genderType', $definitions[$rootDefinitionKey]['properties']);
×
492
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['genderType']);
×
493
        $this->assertArrayNotHasKey('default', $definitions[$rootDefinitionKey]['properties']['genderType']);
×
494
        $this->assertArrayNotHasKey('example', $definitions[$rootDefinitionKey]['properties']['genderType']);
×
495
        $this->assertSame('object', $definitions[$rootDefinitionKey]['properties']['genderType']['type']);
×
496
    }
497

498
    #[IgnoreDeprecations]
499
    public function testBuildSchemaForAssociativeArrayLegacy(): void
500
    {
501
        $this->expectUserDeprecationMessage('Since api-platform/metadata 4.2: The "ApiPlatform\Metadata\ApiProperty::withBuiltinTypes()" method is deprecated, use "ApiPlatform\Metadata\ApiProperty::withNativeType()" instead.');
×
502
        $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
503

504
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
505
        $propertyNameCollectionFactoryProphecy->create(NotAResource::class, Argument::cetera())->willReturn(new PropertyNameCollection(['foo', 'bar']));
×
506

507
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
508
        $propertyMetadataFactoryProphecy->create(NotAResource::class, 'foo', Argument::cetera())->willReturn(
×
509
            (new ApiProperty())
×
510
                ->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_ARRAY, false, null, true, new LegacyType(LegacyType::BUILTIN_TYPE_INT), new LegacyType(LegacyType::BUILTIN_TYPE_STRING))])
×
511
                ->withReadable(true)
×
512
                ->withSchema(['type' => 'array', 'items' => ['string', 'int']])
×
513
        );
×
514
        $propertyMetadataFactoryProphecy->create(NotAResource::class, 'bar', Argument::cetera())->willReturn(
×
515
            (new ApiProperty())
×
516
                ->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_ARRAY, false, null, true, new LegacyType(LegacyType::BUILTIN_TYPE_STRING), new LegacyType(LegacyType::BUILTIN_TYPE_STRING))])
×
517
                ->withReadable(true)
×
518
                ->withSchema(['type' => 'object', 'additionalProperties' => 'string'])
×
519
        );
×
520

521
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
522
        $resourceClassResolverProphecy->isResourceClass(NotAResource::class)->willReturn(false);
×
523

NEW
524
        $definitionNameFactory = new DefinitionNameFactory();
×
525

526
        $schemaFactory = new SchemaFactory(
×
527
            resourceMetadataFactory: $resourceMetadataFactoryProphecy->reveal(),
×
528
            propertyNameCollectionFactory: $propertyNameCollectionFactoryProphecy->reveal(),
×
529
            propertyMetadataFactory: $propertyMetadataFactoryProphecy->reveal(),
×
530
            resourceClassResolver: $resourceClassResolverProphecy->reveal(),
×
531
            definitionNameFactory: $definitionNameFactory,
×
532
        );
×
533
        $resultSchema = $schemaFactory->buildSchema(NotAResource::class);
×
534

535
        $rootDefinitionKey = $resultSchema->getRootDefinitionKey();
×
536
        $definitions = $resultSchema->getDefinitions();
×
537

538
        $this->assertSame((new \ReflectionClass(NotAResource::class))->getShortName(), $rootDefinitionKey);
×
539
        $this->assertTrue(isset($definitions[$rootDefinitionKey]));
×
540
        $this->assertArrayHasKey('properties', $definitions[$rootDefinitionKey]);
×
541
        $this->assertArrayHasKey('foo', $definitions[$rootDefinitionKey]['properties']);
×
542
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['foo']);
×
543
        $this->assertArrayNotHasKey('additionalProperties', $definitions[$rootDefinitionKey]['properties']['foo']);
×
544
        $this->assertSame('array', $definitions[$rootDefinitionKey]['properties']['foo']['type']);
×
545
        $this->assertArrayHasKey('bar', $definitions[$rootDefinitionKey]['properties']);
×
546
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['bar']);
×
547
        $this->assertArrayHasKey('additionalProperties', $definitions[$rootDefinitionKey]['properties']['bar']);
×
548
        $this->assertSame('object', $definitions[$rootDefinitionKey]['properties']['bar']['type']);
×
549
        $this->assertSame('string', $definitions[$rootDefinitionKey]['properties']['bar']['additionalProperties']);
×
550
    }
551

552
    public function testBuildSchemaForAssociativeArray(): void
553
    {
554
        $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
555

556
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
557
        $propertyNameCollectionFactoryProphecy->create(NotAResource::class, Argument::cetera())->willReturn(new PropertyNameCollection(['foo', 'bar']));
×
558

559
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
560
        $propertyMetadataFactoryProphecy->create(NotAResource::class, 'foo', Argument::cetera())->willReturn(
×
561
            (new ApiProperty())
×
562
                ->withNativeType(Type::list(Type::string()))
×
563
                ->withReadable(true)
×
564
                ->withSchema(['type' => 'array', 'items' => ['string', 'int']])
×
565
        );
×
566
        $propertyMetadataFactoryProphecy->create(NotAResource::class, 'bar', Argument::cetera())->willReturn(
×
567
            (new ApiProperty())
×
568
                ->withNativeType(Type::dict(Type::string()))
×
569
                ->withReadable(true)
×
570
                ->withSchema(['type' => 'object', 'additionalProperties' => 'string'])
×
571
        );
×
572

573
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
574
        $resourceClassResolverProphecy->isResourceClass(NotAResource::class)->willReturn(false);
×
575

NEW
576
        $definitionNameFactory = new DefinitionNameFactory();
×
577

578
        $schemaFactory = new SchemaFactory(
×
579
            resourceMetadataFactory: $resourceMetadataFactoryProphecy->reveal(),
×
580
            propertyNameCollectionFactory: $propertyNameCollectionFactoryProphecy->reveal(),
×
581
            propertyMetadataFactory: $propertyMetadataFactoryProphecy->reveal(),
×
582
            resourceClassResolver: $resourceClassResolverProphecy->reveal(),
×
583
            definitionNameFactory: $definitionNameFactory,
×
584
        );
×
585
        $resultSchema = $schemaFactory->buildSchema(NotAResource::class);
×
586

587
        $rootDefinitionKey = $resultSchema->getRootDefinitionKey();
×
588
        $definitions = $resultSchema->getDefinitions();
×
589

590
        $this->assertSame((new \ReflectionClass(NotAResource::class))->getShortName(), $rootDefinitionKey);
×
591
        $this->assertTrue(isset($definitions[$rootDefinitionKey]));
×
592
        $this->assertArrayHasKey('properties', $definitions[$rootDefinitionKey]);
×
593
        $this->assertArrayHasKey('foo', $definitions[$rootDefinitionKey]['properties']);
×
594
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['foo']);
×
595
        $this->assertArrayNotHasKey('additionalProperties', $definitions[$rootDefinitionKey]['properties']['foo']);
×
596
        $this->assertSame('array', $definitions[$rootDefinitionKey]['properties']['foo']['type']);
×
597
        $this->assertArrayHasKey('bar', $definitions[$rootDefinitionKey]['properties']);
×
598
        $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['bar']);
×
599
        $this->assertArrayHasKey('additionalProperties', $definitions[$rootDefinitionKey]['properties']['bar']);
×
600
        $this->assertSame('object', $definitions[$rootDefinitionKey]['properties']['bar']['type']);
×
601
        $this->assertSame('string', $definitions[$rootDefinitionKey]['properties']['bar']['additionalProperties']);
×
602
    }
603
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc