是否可以在不使用 [JsonIgnore] 的情况下忽略 nswag 中的模型 属性?
Is it possible to ignore a model property in nswag without using [JsonIgnore]?
在我的项目中无法使用 JsonIgnore,[OpenApiIgnore] 也不起作用。在swashbuckle中可以通过自己的属性制作过滤器,但是在NSwag中我没有找到类似的机制。
代码示例:
命令 class 在 API 网关:
[MessageNamespace("identity")]
public class UpdateUser:ICommand
{
[JsonConstructor]
public UpdateUser(string surname, string name, string middleName, string department, string position, string adAccount, string email, string userName, string password)
{
Surname = surname;
Name = name;
MiddleName = middleName;
Department = department;
Position = position;
AdAccount = adAccount;
Email = email;
UserName = userName;
}
[JsonIgnore]
public Guid Id { get; }
...
}
微服务上的命令 class:
public class UpdateUser:ICommand
{
[JsonConstructor]
public UpdateUser(Guid id, string surname, string name, string middleName, string department, string position, string adAccount, string email, string userName, string password)
{
Id = id;
Surname = surname;
Name = name;
MiddleName = middleName;
Department = department;
Position = position;
AdAccount = adAccount;
Email = email;
UserName = userName;
}
public Guid Id { get; }
...
}
Api 网关方法:
[HttpPut("{id}")]
[JwtAuth(Roles.Administrator)]
public async Task<ActionResult> Put(Guid id, UpdateUser command)
{
//Send command to RabbitMQ(serialized)
//Id binded before sending but after construct in service ID is missing
await SendAsync(command.Bind(c => c.Id, id));
return Accepted();
}
为什么我需要从 NSwag 生成中删除 属性?
因为我在路由中需要 Id,但如果 Id 也位于查询正文中,它会使我的前端编码器具有侵略性和破坏性 хD,而且它也不漂亮:
我的解决方案是使用单独的 classes
namespace API.Models
{
public class UpdateUser
{
public string Surname { get; set; }
public string Name { get; set; }
...
}
}
namespace Domain.Commands
{
public class UpdateUser:ICommand
{
[JsonConstructor]
public UpdateUser(Guid id, string surname, string name, string middleName, string department, string position, string adAccount, string email, string userName, string password)
{
Id = id;
Surname = surname;
Name = name;
MiddleName = middleName;
Department = department;
Position = position;
AdAccount = adAccount;
Email = email;
UserName = userName;
}
public Guid Id { get; }
...
}
}
并在控制器中
namespace API.Controllers
{
public class UserController
{
[HttpPut("{id}")]
[JwtAuth(Roles.Administrator)]
public async Task<ActionResult> Put(Guid id, API.Models.UpdateUser command)
{
//Send command to RabbitMQ(serialized)
//Id binded before sending but after construct in service ID is missing
var cmd = new Domain.Commands.UpdateUser( id, command.Surname, command.Name, ... );
await SendAsync(cmd);
return Accepted();
}
}
}
Domain.Commands 可以放在 class 库中,也可以被 API 和微服务使用。
在我的项目中无法使用 JsonIgnore,[OpenApiIgnore] 也不起作用。在swashbuckle中可以通过自己的属性制作过滤器,但是在NSwag中我没有找到类似的机制。
代码示例:
命令 class 在 API 网关:
[MessageNamespace("identity")]
public class UpdateUser:ICommand
{
[JsonConstructor]
public UpdateUser(string surname, string name, string middleName, string department, string position, string adAccount, string email, string userName, string password)
{
Surname = surname;
Name = name;
MiddleName = middleName;
Department = department;
Position = position;
AdAccount = adAccount;
Email = email;
UserName = userName;
}
[JsonIgnore]
public Guid Id { get; }
...
}
微服务上的命令 class:
public class UpdateUser:ICommand
{
[JsonConstructor]
public UpdateUser(Guid id, string surname, string name, string middleName, string department, string position, string adAccount, string email, string userName, string password)
{
Id = id;
Surname = surname;
Name = name;
MiddleName = middleName;
Department = department;
Position = position;
AdAccount = adAccount;
Email = email;
UserName = userName;
}
public Guid Id { get; }
...
}
Api 网关方法:
[HttpPut("{id}")]
[JwtAuth(Roles.Administrator)]
public async Task<ActionResult> Put(Guid id, UpdateUser command)
{
//Send command to RabbitMQ(serialized)
//Id binded before sending but after construct in service ID is missing
await SendAsync(command.Bind(c => c.Id, id));
return Accepted();
}
为什么我需要从 NSwag 生成中删除 属性?
因为我在路由中需要 Id,但如果 Id 也位于查询正文中,它会使我的前端编码器具有侵略性和破坏性 хD,而且它也不漂亮:
我的解决方案是使用单独的 classes
namespace API.Models
{
public class UpdateUser
{
public string Surname { get; set; }
public string Name { get; set; }
...
}
}
namespace Domain.Commands
{
public class UpdateUser:ICommand
{
[JsonConstructor]
public UpdateUser(Guid id, string surname, string name, string middleName, string department, string position, string adAccount, string email, string userName, string password)
{
Id = id;
Surname = surname;
Name = name;
MiddleName = middleName;
Department = department;
Position = position;
AdAccount = adAccount;
Email = email;
UserName = userName;
}
public Guid Id { get; }
...
}
}
并在控制器中
namespace API.Controllers
{
public class UserController
{
[HttpPut("{id}")]
[JwtAuth(Roles.Administrator)]
public async Task<ActionResult> Put(Guid id, API.Models.UpdateUser command)
{
//Send command to RabbitMQ(serialized)
//Id binded before sending but after construct in service ID is missing
var cmd = new Domain.Commands.UpdateUser( id, command.Surname, command.Name, ... );
await SendAsync(cmd);
return Accepted();
}
}
}
Domain.Commands 可以放在 class 库中,也可以被 API 和微服务使用。