diff --git a/ApiDocGenerator.php b/ApiDocGenerator.php index 575bf56..6909d96 100644 --- a/ApiDocGenerator.php +++ b/ApiDocGenerator.php @@ -91,12 +91,7 @@ final class ApiDocGenerator return !$processor instanceof \OpenApi\Processors\OperationId; })); - $context = Util::createContext( - // BC for for zircote/swagger-php < 4.2 - method_exists($generator, 'getVersion') - ? ['version' => $generator->getVersion()] - : [] - ); + $context = Util::createContext(['version' => $generator->getVersion()]); $this->openApi = new OpenApi(['_context' => $context]); $modelRegistry = new ModelRegistry($this->modelDescribers, $this->openApi, $this->alternativeNames); diff --git a/Controller/DocumentationController.php b/Controller/DocumentationController.php index 020451a..a09c6a8 100644 --- a/Controller/DocumentationController.php +++ b/Controller/DocumentationController.php @@ -11,6 +11,7 @@ namespace Nelmio\ApiDocBundle\Controller; +use Nelmio\ApiDocBundle\Exception\RenderInvalidArgumentException; use Nelmio\ApiDocBundle\Render\RenderOpenApi; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; @@ -34,7 +35,7 @@ final class DocumentationController return JsonResponse::fromJsonString( $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)); } } diff --git a/Describer/OpenApiPhpDescriber.php b/Describer/OpenApiPhpDescriber.php index 645c11d..1c297c6 100644 --- a/Describer/OpenApiPhpDescriber.php +++ b/Describer/OpenApiPhpDescriber.php @@ -208,11 +208,6 @@ final class OpenApiPhpDescriber */ private function getAttributesAsAnnotation($reflection, \OpenApi\Context $context): array { - // BC zircote/swagger-php < 4.0 - if (!class_exists(AttributeAnnotationFactory::class)) { - return []; - } - $attributesFactory = new AttributeAnnotationFactory(); $attributes = $attributesFactory->build($reflection, $context); // The attributes factory removes the context after executing so we need to set it back... diff --git a/Tests/Functional/Resources/routes.yaml b/Tests/Functional/Resources/routes.yaml index 66e8be6..9b52786 100644 --- a/Tests/Functional/Resources/routes.yaml +++ b/Tests/Functional/Resources/routes.yaml @@ -31,15 +31,15 @@ api_platform: # Controllers doc_area: - path: /docs/{area} + path: /{area}/docs controller: nelmio_api_doc.controller.swagger_ui defaults: area: default doc_json: - path: /docs.json + path: /{area}/docs.json controller: nelmio_api_doc.controller.swagger_json doc_yaml: - path: /docs.yaml + path: /{area}/docs.yaml controller: nelmio_api_doc.controller.swagger_yaml \ No newline at end of file diff --git a/Tests/Functional/SwaggerUiTest.php b/Tests/Functional/SwaggerUiTest.php index 01dc4a0..cbc5917 100644 --- a/Tests/Functional/SwaggerUiTest.php +++ b/Tests/Functional/SwaggerUiTest.php @@ -25,12 +25,12 @@ class SwaggerUiTest extends WebTestCase { 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() { - $crawler = $this->client->request('GET', '/app_dev.php/docs'); + $crawler = $this->client->request('GET', '/app_dev.php/default/docs'); $response = $this->client->getResponse(); $this->assertEquals(200, $response->getStatusCode()); @@ -44,7 +44,7 @@ class SwaggerUiTest extends WebTestCase 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(); $this->assertEquals(200, $response->getStatusCode()); @@ -60,7 +60,7 @@ class SwaggerUiTest extends WebTestCase 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(); $this->assertEquals(200, $response->getStatusCode()); @@ -74,9 +74,17 @@ class SwaggerUiTest extends WebTestCase $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() { - $this->client->request('GET', '/app_dev.php/docs.yaml'); + $this->client->request('GET', '/app_dev.php/default/docs.yaml'); $response = $this->client->getResponse(); $this->assertEquals(200, $response->getStatusCode()); diff --git a/Tests/Functional/TestKernel.php b/Tests/Functional/TestKernel.php index 321d7c8..3f56348 100644 --- a/Tests/Functional/TestKernel.php +++ b/Tests/Functional/TestKernel.php @@ -134,7 +134,16 @@ class TestKernel extends Kernel 'serializer' => ['enable_annotations' => 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('twig', [ diff --git a/Util/SetsContextTrait.php b/Util/SetsContextTrait.php index bf6cba5..c4cb8bb 100644 --- a/Util/SetsContextTrait.php +++ b/Util/SetsContextTrait.php @@ -11,12 +11,7 @@ trait SetsContextTrait { private function setContext(?Context $context): void { - if (class_exists(\OpenApi\Analyser::class)) { - // zircote/swagger-php ^3.2 - \OpenApi\Analyser::$context = $context; - } else { - // zircote/swagger-php ^4.0 - \OpenApi\Generator::$context = $context; - } + // zircote/swagger-php ^4.0 + \OpenApi\Generator::$context = $context; } }