• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 13200284839

07 Feb 2025 12:56PM UTC coverage: 0.0% (-8.2%) from 8.164%
13200284839

Pull #6952

github

web-flow
Merge 519fbf8cc into 62377f880
Pull Request #6952: fix: errors retrieval and documentation

0 of 206 new or added lines in 17 files covered. (0.0%)

10757 existing lines in 366 files now uncovered.

0 of 47781 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/JsonSchema/Command/JsonSchemaGenerateCommand.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\JsonSchema\Command;
15

16
use ApiPlatform\JsonSchema\Schema;
17
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
18
use ApiPlatform\Metadata\HttpOperation;
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Exception\InvalidOptionException;
21
use Symfony\Component\Console\Input\InputArgument;
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

27
/**
28
 * Generates a resource JSON Schema.
29
 *
30
 * @author Jacques Lefebvre <jacques@les-tilleuls.coop>
31
 */
32
final class JsonSchemaGenerateCommand extends Command
33
{
34
    private array $formats;
35

36
    public function __construct(private readonly SchemaFactoryInterface $schemaFactory, array $formats)
37
    {
UNCOV
38
        $this->formats = array_keys($formats);
×
39

UNCOV
40
        parent::__construct();
×
41
    }
42

43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function configure(): void
47
    {
UNCOV
48
        $this
×
UNCOV
49
            ->setDescription('Generates the JSON Schema for a resource operation.')
×
UNCOV
50
            ->addArgument('resource', InputArgument::REQUIRED, 'The Fully Qualified Class Name (FQCN) of the resource')
×
UNCOV
51
            ->addOption('operation', null, InputOption::VALUE_REQUIRED, 'The operation name')
×
UNCOV
52
            ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The response format', (string) $this->formats[0])
×
UNCOV
53
            ->addOption('type', null, InputOption::VALUE_REQUIRED, \sprintf('The type of schema to generate (%s or %s)', Schema::TYPE_INPUT, Schema::TYPE_OUTPUT), Schema::TYPE_INPUT);
×
54
    }
55

56
    /**
57
     * {@inheritdoc}
58
     */
59
    protected function execute(InputInterface $input, OutputInterface $output): int
60
    {
UNCOV
61
        $io = new SymfonyStyle($input, $output);
×
62

63
        /** @var string $resource */
UNCOV
64
        $resource = $input->getArgument('resource');
×
UNCOV
65
        $operation = $input->getOption('operation');
×
66
        /** @var string $format */
UNCOV
67
        $format = $input->getOption('format');
×
68
        /** @var string $type */
UNCOV
69
        $type = $input->getOption('type');
×
70

UNCOV
71
        if (!\in_array($type, [Schema::TYPE_INPUT, Schema::TYPE_OUTPUT], true)) {
×
72
            $io->error(\sprintf('You can only use "%s" or "%s" for the "type" option', Schema::TYPE_INPUT, Schema::TYPE_OUTPUT));
×
73

74
            return 1;
×
75
        }
76

UNCOV
77
        if (!\in_array($format, $this->formats, true)) {
×
78
            throw new InvalidOptionException(\sprintf('The response format "%s" is not supported. Supported formats are : %s.', $format, implode(', ', $this->formats)));
×
79
        }
80

UNCOV
81
        $schema = $this->schemaFactory->buildSchema($resource, $format, $type, $operation ? (new class extends HttpOperation {})->withName($operation) : null);
×
82

UNCOV
83
        if (!$schema->isDefined()) {
×
84
            $io->error(\sprintf('There is no %s defined for the operation "%s" of the resource "%s".', $type, $operation, $resource));
×
85

86
            return 1;
×
87
        }
88

UNCOV
89
        $io->text((string) json_encode($schema, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES));
×
90

UNCOV
91
        return 0;
×
92
    }
93

94
    public static function getDefaultName(): string
95
    {
UNCOV
96
        return 'api:json-schema:generate';
×
97
    }
98
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc