【问题标题】:Swagger is Adding Placeholder Text to the Url in Requests sent by the Try It Out FeatureSwagger 正在将占位符文本添加到 Try It Out 功能发送的请求中的 Url
【发布时间】:2018-07-10 23:29:13
【问题描述】:

我正在为我的 WebAPI 项目使用 Swagger 和 SwaggerUi(通过 Swashbuckle.AspNetCore Nuget 包)。

我遇到的问题是 UI 没有正确形成 Try It Out 功能的请求。例如,我的 API 在服务器的方法签名中有一个可以为空的long。该参数称为modifiedSince

当我点击执行时,modifiedSince文本输入中没有值,点击以下Urlhttps://localhost:44348/v1/Complication/%7BmodifiedSince%7D

它正在拾取占位符文本并将其包含在 URL 中(在 html 编码的花括号内)。

在另一个端点上,我有两个参数,URL 看起来像这样:https://localhost:44348/v1/Logbook/37193/Entry/2121321

但是当我点击 Try It Out 按钮时,SwaggerUi 会发送https://localhost:44348/v1/Logbook/37193/Entry/%7BentryId%7D?entryId=2121321,结果,我的服务器方法中的 entryId 参数会出现一个空值。

同样,相关文本输入的占位符被添加到 url。

我从 2.5.0 升级到 3.0.0 以解决这个问题。但它仍在发生。

Swagger API 正在执行的控制器操作的示例是:

/// <summary>
/// Logbooks of user.
/// </summary>
/// <param name="modifiedSince"></param>
/// <returns></returns>
/// <response code="200">Logbooks assigned to the user</response>
/// <response code="204">No logbooks assigned to the user</response>
/// <response code="400">Error state. Error message included in payload.</response>        
/// <response code="403">Authentication required.</response>        
[HttpGet]
[Route("[action]/{modifiedSince?}")]
[MapToApiVersion("1.0")]
[ProducesResponseType(typeof(ApiResponse<IEnumerable<LogbookDto>>), 200)]
[ProducesResponseType(204)]
[ProducesResponseType(typeof(ApiResponse<string>), 400)]
[ProducesResponseType(typeof(ApiResponse<string>), 403)]
public async Task<IActionResult> Logbook(long? modifiedSince)
{
    var logbookDtos = default(IEnumerable<LogbookDto>);

    if (await AuthorizeAsync(User, IdentityConstants.PolicyNames.RacsUser) &&
        await Execute(async () => logbookDtos = await _mediator.Send(
            new GetLogbooksQuery
            {
                UserId = _currentUser.UserId
                //LastConfigChange = NULL
            }))
            )
    {
        if (logbookDtos.Any())
            return Ok(new ApiResponse<IEnumerable<LogbookDto>> { Data = logbookDtos, Outcome = new OperationOutcome { Error = false, Message = "" } });

        return NoContent();
    }

    return ErrorResponse(new ApiResponse<string> { Data = Errors, Outcome = new OperationOutcome { Error = true, Message = "" } });
}

这是来自 Chrome 开发工具的图片,用于描述正在发生的事情:

有人知道发生了什么吗?我是否错过了某个地方的配置?

【问题讨论】:

  • 请分享您遇到问题的控制器的代码。
  • 您是否测试过从参数中删除可空值?
  • @HelderSepu 抱歉耽搁了。我在另一个时区。我从 Action 方法的参数中删除了 nullable,并且正确地形成了 url https://localhost:44348/v1/Logbook 。我现在将使用控制器代码更新问题。我希望删除参数只是一种故障排除技术,因为它并不能真正解决问题。

标签: swagger swagger-ui swashbuckle


【解决方案1】:

从 cmets 扩展:

如果当您删除指向 swashbuckle 中的错误的 nullable 时它有效,您应该报告它:
https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/new

我唯一有点模糊的是,从 JSON Schema 的角度来看,nullable 不一定意味着 not required...
您可以拥有必填字段并允许 null 作为值

C# 做的某些事情并不能直接转换为大张旗鼓的东西,我的建议是删除可空值,也许您可​​以使用默认值,例如:
public async Task&lt;IActionResult&gt; Logbook(long modifiedSince = -1)

并处理代码中的默认情况

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-01-18
  • 2014-11-24
  • 2012-01-03
  • 1970-01-01
  • 2013-12-20
  • 2012-09-04
  • 2014-01-08
  • 2018-11-27
相关资源
最近更新 更多