NelmioApiDocBundle 否定 path_patterns

问题描述

现在我尝试使用 NelmioApiDocBundle 在 Symfony 3 中创建 API 文档。到目前为止,一切都按照给定的 symfony 文档中的描述工作。

现在我想从 swagger 文档中删除 _error 和 _profiler 路由。它说你可以只使用 path_patterns。所以我需要在文档中写下我需要的所有路线。但我有一些完全不同的路径。

有机会创建像

这样的负面路径模式会很酷
...
    path_patterns:
        - !^/_error
        - !^/fubar

这样的事情有可能吗?

解决方法

那些是正则表达式模式,所以,是的,您应该能够匹配正则表达式允许的任何类型的模式。
查看 "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