Skip to content

Commit

Permalink
#242 Fixes path config to work as expected
Browse files Browse the repository at this point in the history
Creates RouteDecorator::templateToOpenApiPath to resolve main bug and
updates unit tests to work with fix.
  • Loading branch information
cnizzardini committed Jan 17, 2021
1 parent dcebe05 commit e5296ae
Show file tree
Hide file tree
Showing 33 changed files with 74 additions and 129 deletions.
40 changes: 1 addition & 39 deletions src/Lib/Path/PathFromRouteFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function create(): ?Path
return null;
}

$path = (new Path())->setResource($this->getResourceName());
$path = (new Path())->setResource($this->route->templateToOpenApiPath());

$swagPath = $this->getSwagPathAnnotation($fqn);

Expand Down Expand Up @@ -91,42 +91,4 @@ private function getSwagPathAnnotation(string $fqns): ?SwagPath

return reset($results);
}

/**
* Returns a routes resource (e.g. /api/model/action)
*
* @return string
*/
private function getResourceName(): string
{
$pieces = $this->getRoutablePieces();

if ($this->config->getPrefix() == '/') {
return implode('/', $pieces);
}

return substr(
implode('/', $pieces),
strlen($this->config->getPrefix())
);
}

/**
* Splits the route (URL) into pieces with forward-slash "/" as the separator after removing path variables
*
* @return string[]
*/
private function getRoutablePieces(): array
{
return array_map(
function ($piece) {
if (substr($piece, 0, 1) == ':') {
return '{' . str_replace(':', '', $piece) . '}';
}

return $piece;
},
explode('/', $this->route->getTemplate())
);
}
}
21 changes: 21 additions & 0 deletions src/Lib/Route/RouteDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,25 @@ public function setControllerFqn(string $controllerFqn)

return $this;
}

/**
* Converts a CakePHP route template to an OpenAPI path
*
* @return string
*/
public function templateToOpenApiPath(): string
{
$pieces = array_map(
function ($piece) {
if (substr($piece, 0, 1) == ':') {
return '{' . str_replace(':', '', $piece) . '}';
}

return $piece;
},
explode('/', $this->template)
);

return implode('/', $pieces);
}
}
32 changes: 1 addition & 31 deletions src/Lib/Swagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ private function buildPathsFromRoutes(): void
$ignorePaths = array_keys($this->array['paths']);

foreach ($routes as $route) {
$resource = $this->convertCakePathToOpenApiResource($route->getTemplate());
$resource = $route->templateToOpenApiPath();

if ($this->hasPathByResource($resource)) {
$path = $this->array['paths'][$resource];
Expand Down Expand Up @@ -399,36 +399,6 @@ private function buildSchemaFromYml(): void
}
}

/**
* Converts Cake path parameters to OpenApi Spec
*
* @example /actor/:id to /actor/{id}
* @param string $resource Resource name
* @return string
*/
private function convertCakePathToOpenApiResource(string $resource): string
{
$pieces = array_map(
function ($piece) {
if (substr($piece, 0, 1) == ':') {
return '{' . str_replace(':', '', $piece) . '}';
}

return $piece;
},
explode('/', $resource)
);

if ($this->config->getPrefix() == '/') {
return implode('/', $pieces);
}

return substr(
implode('/', $pieces),
strlen($this->config->getPrefix())
);
}

/**
* Returns an array of Operation objects that do not have a 200-299 HTTP status code
*
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Command/InstallCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function setUp() : void

public function testExecute()
{
$this->exec('swagger install --config_test ' . $this->configDir, ['Y','/api']);
$this->exec('swagger install --config_test ' . $this->configDir, ['Y','/']);
$this->assertOutputContains('Installation Complete!');
$this->assertFileExists($this->configDir . 'swagger.yml');
$this->assertFileExists($this->configDir . 'swagger_bake.php');
Expand All @@ -42,7 +42,7 @@ public function testExecuteWithExitDoNotOverwriteExisting()

public function testExecuteFileCopyFails()
{
$this->exec('swagger install --config_test /config/no-exists', ['Y','/api']);
$this->exec('swagger install --config_test /config/no-exists', ['Y','/']);
$this->assertExitError();
}

Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Lib/Annotations/SwagDtoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$router = new Router();
$router::scope('/api', function (RouteBuilder $builder) {
$router::scope('/', function (RouteBuilder $builder) {
$builder->setExtensions(['json']);
$builder->resources('Employees', [
'only' => ['dtoPost','dtoQuery'],
Expand All @@ -45,7 +45,7 @@ public function setUp(): void
$this->router = $router;

$this->config = new Configuration([
'prefix' => '/api',
'prefix' => '/',
'yml' => '/config/swagger-bare-bones.yml',
'json' => '/webroot/swagger.json',
'webPath' => '/swagger.json',
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Lib/Annotations/SwagEntityAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$router = new Router();
$router::scope('/api', function (RouteBuilder $builder) {
$router::scope('/', function (RouteBuilder $builder) {
$builder->setExtensions(['json']);
$builder->resources('Employees');
});
$this->router = $router;

$this->config = new Configuration([
'prefix' => '/api',
'prefix' => '/',
'yml' => '/config/swagger-bare-bones.yml',
'json' => '/webroot/swagger.json',
'webPath' => '/swagger.json',
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Lib/Annotations/SwagEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$router = new Router();
$router::scope('/api', function (RouteBuilder $builder) {
$router::scope('/', function (RouteBuilder $builder) {
$builder->setExtensions(['json']);
$builder->resources('Employees', function (RouteBuilder $routes) {
$routes->resources('EmployeeSalaries');
Expand All @@ -46,7 +46,7 @@ public function setUp(): void
$this->router = $router;

$this->config = new Configuration([
'prefix' => '/api',
'prefix' => '/',
'yml' => '/config/swagger-bare-bones.yml',
'json' => '/webroot/swagger.json',
'webPath' => '/swagger.json',
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Lib/Annotations/SwagFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$router = new Router();
$router::scope('/api', function (RouteBuilder $builder) {
$router::scope('/', function (RouteBuilder $builder) {
$builder->setExtensions(['json']);
$builder->resources('Employees', [
'map' => [
Expand All @@ -39,7 +39,7 @@ public function setUp(): void
$this->router = $router;

$this->config = new Configuration([
'prefix' => '/api',
'prefix' => '/',
'yml' => '/config/swagger-bare-bones.yml',
'json' => '/webroot/swagger.json',
'webPath' => '/swagger.json',
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Lib/Annotations/SwagHeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$router = new Router();
$router::scope('/api', function (RouteBuilder $builder) {
$router::scope('/', function (RouteBuilder $builder) {
$builder->setExtensions(['json']);
$builder->resources('Employees', [
'map' => [
Expand All @@ -39,7 +39,7 @@ public function setUp(): void
$this->router = $router;

$this->config = new Configuration([
'prefix' => '/api',
'prefix' => '/',
'yml' => '/config/swagger-bare-bones.yml',
'json' => '/webroot/swagger.json',
'webPath' => '/swagger.json',
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Lib/Annotations/SwagOperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$router = new Router();
$router::scope('/api', function (RouteBuilder $builder) {
$router::scope('/', function (RouteBuilder $builder) {
$builder->setExtensions(['json']);
$builder->resources('Operations', [
'map' => [
Expand All @@ -61,7 +61,7 @@ public function setUp(): void
$this->router = $router;

$this->config = [
'prefix' => '/api',
'prefix' => '/',
'yml' => '/config/swagger-bare-bones.yml',
'json' => '/webroot/swagger.json',
'webPath' => '/swagger.json',
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Lib/Annotations/SwagPaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$router = new Router();
$router::scope('/api', function (RouteBuilder $builder) {
$router::scope('/', function (RouteBuilder $builder) {
$builder->setExtensions(['json']);
$builder->resources('Departments');
});
$this->router = $router;

$this->config = new Configuration([
'prefix' => '/api',
'prefix' => '/',
'yml' => '/config/swagger-bare-bones.yml',
'json' => '/webroot/swagger.json',
'webPath' => '/swagger.json',
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Lib/Annotations/SwagPathParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$router = new Router();
$router::scope('/api', function (RouteBuilder $builder) {
$router::scope('/', function (RouteBuilder $builder) {
$builder->setExtensions(['json']);
$builder->resources('OperationPath', [
'only' => ['pathParameter'],
Expand All @@ -39,7 +39,7 @@ public function setUp(): void
$this->router = $router;

$this->config = new Configuration([
'prefix' => '/api',
'prefix' => '/',
'yml' => '/config/swagger-bare-bones.yml',
'json' => '/webroot/swagger.json',
'webPath' => '/swagger.json',
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Lib/Annotations/SwagPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$router = new Router();
$router::scope('/api', function (RouteBuilder $builder) {
$router::scope('/', function (RouteBuilder $builder) {
$builder->setExtensions(['json']);
$builder->resources('Employees', function (RouteBuilder $routes) {
$routes->resources('EmployeeTitles');
Expand All @@ -41,7 +41,7 @@ public function setUp(): void
$this->router = $router;

$this->config = [
'prefix' => '/api',
'prefix' => '/',
'yml' => '/config/swagger-bare-bones.yml',
'json' => '/webroot/swagger.json',
'webPath' => '/swagger.json',
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Lib/Annotations/SwagQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$router = new Router();
$router::scope('/api', function (RouteBuilder $builder) {
$router::scope('/', function (RouteBuilder $builder) {
$builder->setExtensions(['json']);
$builder->resources('Departments');
});
$this->router = $router;

$this->config = new Configuration([
'prefix' => '/api',
'prefix' => '/',
'yml' => '/config/swagger-bare-bones.yml',
'json' => '/webroot/swagger.json',
'webPath' => '/swagger.json',
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Lib/Annotations/SwagRequestBodyContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$router = new Router();
$router::scope('/api', function (RouteBuilder $builder) {
$router::scope('/', function (RouteBuilder $builder) {
$builder->setExtensions(['json']);
$builder->resources('SwagRequestBodyContent', [
'map' => [
Expand All @@ -49,7 +49,7 @@ public function setUp(): void
$this->router = $router;

$this->config = new Configuration([
'prefix' => '/api',
'prefix' => '/',
'yml' => '/config/swagger-bare-bones.yml',
'json' => '/webroot/swagger.json',
'webPath' => '/swagger.json',
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Lib/Annotations/SwagRequestBodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$router = new Router();
$router::scope('/api', function (RouteBuilder $builder) {
$router::scope('/', function (RouteBuilder $builder) {
$builder->setExtensions(['json']);
$builder->resources('Employees', [
'map' => [
Expand All @@ -39,7 +39,7 @@ public function setUp(): void
$this->router = $router;

$this->config = new Configuration([
'prefix' => '/api',
'prefix' => '/',
'yml' => '/config/swagger-bare-bones.yml',
'json' => '/webroot/swagger.json',
'webPath' => '/swagger.json',
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Lib/Annotations/SwagResponseSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$router = new Router();
$router::scope('/api', function (RouteBuilder $builder) {
$router::scope('/', function (RouteBuilder $builder) {
$builder->setExtensions(['json']);
$builder->resources('Employees', [
'map' => [
Expand All @@ -52,7 +52,7 @@ public function setUp(): void
$this->router = $router;

$this->config = new Configuration([
'prefix' => '/api',
'prefix' => '/',
'yml' => '/config/swagger-with-existing.yml',
'json' => '/webroot/swagger.json',
'webPath' => '/swagger.json',
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Lib/Annotations/SwagSecurityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$router = new Router();
$router::scope('/api', function (RouteBuilder $builder) {
$router::scope('/', function (RouteBuilder $builder) {
$builder->setExtensions(['json']);
$builder->resources('Employees', [
'map' => [
Expand All @@ -39,7 +39,7 @@ public function setUp(): void
$this->router = $router;

$this->config = new Configuration([
'prefix' => '/api',
'prefix' => '/',
'yml' => '/config/swagger-bare-bones.yml',
'json' => '/webroot/swagger.json',
'webPath' => '/swagger.json',
Expand Down
Loading

0 comments on commit e5296ae

Please sign in to comment.