Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to ignore a model property in nswag without using [JsonIgnore]?

Using JsonIgnore is not possible in my project and [OpenApiIgnore] does not work. In swashbuckle it was possible to make a filter by its own attribute, but in NSwag I did not find a similar mechanism.

Code example:

Command class On API Gateway:

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

Command class on microservice:

 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 method on gateway:

[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();
}

Why i need to remove property from NSwag generation? Because i need Id in route, but if Id also located in query body it's makes my front-end coder aggressive and destructive хD and also it's not beautiful: nswag generation

like image 240
x666ep Avatar asked Sep 03 '25 10:09

x666ep


1 Answers

My solution would be to use separate 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; }
...

  }
}

and in the 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();
    }  
  }
}

The Domain.Commands can be placed inside a class library and used by API and Microservice as well.

like image 72
Sir Rufo Avatar answered Sep 05 '25 00:09

Sir Rufo