Swagger EF Core 3.1 Delta<Object> 请求正文 - 不显示对象模式 Odata.Delta
Swagger EF Core 3.1 Delta<Object> Request Body - Show Object Schema not Odata.Delta
我最近开始在我的 EF Core 项目中使用 Swagger/Swashbuckle。
当我向文档添加补丁 API 时,请求正文的示例值/架构显示的是 Delta 的详细信息,而不是我的对象。由于它是需要发布的对象属性的子集,有没有办法显示我的对象架构?
这是什么招摇
我想要什么(屏幕截图来自 Post API 电话)
大招和API的签名如下
/// <summary>
/// Updates the provided Absence Reason.
/// </summary>
/// <remarks>
/// Runs bespoke Patch/Validation logic.
/// Updates the Absence Reason then returns the record.
/// </remarks>
/// <param name="key" required="true">Id of the Absence Reason being updated.</param>
/// <param name="delta" required="true">A delta of the updated Absence Reason record.</param>
/// <returns>Returns the created Absence Reason</returns>
/// <response code="204">Returns the updated Absence Reason.</response>
/// <response code="400">Invalid Absence Reason Record, Missing Row Version etc.</response>
/// <response code="404">Absence Reason not found.</response>
/// <response code="412">Record has been updated since the version provided.</response>
[ApiExplorerSettings]
[HttpPatch("odata/[controller]({key})")]
[Produces("application/json")]
[ProducesResponseType(typeof(AbsenceReason), StatusCodes.Status204NoContent)]
[ProducesResponseType(400)]
[ProducesResponseType(404)]
[ProducesResponseType(412)]
[Authorize(Policy = AuthPolicyNames.GOIAdminRole)]
public IActionResult Patch([FromODataUri] long key, Delta<AbsenceReason> delta)
编辑以添加附加信息以响应 AlFranco
Watch on Reference 截图
您代码中的 _assemblyName 是
“Microsoft.AspNetCore.OData,版本=7.4.1.0,文化=中性,PublicKeyToken=31bf3856ad364e35”
VS 文档创建的精简 XML 文件是:
<member name="M:ESRGOI.Controllers.AbsenceReasonsController.Patch(System.Int64,Microsoft.AspNet.OData.Delta{ESRGOI.Models.AbsenceReason})">
<summary>
Updates the provided Absence Reason.
</summary>
<param name="key" required="true">Id of the Absence Reason being updated.</param>
<param name="delta" required="true">A delta of the updated Absence Reason record.</param>
</member>
编辑 2
好的,我已经设法根据 AlFranco 的回复想出了一个解决方案。
需要注意的是,在 Swagger 中我的模型不包含命名空间。我最初尝试包含命名空间但收到以下错误:
/components/schemas/ESRGOI.Models.AbsenceReason 在文档
中不存在
public class DeltaOperationFilter : IOperationFilter
{
private const string _deltaParam = "Delta";
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (operation.RequestBody == null) return;
var deltaTypes =
operation.RequestBody
.Content
.Where(x => x.Value.Schema.Reference.Id.EndsWith(_deltaParam));
foreach (var (_, value) in deltaTypes)
{
var schema = value.Schema;
string model = schema.Reference.Id.Substring(0, schema.Reference.Id.Length - _deltaParam.Length);
schema.Reference.Id = model;
}
}
}
好的,为此您需要创建一个 IOperationFilter
这对我有用:
public class DeltaOperationFilter : IOperationFilter
{
private const string DeltaWrapper = "Microsoft.AspNet.OData.Delta`1";
private readonly string _assemblyName = typeof(Microsoft.AspNet.OData.Delta).Assembly.FullName;
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (operation.RequestBody == null) return;
var deltaTypes =
operation.RequestBody
.Content
.Where(x => x.Value.Schema.Reference.Id.StartsWith(DeltaWrapper));
foreach (var (_, value) in deltaTypes)
{
var schema = value.Schema;
var deltaType = Type.GetType(schema.Reference.Id+ ", " + _assemblyName);
var deltaArgument = deltaType?.GetGenericArguments().First();
schema.Reference.Id = deltaArgument?.FullName ?? schema.Reference.Id;
}
}
}
之后您唯一需要做的就是在 swaggergen 中注册
// Assuming you are in Startup.cs ConfigureServices
services.AddSwaggerGen(c => c.OperationFilter<DeltaOperationFilter>());
顺便问一下,你能在你的实体上正确使用 Delta<T>
apply 吗?我将对某些端点使用补丁,但我想使用 Delta<DTO>
,然后对其进行转换并将其应用到实际上会持续存在的 Delta<Entity>
。我对您将采用的方法特别感兴趣:)
谢谢!
我最近开始在我的 EF Core 项目中使用 Swagger/Swashbuckle。
当我向文档添加补丁 API 时,请求正文的示例值/架构显示的是 Delta 的详细信息,而不是我的对象。由于它是需要发布的对象属性的子集,有没有办法显示我的对象架构?
这是什么招摇
我想要什么(屏幕截图来自 Post API 电话)
大招和API的签名如下
/// <summary>
/// Updates the provided Absence Reason.
/// </summary>
/// <remarks>
/// Runs bespoke Patch/Validation logic.
/// Updates the Absence Reason then returns the record.
/// </remarks>
/// <param name="key" required="true">Id of the Absence Reason being updated.</param>
/// <param name="delta" required="true">A delta of the updated Absence Reason record.</param>
/// <returns>Returns the created Absence Reason</returns>
/// <response code="204">Returns the updated Absence Reason.</response>
/// <response code="400">Invalid Absence Reason Record, Missing Row Version etc.</response>
/// <response code="404">Absence Reason not found.</response>
/// <response code="412">Record has been updated since the version provided.</response>
[ApiExplorerSettings]
[HttpPatch("odata/[controller]({key})")]
[Produces("application/json")]
[ProducesResponseType(typeof(AbsenceReason), StatusCodes.Status204NoContent)]
[ProducesResponseType(400)]
[ProducesResponseType(404)]
[ProducesResponseType(412)]
[Authorize(Policy = AuthPolicyNames.GOIAdminRole)]
public IActionResult Patch([FromODataUri] long key, Delta<AbsenceReason> delta)
编辑以添加附加信息以响应 AlFranco
Watch on Reference 截图
您代码中的 _assemblyName 是 “Microsoft.AspNetCore.OData,版本=7.4.1.0,文化=中性,PublicKeyToken=31bf3856ad364e35”
VS 文档创建的精简 XML 文件是:
<member name="M:ESRGOI.Controllers.AbsenceReasonsController.Patch(System.Int64,Microsoft.AspNet.OData.Delta{ESRGOI.Models.AbsenceReason})">
<summary>
Updates the provided Absence Reason.
</summary>
<param name="key" required="true">Id of the Absence Reason being updated.</param>
<param name="delta" required="true">A delta of the updated Absence Reason record.</param>
</member>
编辑 2 好的,我已经设法根据 AlFranco 的回复想出了一个解决方案。
需要注意的是,在 Swagger 中我的模型不包含命名空间。我最初尝试包含命名空间但收到以下错误: /components/schemas/ESRGOI.Models.AbsenceReason 在文档
中不存在public class DeltaOperationFilter : IOperationFilter
{
private const string _deltaParam = "Delta";
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (operation.RequestBody == null) return;
var deltaTypes =
operation.RequestBody
.Content
.Where(x => x.Value.Schema.Reference.Id.EndsWith(_deltaParam));
foreach (var (_, value) in deltaTypes)
{
var schema = value.Schema;
string model = schema.Reference.Id.Substring(0, schema.Reference.Id.Length - _deltaParam.Length);
schema.Reference.Id = model;
}
}
}
好的,为此您需要创建一个 IOperationFilter
这对我有用:
public class DeltaOperationFilter : IOperationFilter
{
private const string DeltaWrapper = "Microsoft.AspNet.OData.Delta`1";
private readonly string _assemblyName = typeof(Microsoft.AspNet.OData.Delta).Assembly.FullName;
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (operation.RequestBody == null) return;
var deltaTypes =
operation.RequestBody
.Content
.Where(x => x.Value.Schema.Reference.Id.StartsWith(DeltaWrapper));
foreach (var (_, value) in deltaTypes)
{
var schema = value.Schema;
var deltaType = Type.GetType(schema.Reference.Id+ ", " + _assemblyName);
var deltaArgument = deltaType?.GetGenericArguments().First();
schema.Reference.Id = deltaArgument?.FullName ?? schema.Reference.Id;
}
}
}
之后您唯一需要做的就是在 swaggergen 中注册
// Assuming you are in Startup.cs ConfigureServices
services.AddSwaggerGen(c => c.OperationFilter<DeltaOperationFilter>());
顺便问一下,你能在你的实体上正确使用 Delta<T>
apply 吗?我将对某些端点使用补丁,但我想使用 Delta<DTO>
,然后对其进行转换并将其应用到实际上会持续存在的 Delta<Entity>
。我对您将采用的方法特别感兴趣:)
谢谢!