From b3c53beeadae18dce33fe04f473441a7374b824c Mon Sep 17 00:00:00 2001 From: Mantis Development Date: Sun, 31 May 2020 10:39:50 +0100 Subject: [PATCH] Add dump command to print out swagger JSON. (#1537) (#1540) * Add dump command to print out swagger JSON. Command outputs to stdout, so can be redirected to a file. Pretty prints output by default. --no-pretty can be passed to remove whitespace from json output if it does not need to be human readable * Update for styleci corrections * Fix CS and add type hint * REbase + Updates to documentation of dump command. * Remove defaultName - config file specifies this Add return value to dump command * Update for OA3 * Add a test case * update command name * Fix the tests Co-authored-by: Guilhem Niot Co-authored-by: Guilhem Niot --- Command/DumpCommand.php | 78 +++++++++++++++++++++++++++++++ Resources/config/services.xml | 6 +++ Tests/Command/DumpCommandTest.php | 36 ++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 Command/DumpCommand.php create mode 100644 Tests/Command/DumpCommandTest.php diff --git a/Command/DumpCommand.php b/Command/DumpCommand.php new file mode 100644 index 0000000..8b86b23 --- /dev/null +++ b/Command/DumpCommand.php @@ -0,0 +1,78 @@ +generatorLocator = $generatorLocator; + + parent::__construct(); + } + + /** + * Configures the dump command. + */ + protected function configure() + { + $this + ->setDescription('Dumps documentation in OpenAPI JSON format') + ->addOption('area', '', InputOption::VALUE_OPTIONAL, '', 'default') + ->addOption('no-pretty', '', InputOption::VALUE_NONE, 'Do not pretty format output') + ; + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * + * @throws InvalidArgumentException If the area to dump is not valid + * + * @return int|void + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $area = $input->getOption('area'); + + if (!$this->generatorLocator->has($area)) { + throw new InvalidArgumentException(sprintf('Area "%s" is not supported.', $area)); + } + + $spec = $this->generatorLocator->get($area)->generate(); + + if ($input->hasParameterOption(['--no-pretty'])) { + $output->writeln(json_encode($spec)); + } else { + $output->writeln(json_encode($spec, JSON_PRETTY_PRINT)); + } + + return 0; + } +} diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 91cda00..67a499d 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -4,6 +4,12 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> + + + + + + diff --git a/Tests/Command/DumpCommandTest.php b/Tests/Command/DumpCommandTest.php new file mode 100644 index 0000000..2ccb15e --- /dev/null +++ b/Tests/Command/DumpCommandTest.php @@ -0,0 +1,36 @@ +find('nelmio:apidoc:dump'); + $commandTester = new CommandTester($command); + $commandTester->execute([ + '--area' => 'test', + '--no-pretty' => '', + ]); + + // the output of the command in the console + $output = $commandTester->getDisplay(); + $this->assertEquals(json_encode($this->getOpenApiDefinition('test'))."\n", $output); + } +}