Skip to content
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: 6 additions & 2 deletions src/Lib/Operation/OperationResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,12 @@ private function addPlainText(Response $response, string $mimeType, OpenApiRespo
private function addAssociatedSchema(Response $response, string $mimeType, OpenApiResponse $openApiResponse): bool
{
if (is_array($openApiResponse->associations)) {
$assocSchema = (new OperationResponseAssociation($this->swagger, $this->route, $this->schema))
->build($openApiResponse);
$assocSchema = (new OperationResponseAssociation(
$this->swagger,
$this->config,
$this->route,
$this->schema,
))->build($openApiResponse);
$schema = $this->getMimeTypeSchema(
$mimeType,
$openApiResponse->schemaType,
Expand Down
5 changes: 4 additions & 1 deletion src/Lib/Operation/OperationResponseAssociation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use InvalidArgumentException;
use MixerApi\Core\Model\ModelFactory;
use SwaggerBake\Lib\Attribute\OpenApiResponse;
use SwaggerBake\Lib\Configuration;
use SwaggerBake\Lib\Exception\SwaggerBakeRunTimeException;
use SwaggerBake\Lib\Model\ModelDecorator;
use SwaggerBake\Lib\OpenApi\Schema;
Expand All @@ -39,6 +40,7 @@ class OperationResponseAssociation
*/
public function __construct(
private readonly Swagger $swagger,
private readonly Configuration $config,
private readonly RouteDecorator $route,
private readonly ?Schema $schema = null,
private ?LocatorInterface $locator = null,
Expand Down Expand Up @@ -162,7 +164,8 @@ private function getOrCreateAssociatedSchema(string $schemaName, string $tableNa
if (!$schema) {
$assocTable = $this->locator->get($tableName);
try {
$model = (new ModelFactory(ConnectionManager::get('default'), $assocTable))->create();
$connection = ConnectionManager::get($this->config->getConnectionName());
$model = (new ModelFactory($connection, $assocTable))->create();
} catch (DatabaseException $e) {
throw new SwaggerBakeRunTimeException('Error building association: ' . $e->getMessage());
}
Expand Down
5 changes: 4 additions & 1 deletion tests/TestCase/Lib/MediaType/HalJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function setUp(): void
});

$this->config = new Configuration([
'connectionName' => 'test',
'prefix' => '/',
'yml' => '/config/swagger-bare-bones.yml',
'json' => '/webroot/swagger.json',
Expand Down Expand Up @@ -65,6 +66,7 @@ public function test_item_with_association(): void

$schema = (new OperationResponseAssociation(
$swagger->build(),
$this->config,
$routeScanner->getRoutes()['employees:view'],
null
))->build(new OpenApiResponse(
Expand Down Expand Up @@ -93,6 +95,7 @@ public function test_item_collection_association(): void

$schema = (new OperationResponseAssociation(
$swagger->build(),
$this->config,
$routeScanner->getRoutes()['employees:view'],
null
))->build(new OpenApiResponse(
Expand Down Expand Up @@ -143,4 +146,4 @@ public function test_nested_associations(): void
$this->assertCount(2, $properties['_embedded']['properties']['test_ref_entity']['allOf']);
$this->assertArrayHasKey('test_object', $properties['_embedded']['properties']);
}
}
}
5 changes: 4 additions & 1 deletion tests/TestCase/Lib/MediaType/JsonLdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function setUp(): void
});

$this->config = new Configuration([
'connectionName' => 'test',
'prefix' => '/',
'yml' => '/config/swagger-bare-bones.yml',
'json' => '/webroot/swagger.json',
Expand Down Expand Up @@ -66,6 +67,7 @@ public function test_item_with_association(): void

$schema = (new OperationResponseAssociation(
$swagger->build(),
$this->config,
$routeScanner->getRoutes()['employees:view'],
null
))->build(new OpenApiResponse(
Expand Down Expand Up @@ -95,6 +97,7 @@ public function test_item_collection_association(): void

$schema = (new OperationResponseAssociation(
$swagger->build(),
$this->config,
$routeScanner->getRoutes()['employees:view'],
null
))->build(new OpenApiResponse(
Expand Down Expand Up @@ -147,4 +150,4 @@ public function test_nested_associations(): void
$this->assertCount(2, $object->items->properties->test_ref_entity->allOf);
$this->assertObjectHasProperty('test_object', $object->items->properties);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function setUp(): void
});

$this->config = new Configuration([
'connectionName' => 'test',
'prefix' => '/',
'yml' => '/config/swagger-bare-bones.yml',
'json' => '/webroot/swagger.json',
Expand All @@ -67,6 +68,7 @@ public function test_schema_type_object(): void

$assoc = new OperationResponseAssociation(
$swagger,
$this->config,
$this->routes['employees:view'],
$swagger->getSchemaByName('Employee'),
);
Expand All @@ -88,6 +90,7 @@ public function test_white_list(): void

$assoc = new OperationResponseAssociation(
$swagger,
$this->config,
$this->routes['employees:view'],
$swagger->getSchemaByName('Employee'),
);
Expand All @@ -106,6 +109,7 @@ public function test_false_white_list(): void
$swagger = (new SwaggerFactory($this->config, new RouteScanner(new Router(), $this->config)))->create();
$assoc = new OperationResponseAssociation(
$swagger,
$this->config,
$this->routes['employees:view'],
null
);
Expand All @@ -125,6 +129,7 @@ public function test_null_schema(): void
{
$assoc = new OperationResponseAssociation(
(new SwaggerFactory($this->config, new RouteScanner(new Router(), $this->config)))->create(),
$this->config,
$this->routes['employees:view'],
null
);
Expand All @@ -146,6 +151,7 @@ public function test_invalid_table_throws_exception(): void

(new OperationResponseAssociation(
(new SwaggerFactory($this->config, new RouteScanner(new Router(), $this->config)))->create(),
$this->config,
$this->routes['employees:view'],
null
))->build(new OpenApiResponse(
Expand All @@ -162,6 +168,7 @@ public function test_invalid_schema_mode_exception(): void

(new OperationResponseAssociation(
(new SwaggerFactory($this->config, new RouteScanner(new Router(), $this->config)))->create(),
$this->config,
$route,
null
))->build(new OpenApiResponse(
Expand All @@ -174,6 +181,7 @@ public function test_associate_one(): void
{
$schema = (new OperationResponseAssociation(
(new SwaggerFactory($this->config, new RouteScanner(new Router(), $this->config)))->create(),
$this->config,
$this->routes['employees:view'],
null
))->build(new OpenApiResponse(
Expand All @@ -191,6 +199,7 @@ public function test_associate_throws_exception_when_association_not_found(): vo
$this->expectException(SwaggerBakeRunTimeException::class);
(new OperationResponseAssociation(
(new SwaggerFactory($this->config, new RouteScanner(new Router(), $this->config)))->create(),
$this->config,
$this->routes['employees:view'],
null
))->build(new OpenApiResponse(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_app/src/Controller/OperationPathController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
class OperationPathController extends AppController
{
#[OpenApiPathParam(name: 'id', type: 'integer', format: 'int64', description: 'ID')]
public function pathParameter(string $id = null): void
public function pathParameter(?string $id): void
{

}
}
}