【问题标题】:NelmioApiDocBundle negative path_patternsNelmioApiDocBundle 负 path_patterns
【发布时间】:2021-05-28 15:21:25
【问题描述】:

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

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

有机会创造像这样的负面路径模式会很酷

...
    path_patterns:
        - !^/_error
        - !^/fubar

这样的事情可能吗?

【问题讨论】:

    标签: regex symfony nelmioapidocbundle


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2020-06-03
      • 2020-04-25
      • 2018-11-11
      • 2017-05-19
      • 2021-12-04
      • 2021-09-30
      • 2013-01-16
      • 2021-09-02
      • 2021-04-05
      相关资源
      最近更新 更多