Search code examples
swagger-php

What is correct syntax to apply security globally for OpanApi 3.0 and PHP attributes with swagger-php


I am using PHP attributes to produce my OpenApi yaml file specfication/documentation and then using Swagger UI to interact with it. I have the following defined which creates the .yaml file without any problem

use OpenApi\Attributes as OA;

#[OA\OpenApi(
    info: new OA\Info(
       ...
    ),
    servers: [
        new OA\Server(
            ...
        ),
    ],
    components: new OA\Components(
        parameters: [
            new OA\Parameter(
                ...
            ),
        ],
        responses: [
            new OA\Response( 
                ...
            ),   
        ],
        securitySchemes: [
            new OA\SecurityScheme(
                securityScheme: 'BearerAuth',
                description: 'this is the beaer security scheme',
                type: 'http',
                scheme: 'bearer',
                name: 'Authorization',
                in: 'header'
            )
        ],
    ),
    tags: [
        new OA\Tag(
            ...
        )
    ]
)]

I want to apply the bearer security globally so know I need to add something along the lines of

security: [ 'BearerAuth': [] ]

below or above tags: however this is generating an error .. Error: ParseError: syntax error, unexpected token ":", expecting "]" in ...

If I leave out this security definition from the class file and then manually add the following to the .yaml file security: - Bearer: [] then it all works as expected.

So my question what is the correct syntax for the security: declaration

I have tried a number of different ways but all have failed.


Solution

  • Generally I think this is the correct way (according to https://zircote.github.io/swagger-php/guide/cookbook.html#default-security-scheme-for-all-endpoints)

    Just remember that with Attributes you are writing proper PHP code, not PHPDoc, so it should probably be

    security: [ 'BearerAuth' => [] ]