SwaggerUI 5.0.0忽略JsonProperty名称 [英] SwaggerUI 5.0.0 ignoring JsonProperty name
问题描述
我最近将带有ASP.NET Core 3.1的ASP.NET Core API应用程序升级到Swashbuckle/Swagger 5.0.0.
< 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属性名称.例如:
[HttpGet("testget",Name ="testget")][ProducesResponseType(StatusCodes.Status200OK)]公共ActionResult< TestGetResponse>TestGet(){var response = new TestGetResponse {MyName ="John"};返回Ok(响应);}公共类TestGetResponse{[JsonProperty("name")]公共字符串MyName {get;放;}}
在Swagger UI中输出为:
代码200:值示例
型号{"myName":字符串"}
代替模型属性名称 name
.
对于Startup.cs ConfigureServices中的Swagger,我目前拥有:
services.AddSwaggerGen(c =>{c.SwaggerDoc("v1",新的OpenApiInfo{版本="v1",标题=我的API",});c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory,$"{Assembly.GetExecutingAssembly().GetName().Name} .xml"));});
对于Startup.cs Config中的Swagger,我目前拥有:
app.UseSwagger();app.UseSwaggerUI(c =>{c.SwaggerEndpoint("/swagger/v1/swagger.json",我的API");c.RoutePrefix ="swagger";});
我知道,如果我使用 System.Text.Json
属性 JsonPropertyName
,则可以解决此问题,但是现在我需要坚持使用原始属性./p>
Start.cs中是否有设置告诉SwaggerUI使用Newtonsoft JsonProperty属性,或者我需要在ASP.NET Core的配置级别上进行某些更改吗?
似乎通过单独的软件包提供了对Swashbuckle/Swagger版本5.0.0的NewtonSoft JSON.net的完全支持.
要使其正常工作:
1)安装nuget软件包 Swashbuckle.AspNetCore.Newtonsoft
版本5.0.0 +
2)在Startup.ConfigureServices()中致电支持:
services.AddSwaggerGenNewtonsoftSupport();
更多信息可以在此处找到.
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" />
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; }
}
Is outputting in Swagger UI as:
Code 200: Example Value
Model
{
"myName": "string"
}
Instead of Model property name name
.
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"));
});
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";
});
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.
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?
It would appear that full support for NewtonSoft JSON.net from version 5.0.0 of Swashbuckle/Swagger is provided through a separate package.
To get this working:
1) Install the nuget package Swashbuckle.AspNetCore.Newtonsoft
version 5.0.0+
2) In Startup.ConfigureServices() make a call for the support:
services.AddSwaggerGenNewtonsoftSupport();
Further info can be found here.
这篇关于SwaggerUI 5.0.0忽略JsonProperty名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!