【问题标题】:Nested objects in Swashbuckle auto-generated exampleSwashbuckle 自动生成示例中的嵌套对象
【发布时间】:2022-01-07 12:45:14
【问题描述】:

我有一个通过 Swashbuckle 和 Swagger 记录的 .NET Core API。为 UI 生成的“示例”似乎没有在请求中正确包含嵌套对象,尽管在执行端点时它们被正确处理和处理。

我有一个CreatePaymentRequest 类,它是从HTTP 请求的主体接收的,其中包含Notional 类型的属性。 Notional 由一个 decimal 值和一个 string 值组成。

生成的示例如下所示:

{
  "tradeId": 0,
  "settlementMeans": "SWIFT",
  "notional1": {},
  "notional1Rate": 0,
  "notional2": {},
  "notional2Rate": 0,
  "paymentReference": "string",
  "description": "string"
}

当我期望它看起来像这样时:

{
  "tradeId": 0,
  "settlementMeans": "SWIFT",
  "notional1": {"Amount": 0, "Currency": "string"},
  "notional1Rate": 0,
  "notional2": {"Amount": 0, "Currency": "string"},
  "notional2Rate": 0,
  "paymentReference": "string",
  "description": "string"
}

生成的架构似乎也已关闭,因为它不包含 Notional 的属性:

虽然它在底部的“架构”部分中正确列出:

以下是构成每个对象的类:

public class Request
{
    public long TradeId { get; set; }
    public SettlementMeans SettlementMeans { get; set; }
    public Notional Notional1 { get; set; }
    public decimal Notional1Rate { get; set; }
    public Notional Notional2 { get; set; }
    public decimal Notional2Rate { get; set; }
    public string PaymentReference { get; set; } = "";
    public string Description { get; set; } = "";
}

public class Notional
{
    [JsonContructor]
    public Notional(decimal amount, string currency)
    {
        Amount = amount;
        Currency = currency;
    }

    public Notional()
    {
        Amount = 0;
        Currency = "XXX";
    }

    public decimal Amount { get; }
    public string Currency { get; }
}

控制器方法:

[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(CreatePaymentApi_Response))]
[ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(BadRequestResponse))]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<CreatePaymentApi_Response>> PostCreate(CreatePaymentApi_Request request)
{
    var command = _mapper.Map<CreatePaymentHandler.Request>(request);
    var response = await _mediator.Send(command);
    var result = _mapper.Map<CreatePaymentHandler.Response>(response);
    return Ok(result);
}

我在ConfigureServices 中使用services.AddSwaggerGen(); 设置SwaggerGen,没有任何特殊选项。

通过 Swashbuckle UI 进行调试时,必须不断输入或复制/粘贴 Notional 值的 JSON 开始变得非常重复。有什么办法可以让示例按照我的预期生成吗?

【问题讨论】:

  • 嗨@Bradley Uffner,你的swagger 和asp.net 核心的版本是什么?我已经在 asp.net 5 中进行了测试,它在嵌套模型中按预期工作。
  • 它是 .Net 5,使用 Swashbuckle.AspNetCore 6.2.4 和 NSwag.ApiDescription.Client 13.11.3
  • 嗨@Bradley Uffner,您确定在.net 5 中使用Swashbuckle.AspNetCore 6.2.4 吗? Swashbuckle.AspNetCore 甚至没有这样的版本。你为什么使用NSwag.ApiDescription.Client ?另外,你能分享一下你的 Startup.cs 吗?
  • 这是一个非常好的问题......我刚刚回去验证版本号,它们与我记忆中的完全不同。我以前一定是找错地方了。它实际上只在.net5.0 上使用&lt;PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" /&gt;。很抱歉造成混乱。
  • 我不得不说这太神奇了。我使用与您的 nuget 包相同的版本。也许您需要创建一个新的 .net 5 项目并重试。不确定您是否进行了任何其他使嵌套模型不起作用的配置。

标签: c# asp.net-core swagger swashbuckle


【解决方案1】:

我最终能够追踪到 Notional 上的只读 AmountCurrency 属性。我期待它使用标有JsonConstructor 的构造函数,但显然,这仅在使用newtonsoft 序列化程序时才支持,而不是System.Text.Json

理论上,我应该可以使用自定义 ISchemaFilter 解决这个问题,但我认为现在还没有实现它是不值得的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 2014-12-04
    • 1970-01-01
    • 2017-09-22
    • 2020-11-18
    相关资源
    最近更新 更多