galaxied
galaxied

Reputation: 3

Refit API response of a collection of an object is NULL even though ASP.NET Core backend checks for null and gives a 200

I am retrieving a list of objects from ASP.NET Core backend to my .NET MAUI frontend with the Refit API.

Here is my controller:

[Route("api/Database/[controller]")]
[ApiController]
public class StatementsController : ControllerBase
{
    // ...

    [HttpGet("GetStatementFromUserGuid")]
    public async Task<IActionResult> GetStatementFromUserGuid([FromQuery] Guid guid, [FromQuery] StatementViewOrder statementViewOrder, [FromQuery] int ReturnLoad = 5)
    {
        var user = await _controllerAssist.GuidAssist(guid).GetUserFromQueriesAsync(u => u.Statements);
  
        if (user == null)
        { 
            return NotFound(); 
        }

        if (user.Statements.Count == 0)
        {
            return NoContent();
        }

        List<StatementQuickView> views = user.Statements.GetStatementQuickViewFromRawStatements(statementViewOrder, ReturnLoad);
    
        if (views.Count == 0 || views == null || views.Capacity == 0)
        {
            return StatusCode(500, "Error retrieving data");
        }

        Console.WriteLine("\n\n\n" + views.Count.ToString());
        Console.WriteLine(views.ToString());

        return Ok(views);
    }
}

I checked multiple times for a null list or empty list which would return a different http status code.

This is the interface in the frontend with Refit:

[Get("/api/Database/Statements/GetStatementFromUserGuid")]
Task<ApiResponse<List<StatementQuickView>>> GetStatementFromUserGuid([Query] Guid guid, [Query] StatementViewOrder statementViewOrder, [Query] int ReturnLoad = 5);

This is my API service:

public async Task<List<StatementQuickView>?> SeeStatementsFromUserAsync(Guid guid, StatementViewOrder statementViewOrder, int ReturnLoad = 5)
{
     var Data = await statementApi.GetStatementFromUserGuid(guid, statementViewOrder, ReturnLoad);
         
     if (Data.IsSuccessStatusCode)
     {
         ConfirmationHandler.CreateConfirmation("true");
     }

     if (Data.Content == null)
     {
         ErrorHandler.CreateErrorPopUp("NULL");
         return null;
     }

     switch (Data.StatusCode)
     {
         case System.Net.HttpStatusCode.OK:
             ConfirmationHandler.CreateConfirmation(Data.Content.Count.ToString());
             return Data.Content;                

         case System.Net.HttpStatusCode.InternalServerError:
             ErrorHandler.CreateErrorPopUp("Server Error");
             return null;

         default:
             ErrorHandler.CreateErrorPopUp(Data.StatusCode.ToString());
             return null;
     }
}

Currently, the status code is 200 and Data.Content is NULL.

Using Swagger on the backend, I never got a null result.

This is the size of the payload that would be transferred :

{
  "$id": "1",
  "$values": [
    {
      "$id": "2",
      "statementID": "57d8f6c9-6f05-4aa3-a169-5585b8940ea1",
      "authorID": "7af20928-c6de-4f1f-af78-a452ef5fac11",
      "author": "foo",
      "profilePicture": null,
      "createdAt": "2024-10-06T21:51:57.425993",
      "upvotes": 0,
      "downvotes": 0,
      "commentAmount": 0,
      "body": "blah blah blah",
      "statementType": "General",
      "labels": {
        "$id": "3",
        "$values": [
          "string"
        ]
      }
    },
    {
      "$id": "4",
      "statementID": "aaf65ffa-f89d-4560-97ec-6cd3bd7e37ed",
      "authorID": "7af20928-c6de-4f1f-af78-a452ef5fac11",
      "author": "foo",
      "profilePicture": null,
      "createdAt": "2024-10-06T01:59:01.1006187",
      "upvotes": 0,
      "downvotes": 0,
      "commentAmount": 0,
      "body": "please?",
      "statementType": "General",
      "labels": null
    }
  ]
}

The payload currently can go up to 5 "statements", so if you think that would cause an issue please tell me.

Thanks.

Upvotes: 0

Views: 112

Answers (0)

Related Questions