当枚举处于路径中时,Swashbuckle / Swagger缺少枚举信息

发布于 2025-01-25 22:04:17 字数 1736 浏览 2 评论 0原文

我使用swashbuckle有一个C#.NET 6 API为客户生成Swagger文档。

我有swashbuckle.aspnetcore 6.3.1和 *.filters 7.02安装了。

在startup.cs中,我

...
    swaggerOptions.UseInlineDefinitionsForEnums();
    swaggerOptions.SchemaFilter<EnumDescriptionFilter>(); // Custom filter.
...

.AddJsonOptions(options =>
    {
        options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
        options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
    })

在API中有几个枚举。当枚举在查询字符串中时,这很棒:

但是,当枚举处于路径中时,这是不起作用的:

我尝试摆弄配置,尝试注释软件包,切换到Newtonsoft等。但是没有运气。

如果将参数的类型更改为字符串,那么我会在Swagger中获得一种类型的字符串,但是我无法获得可能的值。

知道我缺少什么吗?

谢谢。


更新。进一步播放后,在查询中只能正确显示列表。如果我将[FromQuery] vehicletype车辆添加到路线上,则它不带有类型的形式出现。

同时,[FROFQUERY]列表&lt; vehicletype&gt;车辆型正确显示。


更新2:看起来这是OpenApi.net的问题 https://github.com/domaindrivendev/swashbuckle.aspnetcore.aspnetcore/issues/1329#:~:~: 20%28 Look%20AT%20Justenum%20in和%20次序列化%20to%20JSON%20BEHAVIOR%20 from%20THAT%20Library

根据线程,这是该错误的工作:

swaggerGenOptions.UseInlineDefinitionsForEnums();

I have a C# .Net 6 API using Swashbuckle to generate Swagger docs for clients.

I have Swashbuckle.AspNetCore 6.3.1 and *.Filters 7.02 installed.

In Startup.cs, I have

...
    swaggerOptions.UseInlineDefinitionsForEnums();
    swaggerOptions.SchemaFilter<EnumDescriptionFilter>(); // Custom filter.
...

.AddJsonOptions(options =>
    {
        options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
        options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
    })

I have several enums in the APIs that are string serialized. This works great when the enum is in the query string:
enter image description here

However, this doesn't work when the enum is in the path:
enter image description here

I've tried fiddling with the configuration, experimenting with the Annotations package, switching to Newtonsoft, etc. but have had no such luck.

If I change the type of the parameter to string then I get a type of string in the swagger, but I don't get the possible values.

Any idea what I'm missing?

Thanks.


Update. Upon further playing around, in the querystring only List show up correctly. If I add [FromQuery] VehicleType vehicleType to a route, then it shows up without a type.

At the same time, [FromQuery] List<VehicleType> vehicleTypes shows up correctly.


Updated 2: Looks like this is an issue with OpenAPI.NET
https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1329#:~:text=Enum%20type%20is%20missing%20%28look%20at%20JustEnum%20in,and%20serialization%20to%20JSON%20behavior%20from%20that%20library.

As per the thread, this is a work around for the bug:

swaggerGenOptions.UseInlineDefinitionsForEnums();

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文