From 7f58c488161675831ef13b49f827b4fd4f901326 Mon Sep 17 00:00:00 2001 From: Shalvah Date: Sun, 24 Dec 2023 17:06:56 +0100 Subject: [PATCH] Support `overrides` and strategy tuples --- src/Extracting/Extractor.php | 24 ++++++++-- tests/Unit/ExtractorPluginSystemTest.php | 61 ++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/src/Extracting/Extractor.php b/src/Extracting/Extractor.php index 5a48b8ed..9eef2cf1 100644 --- a/src/Extracting/Extractor.php +++ b/src/Extracting/Extractor.php @@ -203,14 +203,30 @@ protected function iterateThroughStrategies(string $stage, ExtractedEndpointData { $strategies = $this->config->get("strategies.$stage", []); - foreach ($strategies as $strategyClass) { - /** @var Strategy $strategy */ + $overrides = []; + foreach ($strategies as $strategyClassOrTuple) { + if (is_array($strategyClassOrTuple)) { + $strategyClass = $strategyClassOrTuple[0]; + if ($strategyClass == 'overrides') { + $overrides = $strategyClassOrTuple[1]; + continue; + } + $settings = $strategyClassOrTuple[1]; + } else { + $strategyClass = $strategyClassOrTuple; + $settings = $rulesToApply; + } + $strategy = new $strategyClass($this->config); - $results = $strategy($endpointData, $rulesToApply); + $results = $strategy($endpointData, $settings); if (is_array($results)) { $handler($results); } } + + if (!empty($overrides)) { + $handler($overrides); + } } /** @@ -238,7 +254,7 @@ public static function cleanParams(array $parameters): array * @var Parameter $details */ foreach ($parameters as $paramName => $details) { - + // Remove params which have no intentional examples and are optional. if (!$details->exampleWasSpecified) { if (is_null($details->example) && $details->required === false) { diff --git a/tests/Unit/ExtractorPluginSystemTest.php b/tests/Unit/ExtractorPluginSystemTest.php index 2e72bc12..5a49f783 100644 --- a/tests/Unit/ExtractorPluginSystemTest.php +++ b/tests/Unit/ExtractorPluginSystemTest.php @@ -58,6 +58,56 @@ public function only_specified_strategies_are_loaded() $this->assertFalse(EmptyStrategy2::$called); } + /** @test */ + public function supports_overrides() + { + $config = [ + 'strategies' => [ + 'headers' => [ + DummyHeaderStrategy::class, + [ + 'overrides', + ['Content-Type' => 'application/xml'], + ] + ], + 'bodyParameters' => [], + 'responses' => [], // Making this empty so the Laravel-dependent strategies are not called + ], + ]; + + $endpointData = $this->processRoute($config); + + $this->assertEquals([ + 'Accept' => 'application/form-data', + 'Content-Type' => 'application/xml', + ], $endpointData->headers); + } + + + /** @test */ + public function supports_strategy_tuples() + { + $config = [ + 'strategies' => [ + 'headers' => [ + [ + DummyHeaderStrategy::class, + ['use_this_content_type' => 'text/plain'], + ] + ], + 'bodyParameters' => [], + 'responses' => [], // Making this empty so the Laravel-dependent strategies are not called + ], + ]; + + $endpointData = $this->processRoute($config); + + $this->assertEquals([ + 'Accept' => 'application/form-data', + 'Content-Type' => 'text/plain', + ], $endpointData->headers); + } + /** @test */ public function responses_from_different_strategies_get_added() { @@ -217,6 +267,17 @@ public function __invoke(ExtractedEndpointData $endpointData, array $routeRules } } +class DummyHeaderStrategy extends Strategy +{ + public function __invoke(ExtractedEndpointData $endpointData, array $settings = []): ?array + { + return [ + 'Accept' => 'application/form-data', + 'Content-Type' => $settings['use_this_content_type'] ?? 'application/form-data', + ]; + } +} + class NotDummyMetadataStrategy extends Strategy { public static $called = false;