c# - 将代码 2.0 迁移到 3.1 核心代码,然后 swagger api 版本控制不起作用

Cannot implicitly convert type
'System.Collections.Generic.Dictionary<string, Microsoft.OpenApi.Models.OpenApiPathItem>' to 'Microsoft.OpenApi.Models.OpenApiPaths'
An explicit conversion exists (are you missing a cast?)

public class ReplaceVersionWithExactValueInPath : IDocumentFilter
{
    public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
    {
        swaggerDoc.Paths = swaggerDoc.Paths
            .ToDictionary(
                path => path.Key.Replace("v{version}", swaggerDoc.Info.Version),
                path => path.Value
            );
    }
}

最佳答案

看起来 OpenApiDocument.Path 不是字典 所以你不能使用 swaggerDoc.Paths = swaggerDoc.Paths.ToDictionary() 赋值

尝试使用新的 OpenApiPath 对象和 Add() 方法

var oap = new OpenApiPaths();
foreach (var p in swaggerDoc.Paths)
    oap.Add(p.Key.Replace("v{version}", swaggerDoc.Info.Version),
        p.Value);
swaggerDoc.Paths = oap;

https://stackoverflow.com/questions/60563647/

相关文章:

flutter - 如何在 flutter, android studio 中不生成红色框,

amazon-web-services - AWS CLI : Could not connect

Django - 检查两个密码哈希是否具有相同的原始密码

r - 按列汇总 : mean and sum

r - 如何在 R 中绘制用 kmeans 获得的簇的 3D 图?

python - 在 hvplot 中自定义标记列表

clojure - 您如何使用 Reitit 在 Clojure 中设置中间件以启用主体参数的强制转

reactjs - 功能组件在 setState 更新 Hook 后不重新渲染

git - 将 git 命令与 Google Cloud Builder 结合使用

pandas - 在 Pandas 中使用带有多索引的聚合