API Explorer - Returning default and null values in json

In the Api explorer I have an endpoint that returns a list of this class:

public class Account
{
   public int Id { get; set; }

   public string Username { get; set; }

   public string Email { get; set; }

   public bool Deactivated { get; set; }

   public DateTime? DeactivationProcessed { get; set; }

   public string Note { get; set; }

   public DateTime Created { get; set; }
}

public class QueryAccount : IReturn<QueryAccountResponse>
{
   ///...
}

public class QueryAccountResponse
{
   public List<Account>? Accounts { get; set; }

   public ResponseStatus? ResponseStatus { get; set; }
}

Unless one of the entries has Deactivated = true the properties won’t show in the ApiExplorer grid.

DeactivationProcessed won’t show unless there’s a value.

What’s interesting is that if one entry has Deactivated = true the column will show at the end of the grid and won’t respect the order of the properties.

I’ve gone over the doc here: JSON Format

I tried adding this in my apphost:

JsConfig.Init(new Config { IncludeNullValues = true, ExcludeDefaultValues = false});

I also tried something similar before I return the response dto:

using (JsConfig.With(new Config { ExcludeDefaultValues = false, IncludeNullValues = true}))
{
  //return the QueryAccountReponse
}

But no luck.

Ideally, I would be able to do this only for certain endpoints and speficify that this reponse dto should include Deactivated and DeactivationProcessed in the json response even if the value are null or the default.

Thanks for the help.

The API Explorer calls the APIs with jsconfig query string which specifies the config for that request since API Explorer needs a consistent formatting of the response to function. Your custom config will be taking effect when you are using your API outside of API Explorer, you can see this by appending .json on the route or when your client passes the specified Accept header with application/json.

1 Like