Skip to content

Commit bcf2a46

Browse files
authored
Merge pull request api-platform#3837 from WhiteRabbitDE/feature/make-all-swagger-info-fields-configurable
OpenAPI - make all info model fields configurable
2 parents ab474b6 + 621ef66 commit bcf2a46

File tree

9 files changed

+121
-13
lines changed

9 files changed

+121
-13
lines changed

src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public function load(array $configs, ContainerBuilder $container): void
106106
$this->registerCommonConfiguration($container, $config, $loader, $formats, $patchFormats, $errorFormats);
107107
$this->registerMetadataConfiguration($container, $config, $loader);
108108
$this->registerOAuthConfiguration($container, $config);
109+
$this->registerOpenApiConfiguration($container, $config);
109110
$this->registerSwaggerConfiguration($container, $config, $loader);
110111
$this->registerJsonApiConfiguration($formats, $loader);
111112
$this->registerJsonLdHydraConfiguration($container, $formats, $loader, $config['enable_docs']);
@@ -710,6 +711,16 @@ private function registerSecurityConfiguration(ContainerBuilder $container, XmlF
710711
}
711712
}
712713

714+
private function registerOpenApiConfiguration(ContainerBuilder $container, array $config): void
715+
{
716+
$container->setParameter('api_platform.openapi.termsOfService', $config['openapi']['termsOfService']);
717+
$container->setParameter('api_platform.openapi.contact.name', $config['openapi']['contact']['name']);
718+
$container->setParameter('api_platform.openapi.contact.url', $config['openapi']['contact']['url']);
719+
$container->setParameter('api_platform.openapi.contact.email', $config['openapi']['contact']['email']);
720+
$container->setParameter('api_platform.openapi.license.name', $config['openapi']['license']['name']);
721+
$container->setParameter('api_platform.openapi.license.url', $config['openapi']['license']['url']);
722+
}
723+
713724
private function buildDeprecationArgs(string $version, string $message): array
714725
{
715726
return method_exists(Definition::class, 'getDeprecation')

src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ public function getConfigTreeBuilder()
201201
$this->addMercureSection($rootNode);
202202
$this->addMessengerSection($rootNode);
203203
$this->addElasticsearchSection($rootNode);
204+
$this->addOpenApiSection($rootNode);
204205

205206
$this->addExceptionToStatusSection($rootNode);
206207

@@ -467,6 +468,34 @@ private function addElasticsearchSection(ArrayNodeDefinition $rootNode): void
467468
->end();
468469
}
469470

471+
private function addOpenApiSection(ArrayNodeDefinition $rootNode): void
472+
{
473+
$rootNode
474+
->children()
475+
->arrayNode('openapi')
476+
->addDefaultsIfNotSet()
477+
->children()
478+
->arrayNode('contact')
479+
->addDefaultsIfNotSet()
480+
->children()
481+
->scalarNode('name')->defaultNull()->info('The identifying name of the contact person/organization.')->end()
482+
->scalarNode('url')->defaultNull()->info('The URL pointing to the contact information. MUST be in the format of a URL.')->end()
483+
->scalarNode('email')->defaultNull()->info('The email address of the contact person/organization. MUST be in the format of an email address.')->end()
484+
->end()
485+
->end()
486+
->scalarNode('termsOfService')->defaultNull()->info('A URL to the Terms of Service for the API. MUST be in the format of a URL.')->end()
487+
->arrayNode('license')
488+
->addDefaultsIfNotSet()
489+
->children()
490+
->scalarNode('name')->defaultNull()->info('The license name used for the API.')->end()
491+
->scalarNode('url')->defaultNull()->info('URL to the license used for the API. MUST be in the format of a URL.')->end()
492+
->end()
493+
->end()
494+
->end()
495+
->end()
496+
->end();
497+
}
498+
470499
/**
471500
* @throws InvalidConfigurationException
472501
*/

src/Bridge/Symfony/Bundle/Resources/config/openapi.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
<argument>%api_platform.oauth.refreshUrl%</argument>
2424
<argument>%api_platform.oauth.scopes%</argument>
2525
<argument>%api_platform.swagger.api_keys%</argument>
26+
<argument>%api_platform.openapi.contact.name%</argument>
27+
<argument>%api_platform.openapi.contact.url%</argument>
28+
<argument>%api_platform.openapi.contact.email%</argument>
29+
<argument>%api_platform.openapi.termsOfService%</argument>
30+
<argument>%api_platform.openapi.license.name%</argument>
31+
<argument>%api_platform.openapi.license.url%</argument>
2632
</service>
2733
<service id="ApiPlatform\Core\OpenApi\Options" alias="api_platform.openapi.options" />
2834

src/OpenApi/Factory/OpenApiFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ public function __construct(ResourceNameCollectionFactoryInterface $resourceName
7777
public function __invoke(array $context = []): OpenApi
7878
{
7979
$baseUrl = $context[self::BASE_URL] ?? '/';
80-
$info = new Model\Info($this->openApiOptions->getTitle(), $this->openApiOptions->getVersion(), trim($this->openApiOptions->getDescription()));
80+
$contact = null === $this->openApiOptions->getContactUrl() || null === $this->openApiOptions->getContactEmail() ? null : new Model\Contact($this->openApiOptions->getContactName(), $this->openApiOptions->getContactUrl(), $this->openApiOptions->getContactEmail());
81+
$license = null === $this->openApiOptions->getLicenseName() ? null : new Model\License($this->openApiOptions->getLicenseName(), $this->openApiOptions->getLicenseUrl());
82+
$info = new Model\Info($this->openApiOptions->getTitle(), $this->openApiOptions->getVersion(), trim($this->openApiOptions->getDescription()), $this->openApiOptions->getTermsOfService(), $contact, $license);
8183
$servers = '/' === $baseUrl || '' === $baseUrl ? [new Model\Server('/')] : [new Model\Server($baseUrl)];
8284
$paths = new Model\Paths();
8385
$links = [];

src/OpenApi/Model/Contact.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,45 +21,45 @@ final class Contact
2121
private $url;
2222
private $email;
2323

24-
public function __construct(string $name = '', string $url = '', string $email = '')
24+
public function __construct(string $name = null, string $url = null, string $email = null)
2525
{
2626
$this->name = $name;
2727
$this->url = $url;
2828
$this->email = $email;
2929
}
3030

31-
public function getName(): string
31+
public function getName(): ?string
3232
{
3333
return $this->name;
3434
}
3535

36-
public function getUrl(): string
36+
public function getUrl(): ?string
3737
{
3838
return $this->url;
3939
}
4040

41-
public function getEmail(): string
41+
public function getEmail(): ?string
4242
{
4343
return $this->email;
4444
}
4545

46-
public function withName(string $name): self
46+
public function withName(?string $name): self
4747
{
4848
$clone = clone $this;
4949
$clone->name = $name;
5050

5151
return $clone;
5252
}
5353

54-
public function withUrl(string $url): self
54+
public function withUrl(?string $url): self
5555
{
5656
$clone = clone $this;
5757
$clone->url = $url;
5858

5959
return $clone;
6060
}
6161

62-
public function withEmail(string $email): self
62+
public function withEmail(?string $email): self
6363
{
6464
$clone = clone $this;
6565
$clone->email = $email;

src/OpenApi/Model/License.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class License
2020
private $name;
2121
private $url;
2222

23-
public function __construct(string $name, string $url)
23+
public function __construct(string $name, string $url = null)
2424
{
2525
$this->name = $name;
2626
$this->url = $url;
@@ -31,7 +31,7 @@ public function getName(): string
3131
return $this->name;
3232
}
3333

34-
public function getUrl(): string
34+
public function getUrl(): ?string
3535
{
3636
return $this->url;
3737
}
@@ -44,7 +44,7 @@ public function withName(string $name): self
4444
return $clone;
4545
}
4646

47-
public function withUrl(string $url): self
47+
public function withUrl(?string $url): self
4848
{
4949
$clone = clone $this;
5050
$clone->url = $url;

src/OpenApi/Options.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@ final class Options
2626
private $oAuthRefreshUrl;
2727
private $oAuthScopes;
2828
private $apiKeys;
29-
30-
public function __construct(string $title, string $description = '', string $version = '', bool $oAuthEnabled = false, string $oAuthType = '', string $oAuthFlow = '', string $oAuthTokenUrl = '', string $oAuthAuthorizationUrl = '', string $oAuthRefreshUrl = '', array $oAuthScopes = [], array $apiKeys = [])
29+
private $contactName;
30+
private $contactUrl;
31+
private $contactEmail;
32+
private $termsOfService;
33+
private $licenseName;
34+
private $licenseUrl;
35+
36+
public function __construct(string $title, string $description = '', string $version = '', bool $oAuthEnabled = false, string $oAuthType = '', string $oAuthFlow = '', string $oAuthTokenUrl = '', string $oAuthAuthorizationUrl = '', string $oAuthRefreshUrl = '', array $oAuthScopes = [], array $apiKeys = [], string $contactName = null, string $contactUrl = null, string $contactEmail = null, string $termsOfService = null, string $licenseName = null, string $licenseUrl = null)
3137
{
3238
$this->title = $title;
3339
$this->description = $description;
@@ -40,6 +46,12 @@ public function __construct(string $title, string $description = '', string $ver
4046
$this->oAuthRefreshUrl = $oAuthRefreshUrl;
4147
$this->oAuthScopes = $oAuthScopes;
4248
$this->apiKeys = $apiKeys;
49+
$this->contactName = $contactName;
50+
$this->contactUrl = $contactUrl;
51+
$this->contactEmail = $contactEmail;
52+
$this->termsOfService = $termsOfService;
53+
$this->licenseName = $licenseName;
54+
$this->licenseUrl = $licenseUrl;
4355
}
4456

4557
public function getTitle(): string
@@ -96,4 +108,34 @@ public function getApiKeys(): array
96108
{
97109
return $this->apiKeys;
98110
}
111+
112+
public function getContactName(): ?string
113+
{
114+
return $this->contactName;
115+
}
116+
117+
public function getContactUrl(): ?string
118+
{
119+
return $this->contactUrl;
120+
}
121+
122+
public function getContactEmail(): ?string
123+
{
124+
return $this->contactEmail;
125+
}
126+
127+
public function getTermsOfService(): ?string
128+
{
129+
return $this->termsOfService;
130+
}
131+
132+
public function getLicenseName(): ?string
133+
{
134+
return $this->licenseName;
135+
}
136+
137+
public function getLicenseUrl(): ?string
138+
{
139+
return $this->licenseUrl;
140+
}
99141
}

tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,12 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
11421142
'api_platform.elasticsearch.enabled' => false,
11431143
'api_platform.asset_package' => null,
11441144
'api_platform.defaults' => ['attributes' => ['stateless' => true]],
1145+
'api_platform.openapi.termsOfService' => null,
1146+
'api_platform.openapi.contact.name' => null,
1147+
'api_platform.openapi.contact.url' => null,
1148+
'api_platform.openapi.contact.email' => null,
1149+
'api_platform.openapi.license.name' => null,
1150+
'api_platform.openapi.license.url' => null,
11451151
];
11461152

11471153
if ($hasSwagger) {

tests/Bridge/Symfony/Bundle/DependencyInjection/ConfigurationTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,18 @@ private function runDefaultConfigTests(array $doctrineIntegrationsToLoad = ['orm
207207
'allow_plain_identifiers' => false,
208208
'resource_class_directories' => [],
209209
'asset_package' => null,
210+
'openapi' => [
211+
'contact' => [
212+
'name' => null,
213+
'url' => null,
214+
'email' => null,
215+
],
216+
'termsOfService' => null,
217+
'license' => [
218+
'name' => null,
219+
'url' => null,
220+
],
221+
],
210222
], $config);
211223
}
212224

0 commit comments

Comments
 (0)