mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 23:59:26 +03:00
Allow security policies to be removed using the Security annotation by passing it a name of null.
This commit is contained in:
parent
1885d25cd9
commit
9545a0ce52
@ -109,6 +109,13 @@ final class OpenApiPhpDescriber
|
|||||||
|
|
||||||
if ($annotation instanceof Security) {
|
if ($annotation instanceof Security) {
|
||||||
$annotation->validate();
|
$annotation->validate();
|
||||||
|
|
||||||
|
if (null === $annotation->name) {
|
||||||
|
$mergeProperties->security = [];
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$mergeProperties->security[] = [$annotation->name => $annotation->scopes];
|
$mergeProperties->security[] = [$annotation->name => $annotation->scopes];
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
|
43
Resources/doc/security.rst
Normal file
43
Resources/doc/security.rst
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
Security
|
||||||
|
========
|
||||||
|
|
||||||
|
A default security policy can be added in ``nelmio_api_doc.documentation.security``
|
||||||
|
|
||||||
|
.. code-block:: yaml
|
||||||
|
|
||||||
|
nelmio_api_doc:
|
||||||
|
documentation:
|
||||||
|
components:
|
||||||
|
securitySchemes:
|
||||||
|
Bearer:
|
||||||
|
type: http
|
||||||
|
scheme: bearer
|
||||||
|
ApiKeyAuth:
|
||||||
|
type: apiKey
|
||||||
|
in: header
|
||||||
|
name: X-API-Key
|
||||||
|
security:
|
||||||
|
Bearer: []
|
||||||
|
|
||||||
|
This will add the Bearer security policy to all registered paths.
|
||||||
|
|
||||||
|
Overriding Specific Paths
|
||||||
|
-------------------------
|
||||||
|
|
||||||
|
The security policy can be overriden for a path using the ``@Security`` annotation.
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Security(name="ApiKeyAuth")
|
||||||
|
*/
|
||||||
|
|
||||||
|
Notice at the bottom of the docblock is a ``@Security`` annotation with a name of `ApiKeyAuth`. This will override the global security policy to only accept the ``ApiKeyAuth`` policy for this path.
|
||||||
|
|
||||||
|
You can also completely remove security from a path by providing ``@Security`` with a name of ``null``.
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Security(name=null)
|
||||||
|
*/
|
@ -164,6 +164,16 @@ class ApiController80
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Route("/securityOverride")
|
||||||
|
* @OA\Response(response="201", description="")
|
||||||
|
* @Security(name="api_key")
|
||||||
|
* @Security(name=null)
|
||||||
|
*/
|
||||||
|
public function securityActionOverride()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Route("/swagger/symfonyConstraints", methods={"GET"})
|
* @Route("/swagger/symfonyConstraints", methods={"GET"})
|
||||||
* @OA\Response(
|
* @OA\Response(
|
||||||
|
@ -50,4 +50,12 @@ class ApiController81 extends ApiController80
|
|||||||
public function securityActionAttributes()
|
public function securityActionAttributes()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[Route('/security_override_attributes')]
|
||||||
|
#[OA\Response(response: '201', description: '')]
|
||||||
|
#[Security(name: 'api_key')]
|
||||||
|
#[Security(name: null)]
|
||||||
|
public function securityOverrideActionAttributes()
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -369,6 +369,24 @@ class FunctionalTest extends WebTestCase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider provideSecurityOverrideRoute
|
||||||
|
*/
|
||||||
|
public function testSecurityOverrideAction(string $route)
|
||||||
|
{
|
||||||
|
$operation = $this->getOperation($route, 'get');
|
||||||
|
$this->assertEquals([], $operation->security);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideSecurityOverrideRoute(): iterable
|
||||||
|
{
|
||||||
|
yield 'Annotations' => ['/api/securityOverride'];
|
||||||
|
|
||||||
|
if (\PHP_VERSION_ID >= 80100) {
|
||||||
|
yield 'Attributes' => ['/api/security_override_attributes'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function testClassSecurityAction()
|
public function testClassSecurityAction()
|
||||||
{
|
{
|
||||||
$operation = $this->getOperation('/api/security/class', 'get');
|
$operation = $this->getOperation('/api/security/class', 'get');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user