Deserialization to class not working #1728
-
|
I am trying to capture the Digicert Automation API with a Refit When I define the method to return string, so I can see the raw JSON returned, it appears correct But when I change it to |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
A null inner property after a successful (no-error) deserialize almost always means a name mismatch between the JSON and your model, not a Refit bug. In your payload the shape is: { "error": null, "data": { "agentList": [ ... ] } }By default Refit uses System.Text.Json, which is case-sensitive and does not map snake_case automatically. So a property named Data will not bind to "data" unless you configure it. Fixes (pick one):
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
var api = RestService.For<IDigicert>(httpClient, new RefitSettings
{
ContentSerializer = new SystemTextJsonContentSerializer(options)
});
public class AgentListResponse
{
[JsonPropertyName("error")] public string? Error { get; set; }
[JsonPropertyName("data")] public AgentListData? Data { get; set; }
}Also note agentId comes back as 2545.0 and updatePreference as 0.0 (floating point), so model those as double, not int, or deserialization of that node can fail/return defaults. If after matching the names exactly (or enabling case-insensitive) data is still null, post your model classes inline and we can pinpoint it. |
Beta Was this translation helpful? Give feedback.
A null inner property after a successful (no-error) deserialize almost always means a name mismatch between the JSON and your model, not a Refit bug.
In your payload the shape is:
{ "error": null, "data": { "agentList": [ ... ] } }By default Refit uses System.Text.Json, which is case-sensitive and does not map snake_case automatically. So a property named Data will not bind to "data" unless you configure it.
Fixes (pick one):