Skip to content

Commit

Permalink
fix: before implementation with custom processors (#2421)
Browse files Browse the repository at this point in the history
## Description

The before implementation was never functional, as the returned tags
array is a tag name indexed array of tags. This change resolves that,
and adds a test to actually insert a processor with the before
attribute.


![image](https://github.com/user-attachments/assets/eba83f7a-45ba-4fac-b05b-7e43bd80f33b)

## What type of PR is this? (check all applicable)
- [x] Bug Fix
- [ ] Feature
- [ ] Refactor
- [ ] Deprecation
- [ ] Breaking Change
- [ ] Documentation Update
- [ ] CI

## Checklist
- [ ] I have made corresponding changes to the documentation (`docs/`)
- [ ] I have made corresponding changes to the changelog
(`CHANGELOG.md`)
  • Loading branch information
bobvandevijver authored Jan 17, 2025
1 parent 3216d13 commit e44364d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
14 changes: 3 additions & 11 deletions src/DependencyInjection/Compiler/CustomProcessorPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* Compiler Pass to identify and register custom processors.
Expand All @@ -36,24 +35,17 @@ public function process(ContainerBuilder $container): void
$definition = $container->findDefinition('nelmio_api_doc.open_api.generator');

foreach ($this->findAndSortTaggedServices('nelmio_api_doc.swagger.processor', $container) as $reference) {
$id = (string) $reference;
$tags = $container->findDefinition($id)->getTags();

/**
* Before which processor should this processor be run?
*
* @var string|null
*/
$before = null;
$tags = $container->findDefinition((string) $reference)->getTag('nelmio_api_doc.swagger.processor');

// See if the processor has a 'before' attribute.
$before = null;
foreach ($tags as $tag) {
if (isset($tag['before'])) {
$before = $tag['before'];
}
}

$definition->addMethodCall('addProcessor', [new Reference($id), $before]);
$definition->addMethodCall('addProcessor', [$reference, $before]);
}
}
}
4 changes: 4 additions & 0 deletions tests/Functional/Configs/StubProcessor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
Nelmio\ApiDocBundle\Tests\Functional\StubProcessor:
tags:
- { name: 'nelmio_api_doc.swagger.processor', priority: -100, before: OpenApi\Processors\CleanUnusedComponents }
2 changes: 1 addition & 1 deletion tests/Functional/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public static function provideUniversalTestCases(): \Generator
null,
'VendorExtension',
[],
[__DIR__.'/Configs/VendorExtension.yaml'],
[__DIR__.'/Configs/VendorExtension.yaml', __DIR__.'/Configs/StubProcessor.yaml'],
];
}

Expand Down
22 changes: 22 additions & 0 deletions tests/Functional/StubProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nelmio\ApiDocBundle\Tests\Functional;

use OpenApi\Analysis;

class StubProcessor
{
public function __invoke(Analysis $analysis): void
{
// Does nothing
}
}

0 comments on commit e44364d

Please sign in to comment.