symfonyvalidationannotations

How do I configure Symfony Validator to ignore @OA annotations?


I'm using both OpenAPI OA annotations with Swagger and Symfony Validator annotations, but there seems to be a conflict between the two.

Example snippet:

use Symfony\Component\Validator\Constraints as Assert;
use OpenApi\Annotations as OA;

/**
 * @OA\Schema()
 */
class CustomerPostBody
{
    /**
     * @Assert\NotBlank
     */
    public string $id;
}

Error when running with PHP:

PHP Fatal error: Uncaught Doctrine\Common\Annotations\AnnotationException: [Semantical Error] The annotation "@Symfony\Component\Validator\Constraints\NotBlank" in property app\Customer::$id was never imported. Did you maybe forget to add a "use" statement for this annotation?

How do I configure Symfony Validator to fully ignore all @OA annotations?

I'm on PHP 7.4 (can't be changed right now), so I'm using the 5.x validator package.

I'm not using any yaml files for configuration.

Adding ignore statements works only when I do @OA, not for @OA\Schema.

AnnotationReader::addGlobalIgnoredName('OpenApi\Annotations');

Full stacktrace:

PHP Fatal error:  Uncaught Doctrine\Common\Annotations\AnnotationException: [Semantical Error] The annotation "@Symfony\Component\Validator\Constraints\NotBlank" in property app\Customer::$customer_id was never imported. Did you maybe forget to add a "use" statement for this annotation? in app/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php:40
Stack trace:
#0 app/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php(804): Doctrine\Common\Annotations\AnnotationException::semanticalError()
#1 app/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php(724): Doctrine\Common\Annotations\DocParser->Annotation()
#2 app/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php(377): Doctrine\Common\Annotations\DocParser->Annotations()
#3 app/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationReader.php(179): Doctrine\Common\Annotations\DocParser->parse()
#4 app/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/PsrCachedReader.php(155): Doctrine\Common\Annotations\AnnotationReader->getPropertyAnnotations()
#5 app/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/PsrCachedReader.php(88): Doctrine\Common\Annotations\PsrCachedReader->fetchFromCache()
#6 app/vendor/symfony/validator/Mapping/Loader/AnnotationLoader.php(127): Doctrine\Common\Annotations\PsrCachedReader->getPropertyAnnotations()
#7 app/vendor/symfony/validator/Mapping/Loader/AnnotationLoader.php(60): Symfony\Component\Validator\Mapping\Loader\
AnnotationLoader->getAnnotations()
#8 app/vendor/symfony/validator/Mapping/Factory/LazyLoadingMetadataFactory.php(101): Symfony\Component\Validator\Map
ping\Loader\AnnotationLoader->loadClassMetadata()
#9 app/vendor/symfony/validator/Validator/RecursiveContextualValidator.php(306): Symfony\Component\Validator\Mapping
\Factory\LazyLoadingMetadataFactory->getMetadataFor()
#10 app/vendor/symfony/validator/Validator/RecursiveContextualValidator.php(133): Symfony\Component\Validator\Validator\RecursiveContextualValidator->validateObject()
#11 app/vendor/symfony/validator/Validator/RecursiveValidator.php(93): Symfony\Component\Validator\Validator\RecursiveContextualValidator->validate()
#12 app/testvalidator.php(36): Symfony\Component\Validator\Validator\RecursiveValidator->validate()
#13 {main}
  thrown in app/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php on line 40

Solution

  • AnnotationRegistry::registerUniqueLoader('class_exists');
    

    As suggested by stof on the Symfony slack channel.

    https://github.com/zircote/swagger-php/blob/e8c3bb316e385e93a0c7e8b2aa0681079244c381/src/Analyser.php#L13