从 ASP.Net Core OData 项目中的 Swashbuckle 生成的 Swagger 模式中排除某些模型 [英] Exclude certain Models from Swashbuckle-generated Swagger Schema in ASP.Net Core OData Project

查看:84
本文介绍了从 ASP.Net Core OData 项目中的 Swashbuckle 生成的 Swagger 模式中排除某些模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Swashbuckle 为我的 ASP.Net Core 3.1 OData 项目生成 Swagger 文档.

I am using Swashbuckle to generate the Swagger documentation for my ASP.Net Core 3.1 OData project.

控制器方法的生成是完美的.它根据模型正确生成模式,除了一件事:

The generation of the controllers' methods is perfect. And it is correctly generating the schema based on the models, except for one thing:

对于架构中的每个模型,它还生成一个额外的 [ModelName]IQueryableODataValue 条目,如下面的屏幕截图所示:

For each Model in the schema, it is also generating an additional [ModelName]IQueryableODataValue entry, as shown in the below screenshot:

蓝色勾表示型号.红色圈出的模型条目是多余的条目,也正在添加.

The blue check indicates the model. The model entries circled in red are the superfluous entries that are also being added.

注意它也在生成 Void 模型,同样用红色圈起来.

Note it is also generating the Void model, also circled in red.

如何获取 swagger 文档以省略我用红色圈出的模型条目?我怀疑这与添加 SchemaFilter 有关系.但我不知道如何应用它来解决这个问题.

How do I get the swagger documentation to omit the model entries that I have circled in red? I suspect it has something to do with adding a SchemaFilter. But I can't figure out how to apply it to resolve this issue.

@0909EM 的评论让我对 SchemaFilter 进行了更多的挖掘,最后我应用了以下 SchemaFilter:

@0909EM's comment lead me to do some digging into the SchemaFilter a bit more, and I ended up applying the following SchemaFilter:

using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Linq;

namespace MyProject.SwaggerConfig
{
    public class RemoveBogusDefinitions : ISchemaFilter
    {
        public void Apply(OpenApiSchema schema, SchemaFilterContext context)
        {
            foreach (var item in context.SchemaRepository.Schemas.Keys.Where(r => r.EndsWith("ODataValue")))
                context.SchemaRepository.Schemas.Remove(item);

            context.SchemaRepository.Schemas.Remove("Void");
        }
    }
}

只要添加到 context 的最后一个 schema 是我想要保留的有效模型,它就可以工作.问题是上面的代码执行完,然后在context中加入了schema.所以如果最后一个正在处理的 schema 恰好是我想排除的,上面的代码就不会这样做.

It works as long as the LAST schema added to the context is a valid model that I want to keep. The problem is that the above code executes, and then the schema is added to the context. So if the last schema being processed happens to be one that I want to exclude, the above code won't do it.

如何防止将 schema 添加到 context 中?

How do I prevent a schema from being added to the context?

推荐答案

在我的情况下只有这个有效:

In my case only this worked:

internal class SwaggerSchemaFilter : ISchemaFilter
{
    public void Apply(OpenApiSchema schema, SchemaFilterContext context)
    {
        var keys = new System.Collections.Generic.List<string>();
        var prefix = "My.Namespace";
        foreach(var key in context.SchemaRepository.Schemas.Keys)
        {
            if (key.StartsWith(prefix))
            {
                keys.Add(key);
            }
        }
        foreach(var key in keys)
        {
            context.SchemaRepository.Schemas.Remove(key);
        }
    }
}

这篇关于从 ASP.Net Core OData 项目中的 Swashbuckle 生成的 Swagger 模式中排除某些模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆