zircote / swagger-php

A php swagger annotation and parsing library
http://zircote.github.io/swagger-php/
Apache License 2.0
5.08k stars 933 forks source link

Filtering not done as expected #1624

Closed caixingyue closed 2 months ago

caixingyue commented 3 months ago

image

When all interfaces in a tag are filtered out, the tag should also be removed because it does not have any effect when displayed here.

As shown by the blue line in the figure, no api appears when I click to expand, so it should not continue to exist.

new Processors\PathFilter([], ['/api/']),
new Processors\CleanUnusedComponents(true),
DerManoMann commented 2 months ago

This might be something to be added to the new AugmentTags processor...

momala454 commented 2 months ago

it still doesn't work. I see tags from removed path still listed image

my config

'pathFilter' => [
        'paths' => ['/^\/admin\/.+/'],
    ],
    'cleanUnusedComponents' => ['enabled' => true],
DerManoMann commented 2 months ago

What can I say. Any chance you could post a single file reproducer?

momala454 commented 2 months ago

Test.php

#[\OpenApi\Attributes\OpenApi(
    info: new \OpenApi\Attributes\Info(
        title: 'test',
        description: 'test',
        version: '2.0.0'
    ),
)]
class Test
{
    #[\OpenApi\Attributes\Get(path: '/world/', tags: ['tag world'], responses: [new \OpenApi\Attributes\Response(response: '200', description: 'success')])]
    #[\OpenApi\Attributes\Get(path: '/hello/', tags: ['tag hello'], responses: [new \OpenApi\Attributes\Response(response: '200', description: 'success')])]
    public function hello(): void
    {
    }
}

testrun.php

require("vendor/autoload.php");

spl_autoload_register(function ($string): void {
    if ($string === 'Test') {
        require __DIR__ . '/Test.php';
    }
});

$openapi = (new \OpenApi\Generator())->setVersion('3.0.0')->setConfig([
    'pathFilter' => [
        'paths' => ['/^\/hello\//'],
    ],
    'cleanUnusedComponents' => ['enabled' => true],
])->generate(\OpenApi\Util::finder([__DIR__ . '/Test.php']));
$json = json_decode($openapi->toJson(), true);
echo json_encode($json, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR);

Output, with both tags instead of a single one

{
    "openapi": "3.0.0",
    "info": {
        "title": "test",
        "description": "test",
        "version": "2.0.0"
    },
    "paths": {
        "\/hello\/": {
            "get": {
                "tags": [
                    "tag hello"
                ],
                "operationId": "d334274659cc0b24fab8ee0a76559fdf",
                "responses": {
                    "200": {
                        "description": "success"
                    }
                }
            }
        }
    },
    "tags": [
        {
            "name": "tag world",
            "description": "tag world"
        },
        {
            "name": "tag hello",
            "description": "tag hello"
        }
    ]
}