mirror of
https://github.com/Sylius/Sylius.git
synced 2026-07-14 17:10:53 +00:00
feature #15366 [Api][Attribute] Add types enum to product attribute schema (NoResponseMate)
This PR was merged into the 1.13 branch.
Discussion
----------
| Q | A |
|-----------------|--------------------------------------------------------------|
| Branch? | 1.13 |
| Bug fix? | no |
| New feature? | yes? |
| BC breaks? | no |
| Deprecations? | no |
| Related tickets | continues #15288 |
| License | MIT |
Commits
-------
7205489a60 [Api][Attribute] Add types enum to product attribute schema
This commit is contained in:
commit
564fe783e6
2 changed files with 86 additions and 0 deletions
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Sylius package.
|
||||
*
|
||||
* (c) Sylius Sp. z o.o.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Sylius\Bundle\ApiBundle\OpenApi\Documentation;
|
||||
|
||||
use ApiPlatform\OpenApi\OpenApi;
|
||||
use Sylius\Component\Attribute\AttributeType\AttributeTypeInterface;
|
||||
use Sylius\Component\Registry\ServiceRegistryInterface;
|
||||
|
||||
/** @experimental */
|
||||
final class AttributeTypeDocumentationModifier implements DocumentationModifierInterface
|
||||
{
|
||||
public function __construct(
|
||||
private ServiceRegistryInterface $attributeTypeRegistry,
|
||||
) {
|
||||
}
|
||||
|
||||
public function modify(OpenApi $docs): OpenApi
|
||||
{
|
||||
$components = $docs->getComponents();
|
||||
$schemas = $components->getSchemas();
|
||||
if (null === $schemas) {
|
||||
return $docs;
|
||||
}
|
||||
|
||||
$schemas = $this->updateAttributeTypesSchema($schemas);
|
||||
|
||||
return $docs->withComponents(
|
||||
$components->withSchemas($schemas),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \ArrayObject<string, mixed> $schemas
|
||||
*
|
||||
* @return \ArrayObject<string, mixed>
|
||||
*/
|
||||
private function updateAttributeTypesSchema(\ArrayObject $schemas): \ArrayObject
|
||||
{
|
||||
$attributeTypes = $this->getAttributeTypes();
|
||||
|
||||
$schemasToBeUpdated = [
|
||||
'ProductAttribute.admin.product_attribute.read',
|
||||
'ProductAttribute.admin.product_attribute.create',
|
||||
'ProductAttribute.jsonld-admin.product_attribute.read',
|
||||
'ProductAttribute.jsonld-admin.product_attribute.create',
|
||||
];
|
||||
|
||||
foreach ($schemasToBeUpdated as $schemaToBeUpdated) {
|
||||
$schemas[$schemaToBeUpdated]['properties']['type'] = [
|
||||
'type' => 'string',
|
||||
'enum' => $attributeTypes,
|
||||
];
|
||||
}
|
||||
|
||||
return $schemas;
|
||||
}
|
||||
|
||||
/** @return array<string> */
|
||||
private function getAttributeTypes(): array
|
||||
{
|
||||
$attributeTypes = [];
|
||||
|
||||
/** @var AttributeTypeInterface $attributeType */
|
||||
foreach ($this->attributeTypeRegistry->all() as $attributeType) {
|
||||
$attributeTypes[] = $attributeType->getType();
|
||||
}
|
||||
|
||||
return $attributeTypes;
|
||||
}
|
||||
}
|
||||
|
|
@ -38,6 +38,11 @@
|
|||
<tag name="sylius.open_api.modifier" />
|
||||
</service>
|
||||
|
||||
<service id="Sylius\Bundle\ApiBundle\OpenApi\Documentation\AttributeTypeDocumentationModifier">
|
||||
<argument type="service" id="sylius.registry.attribute_type" />
|
||||
<tag name="sylius.open_api.modifier" />
|
||||
</service>
|
||||
|
||||
<service id="Sylius\Bundle\ApiBundle\OpenApi\Documentation\ProductDocumentationModifier">
|
||||
<tag name="sylius.open_api.modifier" />
|
||||
</service>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue