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

api-platform / core / 7142557150

08 Dec 2023 02:28PM UTC coverage: 36.003% (-1.4%) from 37.36%
7142557150

push

github

web-flow
fix(jsonld): remove link to ApiDocumentation when doc is disabled (#6029)

0 of 1 new or added line in 1 file covered. (0.0%)

2297 existing lines in 182 files now uncovered.

9992 of 27753 relevant lines covered (36.0%)

147.09 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
    // @noRector \Rector\Php81\Rector\Property\ReadOnlyPropertyRector
35
    private array $formats;
36

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

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

44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected function configure(): void
48
    {
UNCOV
49
        $this
×
UNCOV
50
            ->setDescription('Generates the JSON Schema for a resource operation.')
×
UNCOV
51
            ->addArgument('resource', InputArgument::REQUIRED, 'The Fully Qualified Class Name (FQCN) of the resource')
×
UNCOV
52
            ->addOption('operation', null, InputOption::VALUE_REQUIRED, 'The operation name')
×
UNCOV
53
            ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The response format', (string) $this->formats[0])
×
UNCOV
54
            ->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);
×
55
    }
56

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

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

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

75
            return 1;
×
76
        }
77

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

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

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

87
            return 1;
×
88
        }
89

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

UNCOV
92
        return 0;
×
93
    }
94

95
    public static function getDefaultName(): string
96
    {
UNCOV
97
        return 'api:json-schema:generate';
×
98
    }
99
}
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

© 2026 Coveralls, Inc