Swagger 正在向试用功能发送的请求中的 Url 添加占位符文本
Swagger is Adding Placeholder Text to the Url in Requests sent by the Try It Out Feature
我正在为我的 WebAPI 项目使用 Swagger 和 SwaggerUi(通过 Swashbuckle.AspNetCore Nuget 包)。
我遇到的问题是 UI 没有正确形成试用功能的请求。例如,我的 API 在服务器的方法签名中有一个可为 null 的 long
。该参数称为 modifiedSince
.
当我点击执行时,modifiedSince文本输入中没有值,下面的Url被命中https://localhost:44348/v1/Complication/%7BmodifiedSince%7D
它正在拾取占位符文本并将其包含在 URL 中(在 html 编码的 curly 大括号内)。
在另一个端点上,我有两个参数,URL 看起来像这样:https://localhost:44348/v1/Logbook/37193/Entry/2121321
但是,当我单击“试用”按钮时,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 开发工具的图片,用于描述正在发生的事情:
有人知道这是怎么回事吗?我是不是漏掉了什么地方的配置?
根据评论展开:
如果在您删除指向 swashbuckle 中的错误的可空值时它有效,您应该报告它:
https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/new
我唯一有点模糊的是,从 JSON 架构的角度来看,nullable
并不一定意味着 not required
...
您可以要求字段并允许 null 作为值
C# 做的一些事情并不能直接转化为 swagger 的东西,我的建议是删除可为空的,也许您可以使用默认值,例如:
public async Task<IActionResult> Logbook(long modifiedSince = -1)
并在代码中处理默认情况
我正在为我的 WebAPI 项目使用 Swagger 和 SwaggerUi(通过 Swashbuckle.AspNetCore Nuget 包)。
我遇到的问题是 UI 没有正确形成试用功能的请求。例如,我的 API 在服务器的方法签名中有一个可为 null 的 long
。该参数称为 modifiedSince
.
当我点击执行时,modifiedSince文本输入中没有值,下面的Url被命中https://localhost:44348/v1/Complication/%7BmodifiedSince%7D
它正在拾取占位符文本并将其包含在 URL 中(在 html 编码的 curly 大括号内)。
在另一个端点上,我有两个参数,URL 看起来像这样:https://localhost:44348/v1/Logbook/37193/Entry/2121321
但是,当我单击“试用”按钮时,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 开发工具的图片,用于描述正在发生的事情:
有人知道这是怎么回事吗?我是不是漏掉了什么地方的配置?
根据评论展开:
如果在您删除指向 swashbuckle 中的错误的可空值时它有效,您应该报告它:
https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/new
我唯一有点模糊的是,从 JSON 架构的角度来看,nullable
并不一定意味着 not required
...
您可以要求字段并允许 null 作为值
C# 做的一些事情并不能直接转化为 swagger 的东西,我的建议是删除可为空的,也许您可以使用默认值,例如:
public async Task<IActionResult> Logbook(long modifiedSince = -1)
并在代码中处理默认情况