【发布时间】:2019-10-02 17:12:10
【问题描述】:
在我的项目中无法使用 JsonIgnore,并且 [OpenApiIgnore] 不起作用。在 swashbuckle 中可以通过自己的属性制作过滤器,但在 NSwag 中我没有找到类似的机制。
代码示例:
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; }
...
}
微服务上的命令类:
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,而且它也不美观:
【问题讨论】:
-
您能详细说明为什么无法使用 [JsonIgnore] 吗?
-
@MindSwipe 来自 api 的命令(api 方法中的参数)序列化并发送到 RabbitMQ。并且 jsonignore 从 serealized rabbitmq 命令中删除我的属性
-
很遗憾,我对 NSwag 一无所知,但您很有可能自己编写 Json.Net Contract Resolver
-
您的 API 有一个模型类,其中包含 API 不需要的属性,对吗?听起来像是违反SRP
-
@SirRufo 没有。我将 cqrs 命令用于微服务。我添加了有问题的代码示例。
标签: c# asp.net-core-3.0 nswag