octokit.net
octokit.net copied to clipboard
Support deserializing C# record types
trafficstars
What you were trying to do.
I was trying to call IGitHubClient.Connection.Get<T>() where T is a C# record type
What you expected to happen.
That method to return an object
What actually happened.
A System.NullReferenceException was thrown
Steps to reproduce the problem.
I defined the record types:
private sealed record RunnerListResponse(List<RunnerInfo> runners);
private sealed record RunnerInfo(int id, string name, string status, string os, bool busy, List<RunnerLabel> labels);
private sealed record RunnerLabel(int id, string name, string type);
then I made the call:
IApiResponse<RunnerListResponse> runnerListResponse = await m_github.Connection.Get<RunnerListResponse>(
new Uri( "/enterprises/xxx/actions/runners", UriKind.Relative ),
parameters: new Dictionary<string, string> {
{ "per_page", "100" }
},
accepts: "application/vnd.github.v3+json"
);
Note that System.Text.Json does support record types. I can work around this issue with the following call:
IApiResponse<byte[]> rawResponse = await m_github.Connection.GetRaw(
new Uri( m_githubRunnersRoute, UriKind.Relative ),
parameters: new Dictionary<string, string> {
{ "per_page", "100" }
}
);
RunnerListResponse runnerResponse = System.Text.Json.JsonSerializer.Deserialize<RunnerListResponse>( (string)rawResponse.HttpResponse.Body );
But I would very much prefer not to do this
Switching to Refit (see #2562) would make generic interfaces possible as well and uses System.Text.Json by default
Closing this in favor of: https://github.com/octokit/octokit.net/issues/2562