繁体   English   中英

是否可以在不使用 [JsonIgnore] 的情况下忽略 nswag 中的 model 属性?

[英]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,而且它也不美观: 新生代

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

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; }
...

  }
}

在 controller

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 和微服务使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM