From 869c8c970ed41b74350eb2514a1d867286de7a8d Mon Sep 17 00:00:00 2001 From: Chris Nizzardini Date: Mon, 27 Dec 2021 11:32:39 -0500 Subject: [PATCH] Fixes OpenApiResponse association permenantly modifying the Schema Resolves #363 --- src/Lib/Operation/OperationResponseAssociation.php | 8 ++++++-- tests/TestCase/Lib/Operation/OperationResponseTest.php | 8 +++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Lib/Operation/OperationResponseAssociation.php b/src/Lib/Operation/OperationResponseAssociation.php index b9d5cd1f..412e478a 100644 --- a/src/Lib/Operation/OperationResponseAssociation.php +++ b/src/Lib/Operation/OperationResponseAssociation.php @@ -178,8 +178,12 @@ private function findSchema(Table $table, string $schemaMode): Schema $entityName = $this->inflector::singularize($table->getAlias()); - return $this->swagger->getSchemaByName($entityName . '-' . $schemaMode) ?? - $this->buildAssociatedSchema($entityName, $table->getAlias()); + $schema = $this->swagger->getSchemaByName($entityName . '-' . $schemaMode); + if ($schema) { + return clone $schema; + } + + return clone $this->buildAssociatedSchema($entityName, $table->getAlias()); } /** diff --git a/tests/TestCase/Lib/Operation/OperationResponseTest.php b/tests/TestCase/Lib/Operation/OperationResponseTest.php index 5166e809..837f3525 100644 --- a/tests/TestCase/Lib/Operation/OperationResponseTest.php +++ b/tests/TestCase/Lib/Operation/OperationResponseTest.php @@ -371,8 +371,10 @@ public function test_associations(): void ]) ); + $swagger = (new SwaggerFactory($this->config, new RouteScanner($this->router, $this->config)))->create(); + $operationResponse = new OperationResponse( - (new SwaggerFactory($this->config, new RouteScanner($this->router, $this->config)))->create(), + $swagger, $this->config, new Operation('hello', 'get'), $route, @@ -383,6 +385,10 @@ public function test_associations(): void $operation = $operationResponse->getOperationWithResponses(); $content = $operation->getResponseByCode('200')->getContentByMimeType('application/json'); $this->assertArrayHasKey('department_employees', $content->getSchema()->getProperties()); + + // issue #363 association schema should only modify the operations response schema and not the main schema. + $schema = $swagger->getSchemaByName('Employee-Read'); + $this->assertArrayNotHasKey('department_employees', $schema->getProperties()); } public function test_text_plain_mime_type(): void