Skip to content

Commit 4309ffe

Browse files
committed
in swagger mark property as readOnly when is not writable and initializable
1 parent e1733fd commit 4309ffe

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/Swagger/Serializer/DocumentationNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ private function getPropertySchema(PropertyMetadata $propertyMetadata, \ArrayObj
445445
{
446446
$propertySchema = new \ArrayObject($propertyMetadata->getAttributes()['swagger_context'] ?? []);
447447

448-
if (false === $propertyMetadata->isWritable()) {
448+
if (false === $propertyMetadata->isWritable() && !$propertyMetadata->isInitializable()) {
449449
$propertySchema['readOnly'] = true;
450450
}
451451

tests/Swagger/Serializer/DocumentationNormalizerTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function testNormalize()
7979
$documentation = new Documentation(new ResourceNameCollection([Dummy::class]), 'Test API', 'This is a test API.', '1.2.3', ['jsonld' => ['application/ld+json']]);
8080

8181
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
82-
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->shouldBeCalled()->willReturn(new PropertyNameCollection(['id', 'name']));
82+
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->shouldBeCalled()->willReturn(new PropertyNameCollection(['id', 'name', 'description']));
8383

8484
$dummyMetadata = new ResourceMetadata('Dummy', 'This is a dummy.', 'http://schema.example.com/Dummy', ['get' => ['method' => 'GET'], 'put' => ['method' => 'PUT']], ['get' => ['method' => 'GET'], 'post' => ['method' => 'POST'], 'custom' => ['method' => 'GET', 'path' => '/foo'], 'custom2' => ['method' => 'POST', 'path' => '/foo']], ['pagination_client_items_per_page' => true]);
8585
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
@@ -88,6 +88,7 @@ public function testNormalize()
8888
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
8989
$propertyMetadataFactoryProphecy->create(Dummy::class, 'id')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT), 'This is an id.', true, false));
9090
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'This is a name.', true, true, true, true, false, false, null, null, []));
91+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'description')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'This is a initializable but not writable property.', true, false, true, true, false, false, null, null, [], null, true));
9192
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
9293
$resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
9394

@@ -299,6 +300,10 @@ public function testNormalize()
299300
'type' => 'string',
300301
'description' => 'This is a name.',
301302
]),
303+
'description' => new \ArrayObject([
304+
'type' => 'string',
305+
'description' => 'This is a initializable but not writable property.',
306+
]),
302307
],
303308
]),
304309
]),

0 commit comments

Comments
 (0)