我将 OpenAPI OA 注释与 Swagger 和 Symfony Validator 注释一起使用,但两者之间似乎存在冲突。
示例片段:
use Symfony\Component\Validator\Constraints as Assert;
use OpenApi\Annotations as OA;
/**
* @OA\Schema()
*/
class CustomerPostBody
{
/**
* @Assert\NotBlank
*/
public string $id;
}
使用 PHP 运行时出错:
PHP 致命错误:未捕获 Doctrine\Common\Annotations\AnnotationException:[语义错误] 属性 app\Customer::$id 中的注释“@Symfony\Component\Validator\Constraints\NotBlank”从未导入。您是否忘记为此注释添加“use”语句?
如何配置 Symfony Validator 以完全忽略所有
@OA
注释?
我使用的是 PHP 7.4(现在无法更改),所以我使用 5.x 验证程序包。
我没有使用任何 yaml 文件进行配置。
添加忽略语句仅在我执行
@OA
时才起作用,而不是在 @OA\Schema
时起作用。
AnnotationReader::addGlobalIgnoredName('OpenApi\Annotations');
完整的堆栈跟踪:
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
AnnotationRegistry::registerUniqueLoader('class_exists');
正如 stof 在 Symfony slack 频道上的建议。