Skip to content
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

Versioning in Swagger Api Platform #61

Open
michnaadam33 opened this issue Oct 2, 2019 · 1 comment
Open

Versioning in Swagger Api Platform #61

michnaadam33 opened this issue Oct 2, 2019 · 1 comment

Comments

@michnaadam33
Copy link

I had to use versioning-bundle on ApliPlatform so I had to create simple normalizer for that.

use ApiPlatform\Core\Documentation\Documentation;
use ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer;
use Shivas\VersioningBundle\Service\VersionManager;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class SwaggerVersionNormalizer implements
    NormalizerInterface,
    NormalizerAwareInterface
{
    use NormalizerAwareTrait;

    public const ALREADY_CALLED = 'SWAGGER_DECORATOR_NORMALIZER_ALREADY_CALLED';

    /** @var VersionManager  */
    private $versionManager;

    public function __construct(VersionManager $versionManager)
    {
        $this->versionManager = $versionManager;
    }

    /**
     * @param Documentation $object
     * @param string $format
     * @param array $context
     * @return array
     * @throws \Psr\Cache\InvalidArgumentException
     * @throws \Symfony\Component\Serializer\Exception\ExceptionInterface
     */
    public function normalize($object, $format = null, array $context = []): array
    {
        $context[self::ALREADY_CALLED] = true;
        $result = $this->normalizer->normalize($object, $format, $context);

        if (isset($result['info'])) {
            $result['info']['version'] = $this->versionManager->getVersion()->getVersionString();
        }
        return $result;
    }

    /**
     * {@inheritdoc}
     */
    public function supportsNormalization($data, $format = null, array $context = [])
    {
        if (isset($context[self::ALREADY_CALLED])) {
            return false;
        }
        return DocumentationNormalizer::FORMAT === $format && $data instanceof Documentation;
    }
}

I think it can be helpful for someone.

@michnaadam33 michnaadam33 changed the title Versioning on Swagger Api Platform Versioning in Swagger Api Platform Oct 7, 2019
@Jayfrown
Copy link

@michnaadam33 thanks I was looking for something like this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants