Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mepeiso/add false option to associations white list #538

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: [])]
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 @@ -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 */
Expand Down
19 changes: 19 additions & 0 deletions tests/TestCase/Lib/Operation/OperationResponseAssociationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading