Skip to content

Commit

Permalink
Fixed issue with custom registered API config wasn't taken into consi…
Browse files Browse the repository at this point in the history
…deration when generating server's URL (#445)

* fixed issue with custom api config wont taken into consideration when generating server path

* Fix styling

---------

Co-authored-by: romalytvynenko <[email protected]>
  • Loading branch information
romalytvynenko and romalytvynenko authored Jul 7, 2024
1 parent c53d3de commit 74ea258
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 24 deletions.
15 changes: 6 additions & 9 deletions src/Extensions/OperationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@

namespace Dedoc\Scramble\Extensions;

use Dedoc\Scramble\GeneratorConfig;
use Dedoc\Scramble\Infer;
use Dedoc\Scramble\Support\Generator\Operation;
use Dedoc\Scramble\Support\Generator\TypeTransformer;
use Dedoc\Scramble\Support\RouteInfo;

abstract class OperationExtension
{
protected Infer $infer;

protected TypeTransformer $openApiTransformer;

public function __construct(Infer $infer, TypeTransformer $openApiTransformer)
{
$this->infer = $infer;
$this->openApiTransformer = $openApiTransformer;
}
public function __construct(
protected Infer $infer,
protected TypeTransformer $openApiTransformer,
protected GeneratorConfig $config
) {}

abstract public function handle(Operation $operation, RouteInfo $routeInfo);
}
8 changes: 4 additions & 4 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public function __invoke(?GeneratorConfig $config = null)
$openApi = $this->makeOpenApi($config);

$this->getRoutes($config)
->map(function (Route $route) use ($openApi) {
->map(function (Route $route) use ($openApi, $config) {
try {
return $this->routeToOperation($openApi, $route);
return $this->routeToOperation($openApi, $route, $config);
} catch (Throwable $e) {
if ($e instanceof RouteAware) {
$e->setRoute($route);
Expand Down Expand Up @@ -149,15 +149,15 @@ private function getRoutes(GeneratorConfig $config): Collection
->values();
}

private function routeToOperation(OpenApi $openApi, Route $route)
private function routeToOperation(OpenApi $openApi, Route $route, GeneratorConfig $config)
{
$routeInfo = new RouteInfo($route, $this->fileParser, $this->infer);

if (! $routeInfo->isClassBased()) {
return null;
}

$operation = $this->operationBuilder->build($routeInfo, $openApi);
$operation = $this->operationBuilder->build($routeInfo, $openApi, $config);

$this->ensureSchemaTypes($route, $operation);

Expand Down
4 changes: 3 additions & 1 deletion src/Support/OperationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Dedoc\Scramble\Support;

use Dedoc\Scramble\Extensions\OperationExtension;
use Dedoc\Scramble\GeneratorConfig;
use Dedoc\Scramble\Support\Generator\OpenApi;
use Dedoc\Scramble\Support\Generator\Operation;

Expand All @@ -16,13 +17,14 @@ public function __construct(array $extensionsClasses = [])
$this->extensionsClasses = $extensionsClasses;
}

public function build(RouteInfo $routeInfo, OpenApi $openApi)
public function build(RouteInfo $routeInfo, OpenApi $openApi, GeneratorConfig $config)
{
$operation = new Operation('get');

foreach ($this->extensionsClasses as $extensionClass) {
$extension = app()->make($extensionClass, [
'openApi' => $openApi,
'config' => $config,
]);

$extension->handle($operation, $routeInfo);
Expand Down
16 changes: 6 additions & 10 deletions src/Support/OperationExtensions/RequestEssentialsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Dedoc\Scramble\Support\OperationExtensions;

use Dedoc\Scramble\Extensions\OperationExtension;
use Dedoc\Scramble\GeneratorConfig;
use Dedoc\Scramble\Infer;
use Dedoc\Scramble\PhpDoc\PhpDocTypeHelper;
use Dedoc\Scramble\Scramble;
Expand Down Expand Up @@ -37,19 +38,14 @@

class RequestEssentialsExtension extends OperationExtension
{
private OpenApi $openApi;

private ServerFactory $serverFactory;

public function __construct(
Infer $infer,
TypeTransformer $openApiTransformer,
OpenApi $openApi,
ServerFactory $serverFactory
GeneratorConfig $config,
private OpenApi $openApi,
private ServerFactory $serverFactory
) {
parent::__construct($infer, $openApiTransformer);
$this->openApi = $openApi;
$this->serverFactory = $serverFactory;
parent::__construct($infer, $openApiTransformer, $config);
}

public function handle(Operation $operation, RouteInfo $routeInfo)
Expand Down Expand Up @@ -94,7 +90,7 @@ private function getAlternativeServers(Route $route)
}

[$protocol] = explode('://', url('/'));
$expectedServer = $this->serverFactory->make($protocol.'://'.$route->getDomain().'/'.config('scramble.api_path', 'api'));
$expectedServer = $this->serverFactory->make($protocol.'://'.$route->getDomain().'/'.$this->config->get('api_path', 'api'));

if ($this->isServerMatchesAllGivenServers($expectedServer, $this->openApi->servers)) {
return [];
Expand Down
29 changes: 29 additions & 0 deletions tests/ScrambleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ public function registers_routes_for_default_api()
->and($routes[1]->methods)->toBe(['GET', 'HEAD']);
}

/** @test */
#[DefineEnvironment('registerCustomPathApi')]
#[DefineRoute('registerCustomNewsletterApiRoutes')]
public function generates_correct_server_url_when_api_config_defines_custom_api_path()
{
$generator = app(Generator::class);

$doc = $generator(Scramble::getGeneratorConfig('newsletter'));

$this->assertEquals('http://localhost/newsletter/api', $doc['servers'][0]['url']);
$this->assertEquals(['/a'], array_keys($doc['paths']));
}

/** @test */
#[DefineRoute('registerTestConsumerRoutes')]
public function filters_consumer_routes_with_config_file()
Expand All @@ -89,6 +102,22 @@ public function filters_consumer_routes_with_redefined_resolver_and_api_path_con
$this->assertEquals(['/api/a', '/api/b', '/api/c', '/second-api/a', '/second-api/b', '/second-api/c'], array_keys($doc['paths']));
}

protected function registerCustomPathApi()
{
Scramble::ignoreDefaultRoutes();

Scramble::registerApi('newsletter', [
'api_path' => 'newsletter/api',
]);
}

protected function registerCustomNewsletterApiRoutes(Router $router)
{
$router->group(['prefix' => 'newsletter/api'], function (Router $router) {
$router->get('a', [ScrambleTest_Controller::class, 'test']);
});
}

protected function withEnforcedUnknownSchemaPrevention()
{
Scramble::preventSchema(UnknownType::class);
Expand Down

0 comments on commit 74ea258

Please sign in to comment.