Skip to content

Commit e230116

Browse files
committed
Adding openapi_context attribute
1 parent cc34c9e commit e230116

File tree

8 files changed

+29
-5
lines changed

8 files changed

+29
-5
lines changed

src/Annotation/ApiProperty.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* @Attribute("deprecationReason", type="string"),
2727
* @Attribute("fetchable", type="bool"),
2828
* @Attribute("fetchEager", type="bool"),
29+
* @Attribute("openapiContext", type="array"),
2930
* @Attribute("jsonldContext", type="array"),
3031
* @Attribute("swaggerContext", type="array")
3132
* )
@@ -109,6 +110,13 @@ final class ApiProperty
109110
*/
110111
private $swaggerContext;
111112

113+
/**
114+
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
115+
*
116+
* @var array
117+
*/
118+
private $openapiContext;
119+
112120
/**
113121
* @throws InvalidArgumentException
114122
*/

src/Annotation/ApiResource.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
* @Attribute("maximumItemsPerPage", type="int"),
4444
* @Attribute("mercure", type="mixed"),
4545
* @Attribute("normalizationContext", type="array"),
46+
* @Attribute("openapiContext", type="array"),
4647
* @Attribute("order", type="array"),
4748
* @Attribute("outputClass", type="string"),
4849
* @Attribute("paginationClientEnabled", type="bool"),
@@ -288,6 +289,13 @@ final class ApiResource
288289
*/
289290
private $outputClass;
290291

292+
/**
293+
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
294+
*
295+
* @var array
296+
*/
297+
private $openapiContext;
298+
291299
/**
292300
* @throws InvalidArgumentException
293301
*/

src/OpenAPI/Serializer/AbstractDocumentationNormalizer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
abstract class AbstractDocumentationNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
3737
{
3838
const FORMAT = 'json';
39+
const ATTRIBUTE_NAME = 'openapi_context';
3940

4041
protected $resourceMetadataFactory;
4142
protected $propertyNameCollectionFactory;
@@ -212,7 +213,7 @@ protected function getDefinitionSchema(string $resourceClass, ResourceMetadata $
212213
*/
213214
protected function getPropertySchema(PropertyMetadata $propertyMetadata, \ArrayObject $definitions, array $serializerContext = null): \ArrayObject
214215
{
215-
$propertySchema = new \ArrayObject($propertyMetadata->getAttributes()['swagger_context'] ?? []);
216+
$propertySchema = new \ArrayObject($propertyMetadata->getAttributes()[static::ATTRIBUTE_NAME] ?? []);
216217

217218
if (false === $propertyMetadata->isWritable() && !$propertyMetadata->isInitializable()) {
218219
$propertySchema['readOnly'] = true;
@@ -249,7 +250,7 @@ protected function getPropertySchema(PropertyMetadata $propertyMetadata, \ArrayO
249250
*/
250251
protected function getPathOperation(string $operationName, array $operation, string $method, string $operationType, string $resourceClass, ResourceMetadata $resourceMetadata, array $mimeTypes, \ArrayObject $definitions): \ArrayObject
251252
{
252-
$pathOperation = new \ArrayObject($operation['swagger_context'] ?? []);
253+
$pathOperation = new \ArrayObject($operation[static::ATTRIBUTE_NAME] ?? []);
253254
$resourceShortName = $resourceMetadata->getShortName();
254255
$pathOperation['tags'] ?? $pathOperation['tags'] = [$resourceShortName];
255256
$pathOperation['operationId'] ?? $pathOperation['operationId'] = lcfirst($operationName).ucfirst($resourceShortName).ucfirst($operationType);

src/Swagger/Serializer/DocumentationNormalizer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ final class DocumentationNormalizer extends AbstractDocumentationNormalizer
4444

4545
const SWAGGER_VERSION = '2.0';
4646
const SWAGGER_DEFINITION_NAME = 'swagger_definition_name';
47+
const ATTRIBUTE_NAME = 'swagger_context';
4748

4849
/**
4950
* @param ContainerInterface|FilterCollection|null $filterLocator The new filter locator or the deprecated filter collection

tests/Annotation/ApiPropertyTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function testConstruct()
5353
'fetchEager' => false,
5454
'jsonldContext' => ['foo' => 'bar'],
5555
'swaggerContext' => ['foo' => 'baz'],
56+
'openapiContext' => ['foo' => 'baz'],
5657
'attributes' => ['unknown' => 'unknown', 'fetchable' => false],
5758
]);
5859
$this->assertEquals([
@@ -61,6 +62,7 @@ public function testConstruct()
6162
'fetch_eager' => false,
6263
'jsonld_context' => ['foo' => 'bar'],
6364
'swagger_context' => ['foo' => 'baz'],
65+
'openapi_context' => ['foo' => 'baz'],
6466
'unknown' => 'unknown',
6567
], $property->attributes);
6668
}

tests/Annotation/ApiResourceTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function testConstruct()
4444
'mercure' => '[\'foo\', object.owner]',
4545
'normalizationContext' => ['groups' => ['bar']],
4646
'order' => ['foo', 'bar' => 'ASC'],
47+
'openapiContext' => ['description' => 'foo'],
4748
'paginationClientEnabled' => true,
4849
'paginationClientItemsPerPage' => true,
4950
'paginationClientPartial' => true,
@@ -54,6 +55,7 @@ public function testConstruct()
5455
'routePrefix' => '/foo',
5556
'shortName' => 'shortName',
5657
'subresourceOperations' => [],
58+
'swaggerContext' => ['description' => 'bar'],
5759
'validationGroups' => ['foo', 'bar'],
5860
'sunset' => 'Thu, 11 Oct 2018 00:00:00 +0200',
5961
]);
@@ -78,6 +80,7 @@ public function testConstruct()
7880
'mercure' => '[\'foo\', object.owner]',
7981
'normalization_context' => ['groups' => ['bar']],
8082
'order' => ['foo', 'bar' => 'ASC'],
83+
'openapi_context' => ['description' => 'foo'],
8184
'pagination_client_enabled' => true,
8285
'pagination_client_items_per_page' => true,
8386
'pagination_client_partial' => true,
@@ -86,6 +89,7 @@ public function testConstruct()
8689
'pagination_items_per_page' => 42,
8790
'pagination_partial' => true,
8891
'route_prefix' => '/foo',
92+
'swagger_context' => ['description' => 'bar'],
8993
'validation_groups' => ['baz', 'qux'],
9094
'cache_headers' => ['max_age' => 0, 'shared_max_age' => 0],
9195
'sunset' => 'Thu, 11 Oct 2018 00:00:00 +0200',

tests/Fixtures/TestBundle/Entity/DummyCar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
/**
2828
* @ApiResource(
29-
* itemOperations={"get"={"swagger_context"={"tags"={}}}, "put", "delete"},
29+
* itemOperations={"get"={"swagger_context"={"tags"={}}, "openapi_context"={"tags"={}}}, "put", "delete"},
3030
* attributes={
3131
* "normalization_context"={"groups"={"colors"}},
3232
* "sunset"="2050-01-01"

tests/OpenAPI/Serializer/DocumentationNormalizerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,7 +2034,7 @@ public function testNormalizeWithPropertyOpenAPIContext()
20342034

20352035
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
20362036
$propertyMetadataFactoryProphecy->create(Dummy::class, 'id')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT), 'This is an id.', true, false));
2037-
$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']]));
2037+
$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, ['openapi_context' => ['type' => 'string', 'enum' => ['one', 'two'], 'example' => 'one']]));
20382038
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
20392039
$resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
20402040

@@ -2128,7 +2128,7 @@ public function testNormalizeWithPaginationClientEnabled()
21282128

21292129
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
21302130
$propertyMetadataFactoryProphecy->create(Dummy::class, 'id')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT), 'This is an id.', true, false));
2131-
$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']]));
2131+
$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, ['openapi_context' => ['type' => 'string', 'enum' => ['one', 'two'], 'example' => 'one']]));
21322132
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
21332133
$resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
21342134

0 commit comments

Comments
 (0)