【问题标题】:Is it possible to ignore a model property in nswag without using [JsonIgnore]?是否可以在不使用 [JsonIgnore] 的情况下忽略 nswag 中的模型属性?
【发布时间】: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


【解决方案1】:

我的解决方案是使用单独的类

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 可以放在类库中,也可以供 API 和微服务使用。

【讨论】:

  • 是的,我的错!我说错了...当然没有重复!非常感谢创建代码示例。我的代码现在与您的代码非常相似。但是通过单独的程序集/类在微服务和 api 之间共享代码呢?我读过它是什么反模式。你怎么看?
猜你喜欢
  • 2017-12-14
  • 1970-01-01
  • 1970-01-01
  • 2018-04-16
  • 2017-09-14
  • 1970-01-01
  • 2014-09-30
  • 1970-01-01
相关资源
最近更新 更多