Skip to content

Commit 9ba21ef

Browse files
committed
Add feature to update swagger context for properties
1 parent c3c380e commit 9ba21ef

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

src/Swagger/Serializer/DocumentationNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ private function getPropertySchema(PropertyMetadata $propertyMetadata, \ArrayObj
532532

533533
$valueSchema = $this->getType($builtinType, $isCollection, $className, $propertyMetadata->isReadableLink(), $definitions, $serializerContext);
534534

535-
return new \ArrayObject((array) $propertySchema + $valueSchema);
535+
return new \ArrayObject((array) $propertySchema + $valueSchema + ($propertyMetadata->getAttributes()['swagger_context'] ?? []));
536536
}
537537

538538
/**

tests/Swagger/Serializer/DocumentationNormalizerTest.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,4 +1785,93 @@ public function testNormalizeWithSubResource()
17851785

17861786
$this->assertEquals($expected, $normalizer->normalize($documentation));
17871787
}
1788+
1789+
public function testNormalizeWithPropertySwaggerContext()
1790+
{
1791+
$documentation = new Documentation(new ResourceNameCollection([Dummy::class]), 'Test API', 'This is a test API.', '1.2.3', ['jsonld' => ['application/ld+json']]);
1792+
1793+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
1794+
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->shouldBeCalled()->willReturn(new PropertyNameCollection(['id', 'name']));
1795+
1796+
$dummyMetadata = new ResourceMetadata('Dummy', 'This is a dummy.', 'http://schema.example.com/Dummy', ['get' => ['method' => 'GET']]);
1797+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
1798+
$resourceMetadataFactoryProphecy->create(Dummy::class)->shouldBeCalled()->willReturn($dummyMetadata);
1799+
1800+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
1801+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'id')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT), 'This is an id.', true, false));
1802+
$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, ['swagger_context' => ['type' => 'string', 'enum' => ['one', 'two'], 'example' => 'one']]));
1803+
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
1804+
$resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
1805+
1806+
$operationMethodResolverProphecy = $this->prophesize(OperationMethodResolverInterface::class);
1807+
$operationMethodResolverProphecy->getItemOperationMethod(Dummy::class, 'get')->shouldBeCalled()->willReturn('GET');
1808+
1809+
$operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator()));
1810+
1811+
$normalizer = new DocumentationNormalizer(
1812+
$resourceMetadataFactoryProphecy->reveal(),
1813+
$propertyNameCollectionFactoryProphecy->reveal(),
1814+
$propertyMetadataFactoryProphecy->reveal(),
1815+
$resourceClassResolverProphecy->reveal(),
1816+
$operationMethodResolverProphecy->reveal(),
1817+
$operationPathResolver
1818+
);
1819+
1820+
$expected = [
1821+
'swagger' => '2.0',
1822+
'basePath' => '/app_dev.php/',
1823+
'info' => [
1824+
'title' => 'Test API',
1825+
'description' => 'This is a test API.',
1826+
'version' => '1.2.3',
1827+
],
1828+
'paths' => new \ArrayObject([
1829+
'/dummies/{id}' => [
1830+
'get' => new \ArrayObject([
1831+
'tags' => ['Dummy'],
1832+
'operationId' => 'getDummyItem',
1833+
'produces' => ['application/ld+json'],
1834+
'summary' => 'Retrieves a Dummy resource.',
1835+
'parameters' => [
1836+
[
1837+
'name' => 'id',
1838+
'in' => 'path',
1839+
'type' => 'string',
1840+
'required' => true,
1841+
],
1842+
],
1843+
'responses' => [
1844+
200 => [
1845+
'description' => 'Dummy resource response',
1846+
'schema' => ['$ref' => '#/definitions/Dummy'],
1847+
],
1848+
404 => ['description' => 'Resource not found'],
1849+
],
1850+
]),
1851+
],
1852+
]),
1853+
'definitions' => new \ArrayObject([
1854+
'Dummy' => new \ArrayObject([
1855+
'type' => 'object',
1856+
'description' => 'This is a dummy.',
1857+
'externalDocs' => ['url' => 'http://schema.example.com/Dummy'],
1858+
'properties' => [
1859+
'id' => new \ArrayObject([
1860+
'type' => 'integer',
1861+
'description' => 'This is an id.',
1862+
'readOnly' => true,
1863+
]),
1864+
'name' => new \ArrayObject([
1865+
'type' => 'string',
1866+
'description' => 'This is a name.',
1867+
'enum' => ['one', 'two'],
1868+
'example' => 'one',
1869+
]),
1870+
],
1871+
]),
1872+
]),
1873+
];
1874+
1875+
$this->assertEquals($expected, $normalizer->normalize($documentation, DocumentationNormalizer::FORMAT, ['base_url' => '/app_dev.php/']));
1876+
}
17881877
}

0 commit comments

Comments
 (0)