Skip to content

Commit a83c118

Browse files
aaa2000soyuka
authored andcommitted
Use the Doctrine default value as default value in Swagger doc
1 parent 39987fc commit a83c118

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/Bridge/Doctrine/Orm/Metadata/Property/DoctrineOrmPropertyMetadataFactory.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ public function create(string $resourceClass, string $property, array $options =
7676
$propertyMetadata = $propertyMetadata->withIdentifier(false);
7777
}
7878

79+
if ($doctrineClassMetadata instanceof ClassMetadataInfo && \in_array($property, $doctrineClassMetadata->getFieldNames(), true)) {
80+
$fieldMapping = $doctrineClassMetadata->getFieldMapping($property);
81+
if (\array_key_exists('options', $fieldMapping) && \array_key_exists('default', $fieldMapping['options'])) {
82+
$propertyMetadata = $propertyMetadata->withDefault($fieldMapping['options']['default']);
83+
}
84+
}
85+
7986
return $propertyMetadata;
8087
}
8188
}

tests/Bridge/Doctrine/Orm/Metadata/Property/DoctrineOrmPropertyMetadataFactoryTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
1818
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
1919
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
20+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyPropertyWithDefaultValue;
2021
use Doctrine\Common\Persistence\ManagerRegistry;
2122
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
2223
use Doctrine\Common\Persistence\ObjectManager;
@@ -73,6 +74,7 @@ public function testCreateIsWritable()
7374

7475
$classMetadata = $this->prophesize(ClassMetadataInfo::class);
7576
$classMetadata->getIdentifier()->shouldBeCalled()->willReturn(['id']);
77+
$classMetadata->getFieldNames()->shouldBeCalled()->willReturn([]);
7678

7779
$objectManager = $this->prophesize(ObjectManager::class);
7880
$objectManager->getClassMetadata(Dummy::class)->shouldBeCalled()->willReturn($classMetadata->reveal());
@@ -88,6 +90,31 @@ public function testCreateIsWritable()
8890
$this->assertEquals($doctrinePropertyMetadata->isWritable(), false);
8991
}
9092

93+
public function testCreateWithDefaultOption()
94+
{
95+
$propertyMetadata = new PropertyMetadata();
96+
97+
$propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
98+
$propertyMetadataFactory->create(DummyPropertyWithDefaultValue::class, 'dummyDefaultOption', [])->shouldBeCalled()->willReturn($propertyMetadata);
99+
100+
$classMetadata = new ClassMetadataInfo(DummyPropertyWithDefaultValue::class);
101+
$classMetadata->fieldMappings = [
102+
'dummyDefaultOption' => ['options' => ['default' => 'default value']],
103+
];
104+
105+
$objectManager = $this->prophesize(ObjectManager::class);
106+
$objectManager->getClassMetadata(DummyPropertyWithDefaultValue::class)->shouldBeCalled()->willReturn($classMetadata);
107+
108+
$managerRegistry = $this->prophesize(ManagerRegistry::class);
109+
$managerRegistry->getManagerForClass(DummyPropertyWithDefaultValue::class)->shouldBeCalled()->willReturn($objectManager->reveal());
110+
111+
$doctrineOrmPropertyMetadataFactory = new DoctrineOrmPropertyMetadataFactory($managerRegistry->reveal(), $propertyMetadataFactory->reveal());
112+
113+
$doctrinePropertyMetadata = $doctrineOrmPropertyMetadataFactory->create(DummyPropertyWithDefaultValue::class, 'dummyDefaultOption');
114+
115+
$this->assertEquals($doctrinePropertyMetadata->getDefault(), 'default value');
116+
}
117+
91118
public function testCreateClassMetadataInfo()
92119
{
93120
$propertyMetadata = new PropertyMetadata();
@@ -98,6 +125,7 @@ public function testCreateClassMetadataInfo()
98125
$classMetadata = $this->prophesize(ClassMetadataInfo::class);
99126
$classMetadata->getIdentifier()->shouldBeCalled()->willReturn(['id']);
100127
$classMetadata->isIdentifierNatural()->shouldBeCalled()->willReturn(true);
128+
$classMetadata->getFieldNames()->shouldBeCalled()->willReturn([]);
101129

102130
$objectManager = $this->prophesize(ObjectManager::class);
103131
$objectManager->getClassMetadata(Dummy::class)->shouldBeCalled()->willReturn($classMetadata->reveal());

tests/Fixtures/TestBundle/Entity/DummyPropertyWithDefaultValue.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ class DummyPropertyWithDefaultValue
4949
*/
5050
public $foo = 'foo';
5151

52+
/**
53+
* @var string A dummy with a Doctrine default options
54+
*
55+
* @ORM\Column(options={"default"="default value"})
56+
*/
57+
public $dummyDefaultOption;
58+
5259
/**
5360
* @return int
5461
*/

0 commit comments

Comments
 (0)