HttpGet Request returns 'Undocumented Error:OK' on Swagger for no reason

Viewed 731

The problematic api is implemented on my controller.cs as such:

    [HttpGet("GetAlarmList")]
    public IActionResult GetAlarmList()
    {
        try
        {
            ICollection<Alarm> alarms = repo.GetAlarmList();
            return Ok(alarms);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            throw;
        }
    }

You can assume that alarms have the correct value right before returning. The 'Alarm' class is defined as below:

public class Alarm
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int AlarmId { get; set; }
    public string FaultType { get; set; }
    public int DateOccurred { get; set; }
    public Sensor Sensor { get; set; }
    public string EquipmentType { get; set; }
    public string Details { get; set; }
    [ForeignKey("SettingId")]
    public virtual Setting Setting { get; set; }
}

However, swagger returns an error for no specific reason as the screenshot attached. enter image description here

Can anyone spot a problem here? Thank you in advance!

EDIT

Here is the repo constructor.

 public class DataManagerRepository
    {
        private readonly PDAContext _context;

        public SensorsRepository SensorRepo;
        //IDataManager datmanager;
        public DataManagerRepository(PDAContext context)
        {
            _context = context;
            SensorRepo = new SensorsRepository(context);

            String filepath = "C:\\projects\\Corona\\Sec_Files\\";

            DirectoryInfo dir = new DirectoryInfo(filepath);
            FileInfo[] files = dir.GetFiles("*.dat");

            int numfiles = files.Length;
            if (_context.Settings.Count() == 0)
            {
                _context.Settings.Add(new Setting { });
                _context.SaveChanges();
            }
            var setting = _context.Settings.FirstOrDefault();

            ICollection<PD_Datafile> PD_Datafiles = new Collection<PD_Datafile>();

            var rd = new Random();

            ICollection<PD_Datafile> pdfiles = new Collection<PD_Datafile>();
            for (int i = 0; i < numfiles; i++)
            {
                DAT_FILE PD_Datafile = ParseFileName(files[i].Name, filepath);
                string databodystr = Encoding.Default.GetString(PD_Datafile.SensorData.DataBody);
                PD_Datafiles.Add(new PD_Datafile() {FullName = files[i].Name, DataBody = databodystr });
                
            }
            setting.PD_Datafiles = PD_Datafiles;

            _context.SaveChanges();
            
            
            if (_context.Alarms.ToList().Count == 0)
            {
                ICollection<Alarm> alarms = new Collection<Alarm>();
                _context.Alarms.Add(new Alarm() { DateOccurred = 202101201200, EquipmentType = "GISPD", FaultType = "PD Happening", Sensor = null, Details = "None" });
                _context.Alarms.Add(new Alarm() { DateOccurred = 202101211210, EquipmentType = "CBCM", FaultType = "Warning", Sensor = null, Details = "None" });
                _context.Alarms.Add(new Alarm() { DateOccurred = 202101221340, EquipmentType = "DGA", FaultType = "Danger", Sensor = null, Details = "None" });
                _context.Alarms.Add(new Alarm() { DateOccurred = 202101230635, EquipmentType = "OLTC", FaultType = "Danger", Sensor = null, Details = "None" });
            }

            _context.SaveChanges();
            
            Console.WriteLine("Data Manager Initialized!");
        }
1 Answers

I faced a similar problem and here is my solution.

The problem was not in the controller code, but in the middleware, where the next() delegate was called several times. Check if you have a similar problem with your middleware. Here is an example:

Incorrect code:

public async Task InvokeAsync(HttpContext context)
{
    if (true)
    {
        // some code...
        await _next(context);
        // some code...
    }

    await _next(context);
}

Possible solution:

public async Task InvokeAsync(HttpContext context)
{
    if (true)
    {
        // some code...
        await _next(context);
        // some code...
    }
    else
    {
        await _next(context);
    }
}
Related