-
-
Notifications
You must be signed in to change notification settings - Fork 960
add command to generate json schema #2996
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dunglas
merged 2 commits into
api-platform:master
from
jockos:add-json-schema-generate-command
Aug 21, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ApiPlatform\Core\JsonSchema\Command; | ||
|
|
||
| use ApiPlatform\Core\Api\OperationType; | ||
| use ApiPlatform\Core\JsonSchema\SchemaFactoryInterface; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Exception\InvalidOptionException; | ||
| use Symfony\Component\Console\Input\InputArgument; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
| use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
|
||
| /** | ||
| * Generates a resource JSON Schema. | ||
| * | ||
| * @author Jacques Lefebvre <jacques@les-tilleuls.coop> | ||
| */ | ||
| final class JsonSchemaGenerateCommand extends Command | ||
| { | ||
| private $schemaFactory; | ||
| private $formats; | ||
|
|
||
| public function __construct(SchemaFactoryInterface $schemaFactory, array $formats) | ||
| { | ||
| $this->schemaFactory = $schemaFactory; | ||
| $this->formats = array_keys($formats); | ||
|
|
||
| parent::__construct(); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| protected function configure() | ||
| { | ||
| $this | ||
| ->setName('api:json-schema:generate') | ||
| ->setDescription('Generates the JSON Schema for a resource operation.') | ||
| ->addArgument('resource', InputArgument::REQUIRED, 'The Fully Qualified Class Name (FQCN) of the resource') | ||
| ->addOption('itemOperation', null, InputOption::VALUE_REQUIRED, 'The item operation') | ||
| ->addOption('collectionOperation', null, InputOption::VALUE_REQUIRED, 'The collection operation') | ||
| ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The response format', (string) $this->formats[0]) | ||
| ->addOption('type', null, InputOption::VALUE_REQUIRED, 'The type of schema to generate (input or output)', 'input'); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| protected function execute(InputInterface $input, OutputInterface $output) | ||
| { | ||
| $io = new SymfonyStyle($input, $output); | ||
|
|
||
| /** @var string $resource */ | ||
| $resource = $input->getArgument('resource'); | ||
| /** @var ?string $itemOperation */ | ||
| $itemOperation = $input->getOption('itemOperation'); | ||
| /** @var ?string $collectionOperation */ | ||
| $collectionOperation = $input->getOption('collectionOperation'); | ||
| /** @var string $format */ | ||
| $format = $input->getOption('format'); | ||
| /** @var string $outputType */ | ||
| $outputType = $input->getOption('type'); | ||
|
|
||
| if (!\in_array($outputType, ['input', 'output'], true)) { | ||
| $io->error('You can only use "input" or "output" for the "type" option'); | ||
|
|
||
| return 1; | ||
| } | ||
|
|
||
| if (!\in_array($format, $this->formats, true)) { | ||
| throw new InvalidOptionException(sprintf('The response format "%s" is not supported. Supported formats are : %s.', $format, implode(', ', $this->formats))); | ||
| } | ||
|
|
||
| /** @var ?string $operationType */ | ||
| $operationType = null; | ||
| /** @var ?string $operationName */ | ||
| $operationName = null; | ||
|
|
||
| if ($itemOperation && $collectionOperation) { | ||
| $io->error('You can only use one of "--itemOperation" and "--collectionOperation" options at the same time.'); | ||
|
|
||
| return 1; | ||
| } | ||
|
|
||
| if (null !== $itemOperation || null !== $collectionOperation) { | ||
| $operationType = $itemOperation ? OperationType::ITEM : OperationType::COLLECTION; | ||
| $operationName = $itemOperation ?? $collectionOperation; | ||
| } | ||
|
|
||
| $schema = $this->schemaFactory->buildSchema($resource, $format, 'output' === $outputType, $operationType, $operationName); | ||
|
|
||
| if (null !== $operationType && null !== $operationName && !$schema->isDefined()) { | ||
| $io->error(sprintf('There is no %ss defined for the operation "%s" of the resource "%s".', $outputType, $operationName, $resource)); | ||
|
|
||
| return 1; | ||
| } | ||
|
|
||
| $io->text((string) json_encode($schema, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); | ||
|
|
||
| return 0; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ApiPlatform\Core\Tests\JsonSchema\Command; | ||
|
|
||
| use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Dummy as DocumentDummy; | ||
| use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy; | ||
| use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
| use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
| use Symfony\Component\Console\Tester\ApplicationTester; | ||
|
|
||
| /** | ||
| * @author Jacques Lefebvre <jacques@les-tilleuls.coop> | ||
| */ | ||
| class JsonSchemaGenerateCommandTest extends KernelTestCase | ||
| { | ||
| /** | ||
| * @var ApplicationTester | ||
| */ | ||
| private $tester; | ||
|
|
||
| private $entityClass; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $kernel = self::bootKernel(); | ||
|
|
||
| $application = new Application(static::$kernel); | ||
| $application->setCatchExceptions(true); | ||
| $application->setAutoExit(false); | ||
|
|
||
| $this->entityClass = 'mongodb' === $kernel->getEnvironment() ? DocumentDummy::class : Dummy::class; | ||
| $this->tester = new ApplicationTester($application); | ||
| } | ||
|
|
||
| public function testExecuteWithoutOption() | ||
| { | ||
| $this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass]); | ||
|
|
||
| $this->assertJson($this->tester->getDisplay()); | ||
| } | ||
|
|
||
| public function testExecuteWithItemOperationGet() | ||
| { | ||
| $this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--itemOperation' => 'get', '--type' => 'output']); | ||
|
|
||
| $this->assertJson($this->tester->getDisplay()); | ||
| } | ||
|
|
||
| public function testExecuteWithCollectionOperationGet() | ||
| { | ||
| $this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--collectionOperation' => 'get', '--type' => 'output']); | ||
|
|
||
| $this->assertJson($this->tester->getDisplay()); | ||
| } | ||
|
|
||
| public function testExecuteWithTooManyOptions() | ||
| { | ||
| $this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--collectionOperation' => 'get', '--itemOperation' => 'get', '--type' => 'output']); | ||
|
|
||
| $this->assertStringStartsWith('[ERROR] You can only use one of "--itemOperation" and "--collectionOperation"', trim(str_replace(["\r", "\n"], '', $this->tester->getDisplay()))); | ||
| } | ||
|
|
||
| public function testExecuteWithJsonldFormatOption() | ||
| { | ||
| $this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--collectionOperation' => 'post', '--format' => 'jsonld']); | ||
| $result = $this->tester->getDisplay(); | ||
|
|
||
| $this->assertStringContainsString('@id', $result); | ||
| $this->assertStringContainsString('@context', $result); | ||
| $this->assertStringContainsString('@type', $result); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.