From af01dc3923ce5ba0a1d28c932211eee343f19bb8 Mon Sep 17 00:00:00 2001 From: Manuel Enrique Peiso Cruz <100145779+manuelpeiso@users.noreply.github.com> Date: Mon, 19 Feb 2024 13:54:48 -0600 Subject: [PATCH 1/3] Mepeiso/add false option to associations white list (#538) Allow empty whitelist of OpenApiResponse(associations: $var) --------- Co-authored-by: Manuel Peiso --- docs/attributes.md | 8 +++++++- .../OperationResponseAssociation.php | 8 ++++++++ .../OperationResponseAssociationTest.php | 19 +++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/docs/attributes.md b/docs/attributes.md index 05b0c12f..b8274acb 100644 --- a/docs/attributes.md +++ b/docs/attributes.md @@ -503,7 +503,13 @@ add the `#[OpenApiSchema]` attribute to your schema class to change the default #### Associations The association property allows you to include associations defined in your Table class within your OpenAPI response -sample schema. To include all immediately associated tables (depth of one): +sample schema. To not include associations: + +```php +#[OpenApiResponse(associations: false)] +``` + +To include all immediately associated tables (depth of one): ```php #[OpenApiResponse(associations: [])] diff --git a/src/Lib/Operation/OperationResponseAssociation.php b/src/Lib/Operation/OperationResponseAssociation.php index 53a99b85..f5839146 100644 --- a/src/Lib/Operation/OperationResponseAssociation.php +++ b/src/Lib/Operation/OperationResponseAssociation.php @@ -71,6 +71,14 @@ public function build(OpenApiResponse $openApiResponse): Schema ->setProperties([]); } + // if $associations['whiteList'] is set to false no associations need to be loaded + if (isset($associations['whiteList']) && $associations['whiteList'] === false) { + $entity = $this->inflector::singularize($table->getAlias()); + $schema = $this->getOrCreateAssociatedSchema($entity, $table->getAlias()); + + return $schema; + } + if (!isset($associations['whiteList']) || !count($associations['whiteList'])) { $associations['whiteList'] = []; /** @var \Cake\ORM\Association $association */ diff --git a/tests/TestCase/Lib/Operation/OperationResponseAssociationTest.php b/tests/TestCase/Lib/Operation/OperationResponseAssociationTest.php index 131b02a2..dbbbc12f 100644 --- a/tests/TestCase/Lib/Operation/OperationResponseAssociationTest.php +++ b/tests/TestCase/Lib/Operation/OperationResponseAssociationTest.php @@ -106,6 +106,25 @@ public function test_white_list(): void $this->assertArrayHasKey('employee_titles', $schema->getProperties()['employee']->getProperties()); } + public function test_false_white_list(): void + { + $assoc = new OperationResponseAssociation( + (new SwaggerFactory($this->config, new RouteScanner($this->router, $this->config)))->create(), + $this->routes['employees:view'], + null + ); + + $schema = $assoc->build(new OpenApiResponse( + schemaType: 'object', + associations: ['whiteList' => false] + )); + + $this->assertInstanceOf(Schema::class, $schema); + $this->assertArrayNotHasKey('department_employees', $schema->getProperties()); + $this->assertArrayNotHasKey('employee_salaries', $schema->getProperties()); + $this->assertArrayNotHasKey('employee_titles', $schema->getProperties()); + } + public function test_null_schema(): void { $assoc = new OperationResponseAssociation( From 8e95439757f047cefff6d43f6c8caf218b592a40 Mon Sep 17 00:00:00 2001 From: Chris Nizzardini Date: Sun, 25 Feb 2024 10:02:21 -0500 Subject: [PATCH 2/3] Fix extra slash being added to test src path --- tests/bootstrap.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 265cb267..8a90e816 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -18,7 +18,7 @@ define('CAKE', CORE_PATH . 'src' . DS); define('TEST_APP', SWAGGER_BAKE_TEST_APP); define('WWW_ROOT', SWAGGER_BAKE_TEST_APP . DS . 'webroot'); -define('APP', SWAGGER_BAKE_TEST_APP . DS . 'src' . DS); +define('APP', SWAGGER_BAKE_TEST_APP . 'src' . DS); define('CONFIG', SWAGGER_BAKE_TEST_APP . 'config' . DS); ini_set('error_reporting', E_ALL); From d8d5cbb4c046f0dcc30de68eb5a2601f2acb71ce Mon Sep 17 00:00:00 2001 From: Chris Nizzardini Date: Sun, 25 Feb 2024 10:03:29 -0500 Subject: [PATCH 3/3] Update how swagger is constructed in the test --- .../Lib/Operation/OperationResponseAssociationTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/TestCase/Lib/Operation/OperationResponseAssociationTest.php b/tests/TestCase/Lib/Operation/OperationResponseAssociationTest.php index ae0e4ceb..eb6e2583 100644 --- a/tests/TestCase/Lib/Operation/OperationResponseAssociationTest.php +++ b/tests/TestCase/Lib/Operation/OperationResponseAssociationTest.php @@ -104,8 +104,9 @@ public function test_white_list(): void public function test_false_white_list(): void { + $swagger = (new SwaggerFactory($this->config, new RouteScanner(new Router(), $this->config)))->create(); $assoc = new OperationResponseAssociation( - (new SwaggerFactory($this->config, new RouteScanner($this->router, $this->config)))->create(), + $swagger, $this->routes['employees:view'], null );