Skip to content

Commit e3ea015

Browse files
authored
Merge pull request #3052 from bpolaszek/2.4
Fix DateTimeInterface type-hinting
2 parents a0c9eff + 49be823 commit e3ea015

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

src/Bridge/NelmioApiDoc/Parser/ApiPlatformParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ private function parseProperty(ResourceMetadata $resourceMetadata, PropertyMetad
235235
if ('object' === $builtinType) {
236236
$className = $type->getClassName();
237237

238-
if (is_subclass_of($className, \DateTimeInterface::class)) {
238+
if (is_a($className, \DateTimeInterface::class, true)) {
239239
$data['dataType'] = DataTypes::DATETIME;
240240
$data['format'] = sprintf('{DateTime %s}', \DateTime::RFC3339);
241241

src/Swagger/Serializer/DocumentationNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ private function getType(bool $v3, string $type, bool $isCollection, ?string $cl
675675
return ['type' => 'string'];
676676
}
677677

678-
if (is_subclass_of($className, \DateTimeInterface::class)) {
678+
if (is_a($className, \DateTimeInterface::class, true)) {
679679
return ['type' => 'string', 'format' => 'date-time'];
680680
}
681681

tests/Swagger/Serializer/DocumentationNormalizerV2Test.php

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

8686
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
87-
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->shouldBeCalled()->willReturn(new PropertyNameCollection(['id', 'name', 'description']));
87+
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->shouldBeCalled()->willReturn(new PropertyNameCollection(['id', 'name', 'description', 'dummyDate']));
8888

8989
$dummyMetadata = new ResourceMetadata('Dummy', 'This is a dummy.', 'http://schema.example.com/Dummy', ['get' => ['method' => 'GET', 'status' => '202'], 'put' => ['method' => 'PUT', 'status' => '202']], ['get' => ['method' => 'GET', 'status' => '202'], 'post' => ['method' => 'POST', 'status' => '202'], 'custom' => ['method' => 'GET', 'path' => '/foo', 'status' => '202'], 'custom2' => ['method' => 'POST', 'path' => '/foo']], ['pagination_client_items_per_page' => true]);
9090
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
@@ -94,6 +94,7 @@ public function testNormalize()
9494
$propertyMetadataFactoryProphecy->create(Dummy::class, 'id')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT), 'This is an id.', true, false));
9595
$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, []));
9696
$propertyMetadataFactoryProphecy->create(Dummy::class, 'description')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'This is an initializable but not writable property.', true, false, true, true, false, false, null, null, [], null, true));
97+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'dummyDate')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_OBJECT, true, \DateTimeInterface::class), 'This is a \DateTimeInterface object.', true, true, true, true, false, false, null, null, []));
9798
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
9899
$resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
99100

@@ -309,6 +310,11 @@ public function testNormalize()
309310
'type' => 'string',
310311
'description' => 'This is an initializable but not writable property.',
311312
]),
313+
'dummyDate' => new \ArrayObject([
314+
'type' => 'string',
315+
'description' => 'This is a \DateTimeInterface object.',
316+
'format' => 'date-time',
317+
]),
312318
],
313319
]),
314320
]),

tests/Swagger/Serializer/DocumentationNormalizerV3Test.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public function testNormalize()
6262
$documentation = new Documentation(new ResourceNameCollection([Dummy::class]), 'Test API', 'This is a test API.', '1.2.3', ['jsonld' => ['application/ld+json']]);
6363

6464
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
65-
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->shouldBeCalled()->willReturn(new PropertyNameCollection(['id', 'name', 'description']));
66-
$propertyNameCollectionFactoryProphecy->create(Dummy::class)->shouldBeCalled()->willReturn(new PropertyNameCollection(['id', 'name', 'description']));
65+
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->shouldBeCalled()->willReturn(new PropertyNameCollection(['id', 'name', 'description', 'dummyDate']));
66+
$propertyNameCollectionFactoryProphecy->create(Dummy::class)->shouldBeCalled()->willReturn(new PropertyNameCollection(['id', 'name', 'description', 'dummyDate']));
6767

6868
$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]);
6969
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
@@ -73,6 +73,7 @@ public function testNormalize()
7373
$propertyMetadataFactoryProphecy->create(Dummy::class, 'id')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT), 'This is an id.', true, false, null, null, null, true));
7474
$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, []));
7575
$propertyMetadataFactoryProphecy->create(Dummy::class, 'description')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'This is an initializable but not writable property.', true, false, true, true, false, false, null, null, [], null, true));
76+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'dummyDate')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_OBJECT, true, \DateTime::class), 'This is a \DateTimeInterface object.', true, true, true, true, false, false, null, null, []));
7677
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
7778
$resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
7879

@@ -342,6 +343,11 @@ public function testNormalize()
342343
'type' => 'string',
343344
'description' => 'This is an initializable but not writable property.',
344345
]),
346+
'dummyDate' => new \ArrayObject([
347+
'type' => 'string',
348+
'description' => 'This is a \DateTimeInterface object.',
349+
'format' => 'date-time',
350+
]),
345351
],
346352
]),
347353
]),

0 commit comments

Comments
 (0)