SwaggerUI 5.0.0 忽略 JsonProperty 名称 [英] SwaggerUI 5.0.0 ignoring JsonProperty name

查看:47
本文介绍了SwaggerUI 5.0.0 忽略 JsonProperty 名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近使用 ASP.NET Core 3.1 将 ASP.NET Core API 应用程序升级到 Swashbuckle/Swagger 5.0.0.

I recently upgraded a ASP.NET Core API application to Swashbuckle/Swagger 5.0.0 with ASP.NET Core 3.1.

<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="5.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.0.0" />

但是,在这样做时,我注意到 Swagger UI 现在忽略了 DTO 属性上的 Newtonsoft/JSON.NET JsonProperty 属性名称.例如:

However, upon doing so I have noticed that the Swagger UI is now ignoring the Newtonsoft/JSON.NET JsonProperty attribute names on DTO properties. For example:

[HttpGet("testget", Name = "testget")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<TestGetResponse> TestGet()
{
    var response = new TestGetResponse { MyName = "John" };

    return Ok(response);
}

public class TestGetResponse
{
    [JsonProperty("name")]
    public string MyName { get; set; }
}

在 Swagger UI 中输出为:

Is outputting in Swagger UI as:

代码 200:示例值

Model
{
  "myName": "string"
}

代替模型属性名称name.

对于我目前拥有的 Startup.cs ConfigureServices 中的 Swagger:

For Swagger in Startup.cs ConfigureServices I currently have:

services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo
    {
        Version = "v1",
        Title = "My API",
    });

    c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"));
});

对于 Startup.cs 中的 Swagger 配置,我目前拥有:

And for Swagger in Startup.cs Configure I currently have:

app.UseSwagger();

app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API");
    c.RoutePrefix = "swagger";
});

我知道,如果我使用 System.Text.Json 属性 JsonPropertyName 而不是这将被修复,但我现在需要坚持使用原始属性.

I am aware that if I use the System.Text.Json attribute JsonPropertyName instead this will be fixed however I need to stick with the original attributes for now.

Start.cs 中是否有设置告诉 SwaggerUI 使用 Newtonsoft JsonProperty 属性,或者我需要在 ASP.NET Core 配置级别更改某些内容?

Is there a setting in Start.cs to tell SwaggerUI to use the Newtonsoft JsonProperty attribute or is there something I need to change at the ASP.NET Core level of configuration?

推荐答案

Swashbuckle/Swagger 5.0.0 版对 NewtonSoft JSON.net 的完全支持似乎是通过单独的包提供的.

It would appear that full support for NewtonSoft JSON.net from version 5.0.0 of Swashbuckle/Swagger is provided through a separate package.

要使其正常工作:

1) 安装 nuget 包 Swashbuckle.AspNetCore.Newtonsoft 版本 5.0.0+

1) Install the nuget package Swashbuckle.AspNetCore.Newtonsoft version 5.0.0+

2) 在 Startup.ConfigureServices() 中呼叫支持:

2) In Startup.ConfigureServices() make a call for the support:

services.AddSwaggerGenNewtonsoftSupport();

可以在此处找到更多信息.

这篇关于SwaggerUI 5.0.0 忽略 JsonProperty 名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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