Skip to content

Commit

Permalink
Merge from 2.x (#541)
Browse files Browse the repository at this point in the history
* Mepeiso/add false option to associations white list (#538)

Allow empty whitelist of OpenApiResponse(associations: $var)
---------

Co-authored-by: Manuel Peiso <mepeiso@ticketsauce.com>

* Fix extra slash being added to test src path

* Update how swagger is constructed in the test

---------

Co-authored-by: Manuel Enrique Peiso Cruz <100145779+manuelpeiso@users.noreply.github.com>
Co-authored-by: Manuel Peiso <mepeiso@ticketsauce.com>
  • Loading branch information
3 people committed Feb 25, 2024
1 parent 28de36b commit 0668328
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
8 changes: 7 additions & 1 deletion docs/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,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: [])]
Expand Down
8 changes: 8 additions & 0 deletions src/Lib/Operation/OperationResponseAssociation.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,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 */
Expand Down
20 changes: 20 additions & 0 deletions tests/TestCase/Lib/Operation/OperationResponseAssociationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ public function test_white_list(): void
$this->assertArrayHasKey('employee_titles', $schema->getProperties()['employee']->getProperties());
}

public function test_false_white_list(): void
{
$swagger = (new SwaggerFactory($this->config, new RouteScanner(new Router(), $this->config)))->create();
$assoc = new OperationResponseAssociation(
$swagger,
$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(
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 0668328

Please sign in to comment.