Swagger正在向“试用”功能发送的请求中的Url添加占位符文本
我正在为我的WebAPI项目使用Swagger和SwaggerUi(通过swashback.AspNetCore Nuget包) 我遇到的问题是UI没有正确地为“试用”功能生成请求。例如,我的API在服务器上的方法签名中有一个可空的Swagger正在向“试用”功能发送的请求中的Url添加占位符文本,swagger,swagger-ui,swashbuckle,Swagger,Swagger Ui,Swashbuckle,我正在为我的WebAPI项目使用Swagger和SwaggerUi(通过swashback.AspNetCore Nuget包) 我遇到的问题是UI没有正确地为“试用”功能生成请求。例如,我的API在服务器上的方法签名中有一个可空的long。该参数称为modifiedSince 当我单击execute时,modifiedSince文本输入中没有一个值,以下Url被点击https://localhost:44348/v1/Complication/%7BmodifiedSince%7D 它拾取占位
long
。该参数称为modifiedSince
当我单击execute时,modifiedSince文本输入中没有一个值,以下Url被点击https://localhost:44348/v1/Complication/%7BmodifiedSince%7D
它拾取占位符文本并将其包含在URL中(在html编码的花括号内)
在另一个端点上,我有两个参数,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 = "" } });
}
//
///用户日志。
///
///
///
///分配给用户的日志
///没有为用户分配日志
///错误状态。有效负载中包含错误消息。
///需要身份验证。
[HttpGet]
[路由(“[action]/{modifiedSince?}”)]
[地图版本(“1.0”)]
[产品响应类型(类型(ApiResponse),200)]
[产品响应类型(204)]
[产品响应类型(类型(ApiResponse),400)]
[产品响应类型(类型(ApiResponse),403)]
公共异步任务日志(长?修改自)
{
var logbookDtos=默认值(IEnumerable);
if(wait AuthorizeAsync(User,IdentityConstants.PolicyNames.RacsUser)&&
等待执行(异步()=>logbookDtos=wait\u mediator.Send(
新的GetLogbooksQuery
{
UserId=\u currentUser.UserId
//LastConfigChange=NULL
}))
)
{
if(logbookDtos.Any())
返回Ok(新的ApiResponse{Data=logbookDtos,output=newoperationoutcome{Error=false,Message=”“});
返回NoContent();
}
返回ErrorResponse(新ApiResponse{Data=Errors,output=new OperationOutcome{Error=true,Message=”“});
}
下面是Chrome dev tools的一张图片,用来描述正在发生的事情:
有人知道发生了什么事吗?我是否错过了某个配置?从注释展开: 如果在删除指向Swashback中错误的nullable时它起作用,则应报告它:
我唯一有点模糊的是,从JSON模式的角度来看,
nullable
不一定意味着不需要
。您可以将字段设为必填字段,并允许null作为值 C#所做的某些事情不会直接转化为swagger中的某些内容,我的建议是删除nullable,或者您可以使用默认值,比如:
公共异步任务日志(long modifiedSince=-1)
并在代码中处理默认情况请共享您遇到问题的控制器的代码。您是否测试过从参数中删除可空值?@HelderSepu抱歉延迟。我在另一个时区。我从Action方法的参数中删除了nullable,url的格式正确
https://localhost:44348/v1/Logbook
。我现在将用控制器代码更新问题。我希望删除参数只是一种故障排除技术,因为它并不能真正解决问题。我认为使用默认值就足够了。可空值的问题可能是招摇过市本身的局限性-