NelmioApiDocBundle 负 path_patterns
Posted
技术标签:
【中文标题】NelmioApiDocBundle 负 path_patterns【英文标题】:NelmioApiDocBundle negative path_patterns 【发布时间】:2021-05-28 15:21:25 【问题描述】:现在我尝试使用 NelmioApiDocBundle 在 Symfony 3 中创建 API 文档。到目前为止,一切都按照给定的 symfony 文档中的描述工作。
现在我想从 swagger 文档中删除 _error 和 _profiler 路由。它说你可以只使用 path_patterns。所以我需要在文档中写下我需要的所有路线。但我有一些完全不同的路径。
有机会创造像这样的负面路径模式会很酷
...
path_patterns:
- !^/_error
- !^/fubar
这样的事情可能吗?
【问题讨论】:
【参考方案1】:这些是正则表达式模式,是的,您应该能够匹配正则表达式允许的任何类型的模式。 查看 "lookaround" zero-length assertions,特别是 Negative lookahead,然后尝试如下操作:
path_patterns:
- ^\/((?!_error)(?!fubar).)*$
Regex101 是测试和理解正则表达式的绝佳工具。它将解释正则表达式每个部分的影响,如下所示:
^ asserts position at start of a line
\/ matches the character / literally (case sensitive)
1st Capturing Group ((?!_error)(?!fubar).)*
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
Negative Lookahead (?!_error)
Assert that the Regex below does not match
_error matches the characters _error literally (case sensitive)
Negative Lookahead (?!fubar)
Assert that the Regex below does not match
fubar matches the characters fubar literally (case sensitive)
. matches any character (except for line terminators)
$ asserts position at the end of a line
【讨论】:
以上是关于NelmioApiDocBundle 负 path_patterns的主要内容,如果未能解决你的问题,请参考以下文章
NelmioApiDocBundle 对 swagger 2.0 的支持
Symfony 4.4 / NelmioApiDocBundle 4.0.1 注释问题
如何从 NelmioApiDocBundle 生成的文档中删除 _format URL 后缀?
NelmioApiDocBundle 不起作用“规范中没有定义操作!”
Symfony NelmioApiDocBundle swagger-ui PHP Annotations json对象,对象属性数组未显示