Skip to content

Commit ba2aeef

Browse files
committed
Fix OperationResponseAssociation ignoring controller defaultTable
When associations['table'] is not explicitly set, OperationResponseAssociation::build() now resolves the table via the controller's fetchTable() method, which respects $defaultTable, before falling back to the controller name. This aligns with the pattern already used in ModelScanner::routeHasModel().
1 parent 03a7d1c commit ba2aeef

3 files changed

Lines changed: 49 additions & 2 deletions

File tree

src/Lib/Operation/OperationResponseAssociation.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,16 @@ public function build(OpenApiResponse $openApiResponse): Schema
5959
{
6060
$associations = $openApiResponse->associations;
6161
if (!isset($associations['table'])) {
62-
$associations['table'] = $this->route->getController();
62+
$controller = $this->route->getControllerInstance();
63+
if ($controller !== null && method_exists($controller, 'fetchTable')) {
64+
try {
65+
$associations['table'] = $controller->fetchTable()->getAlias();
66+
} catch (\Throwable) {
67+
$associations['table'] = $this->route->getController();
68+
}
69+
} else {
70+
$associations['table'] = $this->route->getController();
71+
}
6372
}
6473

6574
$table = $this->locator->get($associations['table']);

tests/TestCase/Lib/Operation/OperationResponseAssociationTest.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ class OperationResponseAssociationTest extends TestCase
3535

3636
public function setUp(): void
3737
{
38-
parent::setUp(); // TODO: Change the autogenerated stub
38+
parent::setUp();
3939
Router::createRouteBuilder('/')->scope('/', function (RouteBuilder $builder) {
4040
$builder->setExtensions(['json']);
4141
$builder->resources('Employees');
42+
$builder->resources('CustomTable');
4243
});
4344

4445
$this->config = new Configuration([
@@ -199,4 +200,24 @@ public function test_associate_throws_exception_when_association_not_found(): vo
199200
));
200201
$this->expectExceptionMessageMatches('/OpenApiResponse association not found/');
201202
}
203+
204+
public function test_default_table_property_is_respected(): void
205+
{
206+
$swagger = (new SwaggerFactory($this->config, new RouteScanner(new Router(), $this->config)))->create();
207+
208+
$assoc = new OperationResponseAssociation(
209+
$swagger,
210+
$this->routes['customtable:view'],
211+
);
212+
213+
$schema = $assoc->build(new OpenApiResponse(
214+
schemaType: 'object',
215+
associations: [],
216+
));
217+
218+
$this->assertInstanceOf(Schema::class, $schema);
219+
$this->assertArrayHasKey('department_employees', $schema->getProperties());
220+
$this->assertArrayHasKey('employee_salaries', $schema->getProperties());
221+
$this->assertArrayHasKey('employee_titles', $schema->getProperties());
222+
}
202223
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace SwaggerBakeTest\App\Controller;
5+
6+
class CustomTableController extends AppController
7+
{
8+
public ?string $defaultTable = 'Employees';
9+
10+
public function index(): void
11+
{
12+
}
13+
14+
public function view($id = null): void
15+
{
16+
}
17+
}

0 commit comments

Comments
 (0)