AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-15848

Luis Abreu's questions

Martin Hope
Luis Abreu
Asked: 2025-02-05 00:55:49 +0800 CST

OpenAPI 规范和 URL 规范化

  • 6

快速提问:OpenAPI 规范是否要求在将当前 URL 与路径匹配之前对验证器进行 URL 规范化?再一次,我发现正在使用的验证器似乎有缺陷,因为它会将规则应用于 /abc/path,但不应用于 /abc/path/(请注意额外的 /)。

谢谢。

openapi
  • 1 个回答
  • 18 Views
Martin Hope
Luis Abreu
Asked: 2025-01-21 17:26:54 +0800 CST

嵌套的 IEnumerable<int> 生成“无效”的 OpenAPI json 文档

  • 6

我们正在使用 MS 为 ASP.NET Core API 应用程序提供的全新 OpenAPI 开箱即用支持,但在生成 json 的方式上遇到了一些问题。我们有以下类型:

public class MsgMovimentacaoLocaisTrabalho {

    public IList<InfoGeral>? LocaisRemover { get; set; }

    public InfoGeral? LocaisAssociar { get; set; }
}

public class InfoGeral {

    public Guid GuidDirecao { get; set; }

    public IEnumerable<int> Locais { get; set; } = Enumerable.Empty<int>();
}

MsgMovimentacaoLocaisTrabalho用作控制器方法之一的参数类型:

public async Task<IActionResult> MovimentaLocaisTrabalhoAsync(
  [Description("Mensagem que ....")]MsgMovimentacaoLocaisTrabalho msg, 
  CancellationToken cancellationToken) {
  ...

问题在于类型的输出:

...
"MsgMovimentacaoLocaisTrabalho": {
        "type": "object",
        "properties": {
          "locaisRemover": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/InfoGeral"
            },
            "nullable": true
          },
          "locaisAssociar": {
            "$ref": "#/components/schemas/InfoGeral2"
          }
        }
      },
"InfoGeral": {
        "type": "object",
        "properties": {
          "guidDirecao": {
            "type": "string",
            "format": "uuid"
          },
          "locais": {
            "type": "array",
            "items": {
              "type": "integer",
              "format": "int32"
            }
          }
        }
      },
....
 "InfoGeral2": {
        "type": "object",
        "properties": {
          "guidDirecao": {
            "type": "string",
            "format": "uuid"
          },
          "locais": {
            "$ref": "#/components/schemas/#/properties/locaisRemover/items/properties/locais"
          }
        },
        "nullable": true
      },

首先,查看InfoGeral架构,它看起来不错(locais表示为 的数组int),但我不确定为什么会有类型InfoGeral2。我假设这是因为该类型MsgMovimentacaoLocaisTrabalho有 2 个引用该类型的属性...

第一个问题:有没有办法让这两个属性的架构重用InfoGeral?

问题 2:为什么我最终得到的是错误的值locais而不是数组?IEnumerable 不应该在架构上生成数组吗?

顺便说一句,这是错误:

Semantic error at components.schemas.InfoGeral2.properties.locais.$ref
$ref values must be RFC3986-compliant percent-encoded URIs
Jump to line 11623
asp.net-core
  • 1 个回答
  • 48 Views
Martin Hope
Luis Abreu
Asked: 2024-02-04 06:49:56 +0800 CST

正则表达式:将 url 与多个 or 表达式匹配的最佳选择是什么

  • 5

假设您正在尝试匹配与以下 2 条规则兼容的 url:

  1. 以单词 .../contactos(/) 结尾
  2. 或者在 url 中间有 .../gestao/... 一词

最初,我从以下模式开始(模式 1):

^(?:(?:/.+)+/contactos/?(?!.))|(?:(?:/.*)+/gestao(?:/.+))$

它有效,但在这种情况下所需的负前瞻似乎不合适,所以我用以下模式替换(我们称之为模式 2):

^(?:(?:/.+)+/contactos/?)$|^(?:(?:/.*)+/gestao(?:/.+))$

主要区别在于,我使用开始/结束锚点来分隔每个表达式,而不是像第一个示例中那样使用单个对(我没有在两种情况下捕获组,因此这实际上是两种模式之间的唯一区别)。通过“隔离”每个“选项”,我可以放弃模式 1 中使用的负前瞻。

由于模式 2 不使用前瞻,我预计它应该比模式 1 更快。但是,情况似乎并非如此。我用 .net 8 构建了一个小型基准测试,看起来模式 1 比匹配 url 的模式稍快。这是我编写的代码(并不是真正有经验的 Benchmark 用户,所以也许我遗漏了一些东西):

[MemoryDiagnoser]
public class RegexPerformance {
    
    private const string _patternNoCapture = "^(?:(?:/.+)+/contactos/?(?!.))|(?:(?:/.*)+/gestao(?:/.+))$";
    private const string _patternNoCaptureWithStartEndAnchors = "^(?:(?:/.+)+/contactos/?)$|^(?:(?:/.*)+/gestao(?:/.+))$";

    private Regex _noCapture = default!;

    private Regex _noCaptureWithStart = default!;

    [GlobalSetup]
    public void Setup() {
        _noCapture = new (_patternNoCapture,
                               RegexOptions.Compiled | RegexOptions.IgnoreCase);
        
        _noCaptureWithStart = new (_patternNoCaptureWithStartEndAnchors,
                                        RegexOptions.Compiled | RegexOptions.IgnoreCase);
    }
    
    public IEnumerable<string> Urls => [
        "/test/contactos",
        "/test/contactos/",
        "/test/someplace/gestao/123/yyy",
        "/test/someplace/gestao/123"
    ];
    
    [Benchmark]
    [ArgumentsSource(nameof(Urls))]
    public bool MatchPatternNoCapture(string url) => _noCapture.IsMatch(url);
    
    [Benchmark]
    [ArgumentsSource(nameof(Urls))]
    public bool MatchPatternNoCaptureWithStartEndAnchors(string url) => _noCaptureWithStart.IsMatch(url);


}

结果如下:


| Method                                   | url                  | Mean       | Error     | StdDev    | Median     | Allocated |
|----------------------------------------- |--------------------- |-----------:|----------:|----------:|-----------:|----------:|
| MatchPatternNoCapture                    | /test/contactos      |   244.5 ns |   8.25 ns |  23.53 ns |   238.6 ns |         - |
| MatchPatternNoCaptureWithStartEndAnchors | /test/contactos      |   224.3 ns |   3.14 ns |   2.62 ns |   223.5 ns |         - |
| MatchPatternNoCapture                    | /test/contactos/     |   256.6 ns |   5.67 ns |  15.80 ns |   259.9 ns |         - |
| MatchPatternNoCaptureWithStartEndAnchors | /test/contactos/     |   278.6 ns |  18.98 ns |  53.84 ns |   260.6 ns |         - |
| MatchPatternNoCapture                    | /test(...)o/123 [26] | 1,081.5 ns |  37.01 ns | 104.39 ns | 1,057.7 ns |         - |
| MatchPatternNoCaptureWithStartEndAnchors | /test(...)o/123 [26] | 1,367.4 ns | 167.26 ns | 493.18 ns | 1,121.6 ns |         - |
| MatchPatternNoCapture                    | /test(...)3/yyy [30] | 1,861.4 ns |  73.08 ns | 201.28 ns | 1,789.4 ns |         - |
| MatchPatternNoCaptureWithStartEndAnchors | /test(...)3/yyy [30] | 1,925.6 ns |  58.13 ns | 168.65 ns | 1,919.0 ns |         - |

顺便说一句,我使用不匹配的 url ( /test/contactosbad) 运行了另一个简单测试,在这种情况下,模式 2 的表现似乎比模式 1 稍好:

| Method                                   | url                | Mean     | Error    | StdDev   | Allocated |
|----------------------------------------- |------------------- |---------:|---------:|---------:|----------:|
| MatchPatternNoCapture                    | /test/contactosbad | 668.4 ns | 13.44 ns | 14.38 ns |         - |
| MatchPatternNoCaptureWithStartEndAnchors | /test/contactosbad | 518.2 ns | 10.30 ns | 14.43 ns |         - |

我必须承认我对这些结果有点困惑。我预计模式 2 的表现会比模式 1 好得多,但情况似乎并非如此。关于为什么会发生这种情况有什么想法吗?

谢谢。

c#
  • 1 个回答
  • 43 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve