我有一个使用 NSwag 作为其 Swagger 工具的 asp.net 核心项目。我遇到的问题如下:
- 我有一个描述Collection实体的 Dto 类:
#
namespace SchedulerApi.Entities
{
public class JobCollectionDtoV2
{
[Required]
[FromRoute]
[RegularExpression("^[a-z][a-z0-9]+$")]
[StringLength(maximumLength: 24, MinimumLength = 3)]
public string Collection { get; set; }
[Required]
[FromRoute]
[RegularExpression("^[a-z][a-z0-9]+$")]
[StringLength(maximumLength: 24, MinimumLength = 3)]
public string Tenant { get; set; }
}
}
- 以下 API 用于添加Collections:
#
[HttpPut]
[Route("tenants/{tenant}/collections/{collection}")]
[SwaggerResponse(typeof(JobCollectionDtoV2))]
public async Task<IActionResult> CreateTask(JobCollectionDtoV2 collectionParams)
{
}
- 这是 NSwag 生成的 Swagger 文件:
#
{
"put": {
"tags": [
"JobCollectionsControllerV2"
],
"operationId": "JobCollectionsControllerV2_CreateTask",
"consumes": [
"application/json-patch+json",
"application/json",
"text/json",
"application/*+json"
],
"parameters": [
{
"type": "string",
"name": "Collection",
"in": "path",
"required": true,
"maxLength": 24,
"minLength": 3,
"pattern": "^[a-z][a-z0-9]+$",
"x-nullable": true
},
{
"type": "string",
"name": "Tenant",
"in": "path",
"required": true,
"maxLength": 24,
"minLength": 3,
"pattern": "^[a-z][a-z0-9]+$",
"x-nullable": true
},
{
"name": "CollectionDetails",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/JobCollectionDetails"
},
"x-nullable": false
}
],
"responses": {
"200": {
"x-nullable": true,
"description": "",
"schema": {
"$ref": "#/definitions/JobCollectionDtoV2"
}
}
}
}
}
具体来说,我遇到的问题是由于路径声明了两个变量tenant
并collection
使用小写字母,而 DTO 类使用大写字母Tenant
和Collection
.
我希望找到一种方法来装饰 DTO 类属性,该属性将显式声明参数的名称,例如[JsonProperty("collection")]
(这个特定的不会影响输出 Swagger)。
感谢你的帮助