Skip to content

Commit e953cc9

Browse files
committed
fix(metadata): wrong schema generated if openapicontext set on array
1 parent 99314bf commit e953cc9

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

src/JsonSchema/Metadata/Property/Factory/SchemaPropertyMetadataFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,10 @@ public function create(string $resourceClass, string $property, array $options =
100100
$propertySchema['example'] = $propertySchema['default'];
101101
}
102102

103-
// never override the following keys if at least one is already set
103+
// never override the following keys if at least one is already set or if there's a custom openapi context
104104
if ([] === $types
105105
|| ($propertySchema['type'] ?? $propertySchema['$ref'] ?? $propertySchema['anyOf'] ?? $propertySchema['allOf'] ?? $propertySchema['oneOf'] ?? false)
106+
|| ($propertyMetadata->getOpenapiContext() ?? false)
106107
) {
107108
return $propertyMetadata->withSchema($propertySchema);
108109
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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\Fixtures;
15+
16+
use ApiPlatform\JsonSchema\Tests\Fixtures\Enum\IntEnumAsIdentifier;
17+
use ApiPlatform\Metadata\ApiProperty;
18+
use ApiPlatform\Metadata\ApiResource;
19+
20+
/*
21+
* This file is part of the API Platform project.
22+
*
23+
* (c) Kévin Dunglas <[email protected]>
24+
*
25+
* For the full copyright and license information, please view the LICENSE
26+
* file that was distributed with this source code.
27+
*/
28+
29+
#[ApiResource]
30+
class DummyWithCustomOpenApiContext
31+
{
32+
#[ApiProperty(['type' => 'object', 'properties' => ['alpha' => ['type' => 'integer']]])]
33+
public $acme;
34+
}

src/JsonSchema/Tests/Metadata/Property/Factory/SchemaPropertyMetadataFactoryTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\JsonSchema\Tests\Metadata\Property\Factory;
1515

1616
use ApiPlatform\JsonSchema\Metadata\Property\Factory\SchemaPropertyMetadataFactory;
17+
use ApiPlatform\JsonSchema\Tests\Fixtures\DummyWithCustomOpenApiContext;
1718
use ApiPlatform\JsonSchema\Tests\Fixtures\DummyWithEnum;
1819
use ApiPlatform\JsonSchema\Tests\Fixtures\Enum\IntEnumAsIdentifier;
1920
use ApiPlatform\Metadata\ApiProperty;
@@ -34,4 +35,18 @@ public function testEnum(): void
3435
$apiProperty = $schemaPropertyMetadataFactory->create(DummyWithEnum::class, 'intEnumAsIdentifier');
3536
$this->assertEquals(['type' => ['integer', 'null'], 'enum' => [1, 2, null]], $apiProperty->getSchema());
3637
}
38+
39+
public function testWithCustomOpenApiContext(): void
40+
{
41+
$resourceClassResolver = $this->createMock(ResourceClassResolverInterface::class);
42+
$apiProperty = new ApiProperty(
43+
builtinTypes: [new Type(builtinType: 'object', nullable: true, class: IntEnumAsIdentifier::class)],
44+
openapiContext: ['type' => 'object', 'properties' => ['alpha' => ['type' => 'integer']]],
45+
);
46+
$decorated = $this->createMock(PropertyMetadataFactoryInterface::class);
47+
$decorated->expects($this->once())->method('create')->with(DummyWithCustomOpenApiContext::class, 'acme')->willReturn($apiProperty);
48+
$schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory($resourceClassResolver, $decorated);
49+
$apiProperty = $schemaPropertyMetadataFactory->create(DummyWithCustomOpenApiContext::class, 'acme');
50+
$this->assertEquals([], $apiProperty->getSchema());
51+
}
3752
}

0 commit comments

Comments
 (0)