swashbuckle生成内部C#类的模式

发布于 2025-01-28 17:27:08 字数 847 浏览 4 评论 0原文

使用swashbuckle.aspnetcore(版本6.3.1)与system.text.json时,该架构会从c#中生成一堆内部设备,例如incoper> assembly 的架构> constructorInfo等。

我认为,当我创建一个返回接口(可能)的端点时,就会发生这种情况。不完全确定。如果我利用swashbuckle.aspnetcore.newtonsoft软件包来生成宣传文档,那不是问题 - 这些模式未生成。但是,我希望不依赖纽顿夫妇,这是获得有效模式的唯一原因。

这不是一个大问题,但是我们的OpenAPI/Swagger文档有无关紧要的信息有点烦人。我很想解决这个问题。

这是一个已知的错误吗?有办法解决吗?任何帮助都将不胜感激。

我试图搜索问题的不同组合,但没有成功。我的查询技巧我很糟糕,所以如果已经问这个问题,那我真的很抱歉。

编辑: 该错误似乎是由于我们有一个具有自定义序列化器在API中公开的值类型。该对象具有type属性,但是,由于序列化器,这实际上从未在API中曝光 - (我们也向Swashbuckle注册,因此实际上并未在Swashbuckle文档中曝光,但是,我认为Swashbuckle在我们注册“正确”序列化器之前已经将对象添加到模式中。)

When using Swashbuckle.AspNetCore (version 6.3.1) with System.text.json, the schemas generate a bunch of internals from c#, like a schema for Assembly, ConstructorInfo, and others.

I think this happens when I've created an endpoint that returns an interface (possibly). Not entirely sure. If I utilize the Swashbuckle.AspNetCore.Newtonsoft package for generating the swagger documentation it isn't a problem - these schemas are not generated. I'd however like to not have a dependency on newtonsoft for the sole reason of getting the valid schemas.

It's not a huge issue, however it is a bit annoying that our openapi/swagger docs have irrelevant information. I'd love to get around that.

Is this a known bug? Is there a way around it? Any help would be much appreciated.

I've tried to search for different combinations of the problem, but with no success. My querying skills my be bad, so if this question has already been asked, then I am truly sorry.

image of schemas that contain both Assembly, CallingConventions and ContructorInfo

EDIT:
The bug seems to be due to the fact that we have a value type that we expose in the API with a custom serializer. that object has a Type property, however, this is never actually exposed in the API due to the serializer - (which we also registered with Swashbuckle, so it isn't actually exposed in the Swashbuckle documentation, however, I assume Swashbuckle already added the objects to the schema prior to us registering the "correct" serializer.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日裸衫吸 2025-02-04 17:27:08

删除它的一种方法是通过iDocumentFilter这样:


    public class RemoveSchemasFilter : IDocumentFilter
    {
        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            var types = (Assembly.GetAssembly(this.GetType())).GetTypes().Where(x => x.IsClass).Select(x => x.Name);

            IDictionary<string, OpenApiSchema> _remove = swaggerDoc.Components.Schemas.Where(x => !types.Contains(x.Key)).ToDictionary(x => x.Key, x => x.Value);
            foreach (KeyValuePair<string, OpenApiSchema> _item in _remove)
            {
                swaggerDoc.Components.Schemas.Remove(_item.Key);
            }
        }
    }

在您的di中,您注册Swagger只需添加此信息

builder.Services.AddSwaggerGen(c => c.DocumentFilter<RemoveSchemasFilter>())

One way to remove it is by an IDocumentFilter like this:


    public class RemoveSchemasFilter : IDocumentFilter
    {
        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            var types = (Assembly.GetAssembly(this.GetType())).GetTypes().Where(x => x.IsClass).Select(x => x.Name);

            IDictionary<string, OpenApiSchema> _remove = swaggerDoc.Components.Schemas.Where(x => !types.Contains(x.Key)).ToDictionary(x => x.Key, x => x.Value);
            foreach (KeyValuePair<string, OpenApiSchema> _item in _remove)
            {
                swaggerDoc.Components.Schemas.Remove(_item.Key);
            }
        }
    }

in you DI where you register swagger just add this

builder.Services.AddSwaggerGen(c => c.DocumentFilter<RemoveSchemasFilter>())
七禾 2025-02-04 17:27:08

我偶然发现了这个问题,因为我遇到了同样的问题。就我而言,该问题是通过指定filenotfoundexceptionwessioneType

[ProducesResponseType(typeof(FileNotFoundException), StatusCodes.Status404NotFound)]

在我创建自己的filenotfoundresponse class之后,

public class DocumentNotFoundResponse
{
    public required string Message { get; init; }
    public required string FileName { get; init; }
}

并将其指定为<代码>响应类型,内部.NET类不再显示为架构:

[ProducesResponseType(typeof(DocumentNotFoundResponse), StatusCodes.Status404NotFound)]

这种行为可能在其他内部类型中也发生。但是,我尚未对此进行测试。

因此,我不知道它是否解决了OP的问题,但也许会对某人有所帮助。

I stumbled across this question because I had the same problem. In my case, the issue was caused by specifying a FileNotFoundException as the ResponseType:

[ProducesResponseType(typeof(FileNotFoundException), StatusCodes.Status404NotFound)]

After I created my own FileNotFoundResponse class:

public class DocumentNotFoundResponse
{
    public required string Message { get; init; }
    public required string FileName { get; init; }
}

and specified it as ResponseType, the internal .NET classes were no longer displayed as schema:

[ProducesResponseType(typeof(DocumentNotFoundResponse), StatusCodes.Status404NotFound)]

This behavior probably occurs with other internal types as well. However, I have not tested this.

So I don't know if it solves the OP's problem, but maybe it will help someone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文