Skip to content

Commit 0ed1b63

Browse files
authored
fix(metadata): wrong schema generated if openapicontext set on array (#6431)
1 parent e32a4c7 commit 0ed1b63

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-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,33 @@
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\Metadata\ApiProperty;
17+
use ApiPlatform\Metadata\ApiResource;
18+
19+
/*
20+
* This file is part of the API Platform project.
21+
*
22+
* (c) Kévin Dunglas <[email protected]>
23+
*
24+
* For the full copyright and license information, please view the LICENSE
25+
* file that was distributed with this source code.
26+
*/
27+
28+
#[ApiResource]
29+
class DummyWithCustomOpenApiContext
30+
{
31+
#[ApiProperty(openapiContext: ['type' => 'object', 'properties' => ['alpha' => ['type' => 'integer']]])]
32+
public $acme;
33+
}

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)