From 1459e8c8241dc2bb8c5bb7f8e3a13463383402b0 Mon Sep 17 00:00:00 2001 From: romalytvynenko Date: Wed, 5 Feb 2025 08:08:46 +0200 Subject: [PATCH] fixed transformers configuration leaking between versions (#706) --- .../GeneratorConfigCollection.php | 8 +++---- tests/GeneratorConfigTest.php | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/Configuration/GeneratorConfigCollection.php b/src/Configuration/GeneratorConfigCollection.php index fc7e7515..3c563e70 100644 --- a/src/Configuration/GeneratorConfigCollection.php +++ b/src/Configuration/GeneratorConfigCollection.php @@ -41,10 +41,10 @@ public function register(string $name, array $config): GeneratorConfig { $this->apis[$name] = $generatorConfig = new GeneratorConfig( config: array_merge(config('scramble') ?: [], $config), - parametersExtractors: $this->apis[Scramble::DEFAULT_API]->parametersExtractors, - operationTransformers: $this->apis[Scramble::DEFAULT_API]->operationTransformers, - documentTransformers: $this->apis[Scramble::DEFAULT_API]->documentTransformers, - serverVariables: $this->apis[Scramble::DEFAULT_API]->serverVariables, + parametersExtractors: clone $this->apis[Scramble::DEFAULT_API]->parametersExtractors, + operationTransformers: clone $this->apis[Scramble::DEFAULT_API]->operationTransformers, + documentTransformers: clone $this->apis[Scramble::DEFAULT_API]->documentTransformers, + serverVariables: clone $this->apis[Scramble::DEFAULT_API]->serverVariables, ); return $generatorConfig; diff --git a/tests/GeneratorConfigTest.php b/tests/GeneratorConfigTest.php index 27319c0d..f105f9ce 100644 --- a/tests/GeneratorConfigTest.php +++ b/tests/GeneratorConfigTest.php @@ -102,6 +102,17 @@ public function registered_api_exposed_explicitly() collect($routes->getRoutes())->firstWhere('uri', 'docs/v2/openapi.json') ); } + + /** @test */ + #[WithProviders([UsesCommonConfiguration_GeneratorConfigTest::class])] + public function uses_common_configurations() + { + $defaultConfig = Scramble::getConfigurationsInstance()->get('default'); + $v2Config = Scramble::getConfigurationsInstance()->get('v2'); + + $this->assertEquals(['common', 'only-default'], $defaultConfig->documentTransformers->all()); + $this->assertEquals(['common', 'only-v2'], $v2Config->documentTransformers->all()); + } } class DisablesExposedRoutes_GeneratorConfigTest extends ServiceProvider @@ -147,6 +158,18 @@ public function boot() } } +class UsesCommonConfiguration_GeneratorConfigTest extends ServiceProvider +{ + public function boot() + { + Scramble::configure()->withDocumentTransformers('common'); + + Scramble::registerApi('v2')->withDocumentTransformers('only-v2'); + + Scramble::configure()->withDocumentTransformers('only-default'); + } +} + #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)] class WithProviders implements Invokable {