Skip to content

Commit c07b770

Browse files
Merge pull request #1433 from mab05k/mab05k/feature/property-swagger-context
Add feature to update swagger context for properties
2 parents d536e7e + 9ba21ef commit c07b770

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
@@ -533,7 +533,7 @@ private function getPropertySchema(PropertyMetadata $propertyMetadata, \ArrayObj
533533

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

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

539539
/**

tests/Swagger/Serializer/DocumentationNormalizerTest.php

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

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

0 commit comments

Comments
 (0)