Skip to content

Commit 347154b

Browse files
committed
fix: šŸ› fallback the schema for mixed properties to string|null instead of null
1 parent aea8113 commit 347154b

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

ā€Žsrc/JsonSchema/Metadata/Property/Factory/SchemaPropertyMetadataFactory.phpā€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ private function getJsonSchemaFromType(Type $type, ?bool $readableLink = null):
280280
TypeIdentifier::OBJECT => ['type' => 'object'],
281281
TypeIdentifier::RESOURCE => ['type' => 'string'],
282282
TypeIdentifier::CALLABLE => ['type' => 'string'],
283+
TypeIdentifier::MIXED => ['type' => 'string'],
283284
default => ['type' => 'null'],
284285
};
285286

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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\Fixtures;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
18+
#[ApiResource]
19+
class DummyWithMixed
20+
{
21+
public mixed $mixedProperty;
22+
public array $mixedArrayProperty;
23+
}

ā€Žsrc/JsonSchema/Tests/Metadata/Property/Factory/SchemaPropertyMetadataFactoryTest.phpā€Ž

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\JsonSchema\Metadata\Property\Factory\SchemaPropertyMetadataFactory;
1717
use ApiPlatform\JsonSchema\Tests\Fixtures\DummyWithCustomOpenApiContext;
1818
use ApiPlatform\JsonSchema\Tests\Fixtures\DummyWithEnum;
19+
use ApiPlatform\JsonSchema\Tests\Fixtures\DummyWithMixed;
1920
use ApiPlatform\JsonSchema\Tests\Fixtures\DummyWithUnionTypeProperty;
2021
use ApiPlatform\JsonSchema\Tests\Fixtures\Enum\IntEnumAsIdentifier;
2122
use ApiPlatform\Metadata\ApiProperty;
@@ -167,4 +168,33 @@ public function testUnionTypeAnyOfIsArray(): void
167168

168169
$this->assertEquals($expectedSchema, $apiProperty->getSchema());
169170
}
171+
172+
public function testMixed(): void
173+
{
174+
$resourceClassResolver = $this->createMock(ResourceClassResolverInterface::class);
175+
$apiProperty = new ApiProperty(nativeType: Type::mixed());
176+
$decorated = $this->createMock(PropertyMetadataFactoryInterface::class);
177+
$decorated->expects($this->once())->method('create')->with(DummyWithMixed::class, 'mixedProperty')->willReturn($apiProperty);
178+
179+
$schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory($resourceClassResolver, $decorated);
180+
$apiProperty = $schemaPropertyMetadataFactory->create(DummyWithMixed::class, 'mixedProperty');
181+
182+
$this->assertEquals([
183+
'type' => ['string', 'null'],
184+
], $apiProperty->getSchema());
185+
186+
$apiProperty = new ApiProperty(nativeType: Type::array(Type::mixed()));
187+
$decorated = $this->createMock(PropertyMetadataFactoryInterface::class);
188+
$decorated->expects($this->once())->method('create')->with(DummyWithMixed::class, 'mixedArrayProperty')->willReturn($apiProperty);
189+
190+
$schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory($resourceClassResolver, $decorated);
191+
$apiProperty = $schemaPropertyMetadataFactory->create(DummyWithMixed::class, 'mixedArrayProperty');
192+
193+
$this->assertEquals([
194+
'type' => 'array',
195+
'items' => [
196+
'type' => ['string', 'null'],
197+
],
198+
], $apiProperty->getSchema());
199+
}
170200
}

0 commit comments

Comments
Ā (0)