Skip to content

Commit

Permalink
fix(openapi): optional yaml component (#6445)
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka authored Jul 1, 2024
1 parent 65ac0d2 commit c086dfe
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/OpenApi/Command/OpenApiCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$data = $this->normalizer->normalize($this->openApiFactory->__invoke(), 'json', [
'spec_version' => $input->getOption('spec-version'),
]);

if ($input->getOption('yaml') && !class_exists(Yaml::class)) {
$output->writeln('The "symfony/yaml" component is not installed.');

return 1;
}

$content = $input->getOption('yaml')
? Yaml::dump($data, 10, 2, Yaml::DUMP_OBJECT_AS_MAP | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK | Yaml::DUMP_NUMERIC_KEY_AS_STRING)
: (json_encode($data, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES) ?: '');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,11 @@ private function registerSwaggerConfiguration(ContainerBuilder $container, array
}

$loader->load('openapi.xml');

if (class_exists(Yaml::class)) {
$loader->load('openapi/yaml.xml');
}

$loader->load('swagger_ui.xml');

if ($config['event_listeners_backward_compatibility_layer']) {
Expand Down
6 changes: 0 additions & 6 deletions src/Symfony/Bundle/Resources/config/openapi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,6 @@
<tag name="serializer.encoder" />
</service>

<service id="api_platform.yamlopenapi.encoder" class="ApiPlatform\Serializer\YamlEncoder" public="false">
<argument>yamlopenapi</argument>
<argument type="service" id="serializer.encoder.yaml" on-invalid="null" />

<tag name="serializer.encoder" />
</service>
</services>

</container>
14 changes: 14 additions & 0 deletions src/Symfony/Bundle/Resources/config/openapi/yaml.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="api_platform.yamlopenapi.encoder" class="ApiPlatform\Serializer\YamlEncoder" public="false">
<argument>yamlopenapi</argument>
<argument type="service" id="serializer.encoder.yaml" on-invalid="null" />

<tag name="serializer.encoder" />
</service>
</services>
</container>

0 comments on commit c086dfe

Please sign in to comment.