octokit.net icon indicating copy to clipboard operation
octokit.net copied to clipboard

Support deserializing C# record types

Open cesar-d2l opened this issue 3 years ago • 1 comments
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

cesar-d2l avatar Apr 01 '22 20:04 cesar-d2l

Switching to Refit (see #2562) would make generic interfaces possible as well and uses System.Text.Json by default

hansmbakker avatar Sep 06 '22 11:09 hansmbakker

Closing this in favor of: https://github.com/octokit/octokit.net/issues/2562

nickfloyd avatar Oct 16 '23 21:10 nickfloyd