Skip to content

Commit ff0bad9

Browse files
KDederichssoyuka
authored andcommitted
fix: Remove $defaultCommand deprecation (api-platform#4795)
* Overwrite getDefaultName to remove deprecation and preserve BC * Remove comment
1 parent f545e4e commit ff0bad9

File tree

4 files changed

+136
-6
lines changed

4 files changed

+136
-6
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\Command;
15+
16+
use ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer;
17+
use ApiPlatform\Documentation\Documentation;
18+
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
19+
use ApiPlatform\OpenApi\Serializer\ApiGatewayNormalizer;
20+
use Symfony\Component\Console\Command\Command;
21+
use Symfony\Component\Console\Exception\InvalidOptionException;
22+
use Symfony\Component\Console\Input\InputInterface;
23+
use Symfony\Component\Console\Input\InputOption;
24+
use Symfony\Component\Console\Output\OutputInterface;
25+
use Symfony\Component\Console\Style\SymfonyStyle;
26+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
27+
use Symfony\Component\Yaml\Yaml;
28+
29+
/**
30+
* Console command to dump Swagger API documentations.
31+
*
32+
* @author Amrouche Hamza <[email protected]>
33+
*/
34+
final class SwaggerCommand extends Command
35+
{
36+
private $normalizer;
37+
private $resourceNameCollectionFactory;
38+
private $apiTitle;
39+
private $apiDescription;
40+
private $apiVersion;
41+
private $apiFormats;
42+
private $swaggerVersions;
43+
private $legacyMode;
44+
45+
/**
46+
* @param int[] $swaggerVersions
47+
*/
48+
public function __construct(NormalizerInterface $normalizer, ResourceNameCollectionFactoryInterface $resourceNameCollection, string $apiTitle, string $apiDescription, string $apiVersion, array $apiFormats = null, array $swaggerVersions = [2, 3], bool $legacyMode = false)
49+
{
50+
$this->normalizer = $normalizer;
51+
$this->resourceNameCollectionFactory = $resourceNameCollection;
52+
$this->apiTitle = $apiTitle;
53+
$this->apiDescription = $apiDescription;
54+
$this->apiVersion = $apiVersion;
55+
$this->apiFormats = $apiFormats;
56+
$this->swaggerVersions = $swaggerVersions;
57+
$this->legacyMode = $legacyMode;
58+
59+
if (null !== $apiFormats) {
60+
@trigger_error(sprintf('Passing a 6th parameter to the constructor of "%s" is deprecated since API Platform 2.5', __CLASS__), \E_USER_DEPRECATED);
61+
}
62+
63+
parent::__construct();
64+
}
65+
66+
/**
67+
* {@inheritdoc}
68+
*/
69+
protected function configure()
70+
{
71+
$this
72+
->setDescription('Dump the Swagger v2 documentation')
73+
->addOption('yaml', 'y', InputOption::VALUE_NONE, 'Dump the documentation in YAML')
74+
->addOption('spec-version', null, InputOption::VALUE_OPTIONAL, sprintf('OpenAPI version to use (%s)', implode(' or ', $this->swaggerVersions)), (string) ($this->swaggerVersions[0] ?? 2))
75+
->addOption('output', 'o', InputOption::VALUE_OPTIONAL, 'Write output to file')
76+
->addOption('api-gateway', null, InputOption::VALUE_NONE, 'API Gateway compatibility');
77+
}
78+
79+
/**
80+
* {@inheritdoc}
81+
*/
82+
protected function execute(InputInterface $input, OutputInterface $output): int
83+
{
84+
$io = new SymfonyStyle($input, $output);
85+
86+
/** @var string $version */
87+
$version = $input->getOption('spec-version');
88+
89+
if (!\in_array((int) $version, $this->swaggerVersions, true)) {
90+
throw new InvalidOptionException(sprintf('This tool only supports versions %s of the OpenAPI specification ("%s" given).', implode(', ', $this->swaggerVersions), $version));
91+
}
92+
93+
if (3 === (int) $version) {
94+
@trigger_error('The command "api:swagger:export" is deprecated for the spec version 3 use "api:openapi:export".', \E_USER_DEPRECATED);
95+
}
96+
97+
if (!$this->legacyMode) {
98+
@trigger_error('The command "api:swagger:export" is using pre-2.7 metadata, new metadata will not appear, use "api:openapi:export" instead.', \E_USER_DEPRECATED);
99+
}
100+
101+
$documentation = new Documentation($this->resourceNameCollectionFactory->create(), $this->apiTitle, $this->apiDescription, $this->apiVersion, $this->apiFormats);
102+
$data = $this->normalizer->normalize($documentation, DocumentationNormalizer::FORMAT, ['spec_version' => (int) $version, ApiGatewayNormalizer::API_GATEWAY => $input->getOption('api-gateway')]);
103+
$content = $input->getOption('yaml')
104+
? Yaml::dump($data, 10, 2, Yaml::DUMP_OBJECT_AS_MAP | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)
105+
: (json_encode($data, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES) ?: '');
106+
107+
if (!empty($filename = $input->getOption('output')) && \is_string($filename)) {
108+
file_put_contents($filename, $content);
109+
$io->success(sprintf('Data written to %s (specification version %s).', $filename, $version));
110+
} else {
111+
$output->writeln($content);
112+
}
113+
114+
return 0;
115+
}
116+
117+
public static function getDefaultName(): string
118+
{
119+
return 'api:swagger:export';
120+
}
121+
}

src/JsonSchema/Command/JsonSchemaGenerateCommand.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
*/
3232
final class JsonSchemaGenerateCommand extends Command
3333
{
34-
protected static $defaultName = 'api:json-schema:generate';
35-
3634
private SchemaFactoryInterface $schemaFactory;
3735
private $formats;
3836

@@ -94,4 +92,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9492

9593
return 0;
9694
}
95+
96+
public static function getDefaultName(): string
97+
{
98+
return 'api:json-schema:generate';
99+
}
97100
}

src/Symfony/Bundle/Command/GraphQlExportCommand.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
*/
2929
class GraphQlExportCommand extends Command
3030
{
31-
protected static $defaultName = 'api:graphql:export';
32-
3331
private SchemaBuilderInterface $schemaBuilder;
3432

3533
public function __construct(SchemaBuilderInterface $schemaBuilder)
@@ -77,4 +75,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
7775

7876
return 0;
7977
}
78+
79+
public static function getDefaultName(): string
80+
{
81+
return 'api:graphql:export';
82+
}
8083
}

src/Symfony/Bundle/Command/OpenApiCommand.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
*/
3030
final class OpenApiCommand extends Command
3131
{
32-
protected static $defaultName = 'api:openapi:export';
33-
3432
private OpenApiFactoryInterface $openApiFactory;
3533
private NormalizerInterface $normalizer;
3634

@@ -91,4 +89,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9189

9290
return 0;
9391
}
92+
93+
public static function getDefaultName(): string
94+
{
95+
return 'api:openapi:export';
96+
}
9497
}

0 commit comments

Comments
 (0)