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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

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

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

79.17
/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\Attribute\AsCommand;
20
use Symfony\Component\Console\Command\Command;
21
use Symfony\Component\Console\Exception\InvalidOptionException;
22
use Symfony\Component\Console\Input\InputArgument;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Input\InputOption;
25
use Symfony\Component\Console\Output\OutputInterface;
26
use Symfony\Component\Console\Style\SymfonyStyle;
27

28
/**
29
 * Generates a resource JSON Schema.
30
 *
31
 * @author Jacques Lefebvre <jacques@les-tilleuls.coop>
32
 */
33
#[AsCommand(name: 'api:json-schema:generate')]
34
final class JsonSchemaGenerateCommand extends Command
35
{
36
    private array $formats;
37

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

UNCOV
42
        parent::__construct();
20✔
43
    }
44

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

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

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

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

76
            return 1;
×
77
        }
78

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

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

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

88
            return 1;
×
89
        }
90

UNCOV
91
        $io->text((string) json_encode($schema, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES));
20✔
92

UNCOV
93
        return 0;
20✔
94
    }
95
}
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