Catch more precise exception in DocumentationController (#2027)

* Catch more precise exception in DocumentationController

* cs

* Fix tests
This commit is contained in:
Guilhem Niot 2022-09-03 13:18:46 +02:00 committed by GitHub
parent 766ed898cf
commit fd45120d5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 31 additions and 28 deletions

View File

@ -91,12 +91,7 @@ final class ApiDocGenerator
return !$processor instanceof \OpenApi\Processors\OperationId; return !$processor instanceof \OpenApi\Processors\OperationId;
})); }));
$context = Util::createContext( $context = Util::createContext(['version' => $generator->getVersion()]);
// BC for for zircote/swagger-php < 4.2
method_exists($generator, 'getVersion')
? ['version' => $generator->getVersion()]
: []
);
$this->openApi = new OpenApi(['_context' => $context]); $this->openApi = new OpenApi(['_context' => $context]);
$modelRegistry = new ModelRegistry($this->modelDescribers, $this->openApi, $this->alternativeNames); $modelRegistry = new ModelRegistry($this->modelDescribers, $this->openApi, $this->alternativeNames);

View File

@ -11,6 +11,7 @@
namespace Nelmio\ApiDocBundle\Controller; namespace Nelmio\ApiDocBundle\Controller;
use Nelmio\ApiDocBundle\Exception\RenderInvalidArgumentException;
use Nelmio\ApiDocBundle\Render\RenderOpenApi; use Nelmio\ApiDocBundle\Render\RenderOpenApi;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -34,7 +35,7 @@ final class DocumentationController
return JsonResponse::fromJsonString( return JsonResponse::fromJsonString(
$this->renderOpenApi->renderFromRequest($request, RenderOpenApi::JSON, $area) $this->renderOpenApi->renderFromRequest($request, RenderOpenApi::JSON, $area)
); );
} catch (InvalidArgumentException $e) { } catch (RenderInvalidArgumentException $e) {
throw new BadRequestHttpException(sprintf('Area "%s" is not supported as it isn\'t defined in config.', $area)); throw new BadRequestHttpException(sprintf('Area "%s" is not supported as it isn\'t defined in config.', $area));
} }
} }

View File

@ -208,11 +208,6 @@ final class OpenApiPhpDescriber
*/ */
private function getAttributesAsAnnotation($reflection, \OpenApi\Context $context): array private function getAttributesAsAnnotation($reflection, \OpenApi\Context $context): array
{ {
// BC zircote/swagger-php < 4.0
if (!class_exists(AttributeAnnotationFactory::class)) {
return [];
}
$attributesFactory = new AttributeAnnotationFactory(); $attributesFactory = new AttributeAnnotationFactory();
$attributes = $attributesFactory->build($reflection, $context); $attributes = $attributesFactory->build($reflection, $context);
// The attributes factory removes the context after executing so we need to set it back... // The attributes factory removes the context after executing so we need to set it back...

View File

@ -31,15 +31,15 @@ api_platform:
# Controllers # Controllers
doc_area: doc_area:
path: /docs/{area} path: /{area}/docs
controller: nelmio_api_doc.controller.swagger_ui controller: nelmio_api_doc.controller.swagger_ui
defaults: defaults:
area: default area: default
doc_json: doc_json:
path: /docs.json path: /{area}/docs.json
controller: nelmio_api_doc.controller.swagger_json controller: nelmio_api_doc.controller.swagger_json
doc_yaml: doc_yaml:
path: /docs.yaml path: /{area}/docs.yaml
controller: nelmio_api_doc.controller.swagger_yaml controller: nelmio_api_doc.controller.swagger_yaml

View File

@ -25,12 +25,12 @@ class SwaggerUiTest extends WebTestCase
{ {
parent::setUp(); parent::setUp();
$this->client = static::createClient([], ['HTTP_HOST' => 'api.example.com', 'PHP_SELF' => '/app_dev.php/docs', 'SCRIPT_FILENAME' => '/var/www/app/web/app_dev.php']); $this->client = static::createClient([], ['HTTP_HOST' => 'api.example.com', 'PHP_SELF' => '/app_dev.php/default/docs', 'SCRIPT_FILENAME' => '/var/www/app/web/app_dev.php']);
} }
public function testSwaggerUi() public function testSwaggerUi()
{ {
$crawler = $this->client->request('GET', '/app_dev.php/docs'); $crawler = $this->client->request('GET', '/app_dev.php/default/docs');
$response = $this->client->getResponse(); $response = $this->client->getResponse();
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
@ -44,7 +44,7 @@ class SwaggerUiTest extends WebTestCase
public function testApiPlatformSwaggerUi() public function testApiPlatformSwaggerUi()
{ {
$crawler = $this->client->request('GET', '/app_dev.php/docs/test'); $crawler = $this->client->request('GET', '/app_dev.php/test/docs');
$response = $this->client->getResponse(); $response = $this->client->getResponse();
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
@ -60,7 +60,7 @@ class SwaggerUiTest extends WebTestCase
public function testJsonDocs() public function testJsonDocs()
{ {
$this->client->request('GET', '/app_dev.php/docs.json'); $this->client->request('GET', '/app_dev.php/default/docs.json');
$response = $this->client->getResponse(); $response = $this->client->getResponse();
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
@ -74,9 +74,17 @@ class SwaggerUiTest extends WebTestCase
$this->assertEquals($expected, json_decode($response->getContent(), true)); $this->assertEquals($expected, json_decode($response->getContent(), true));
} }
public function testInvalidJsonArea()
{
$this->client->request('GET', '/app_dev.php/nonexistent/docs.json');
$response = $this->client->getResponse();
$this->assertEquals(400, $response->getStatusCode());
}
public function testYamlDocs() public function testYamlDocs()
{ {
$this->client->request('GET', '/app_dev.php/docs.yaml'); $this->client->request('GET', '/app_dev.php/default/docs.yaml');
$response = $this->client->getResponse(); $response = $this->client->getResponse();
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());

View File

@ -134,7 +134,16 @@ class TestKernel extends Kernel
'serializer' => ['enable_annotations' => true], 'serializer' => ['enable_annotations' => true],
'property_access' => true, 'property_access' => true,
]; ];
// Support symfony/framework-bundle < 5.4
if (method_exists(\Symfony\Bundle\FrameworkBundle\Command\CachePoolClearCommand::class, 'complete')) {
$framework += [
'exceptions' => [
'Symfony\Component\HttpKernel\Exception\BadRequestHttpException' => [
'log_level' => 'debug',
],
],
];
}
$c->loadFromExtension('framework', $framework); $c->loadFromExtension('framework', $framework);
$c->loadFromExtension('twig', [ $c->loadFromExtension('twig', [

View File

@ -11,12 +11,7 @@ trait SetsContextTrait
{ {
private function setContext(?Context $context): void private function setContext(?Context $context): void
{ {
if (class_exists(\OpenApi\Analyser::class)) { // zircote/swagger-php ^4.0
// zircote/swagger-php ^3.2 \OpenApi\Generator::$context = $context;
\OpenApi\Analyser::$context = $context;
} else {
// zircote/swagger-php ^4.0
\OpenApi\Generator::$context = $context;
}
} }
} }