Expose string as enum with NSwag

44 Views Asked by At

I have this controller

public class Person
{
   public int Id { get; set; }
   public string Position { get; set; }
}

[ApiController]
[Route("[controller]")]
public class MyCtrl : ControllerBase
{
   [HttpGet("{id}")]
   public ActionResult<Person> Get(int id)
   {
      // load person with ID == id from db
      return new ActionResult<Person>(person);
   }
}

Now, the Position is just a string, but based on configuration in the database, it can only be a set of finite values. I would really like if I could mimic that Position was an enum in the Swagger interface (I use NSwag).

I would like if the API looked like it had been generated from this (where PositionType is generated from the configuration in the DB):

public enum PositionType
{
   Student,
   Teacher
}

public class Person
{
   public int Id { get; set; }
   public PositionType Position { get; set; }
}
0

There are 0 best solutions below