Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate from Newtonsoft.Json to System.Text.Json #672

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<PropertyGroup>
<IsPackable>true</IsPackable>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<LangVersion>latest</LangVersion>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't latest the default? What's the added benefit of this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default language version depends on the target framework, see the C# language versioning documentation.

For .NET Standard 2.0 the language version is C# 7.3 and for .NET Standard 2.1 the language version is C# 8.0.

I don't remember everything I changed but there's a high probability that I used some feature that requires C# greater than 7.3.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake.

But it may be different for different SDK versions.

But, if you're aiming for a version higher than the supported for that target, you need to be very careful.

<PackageIconUrl>https://camo.githubusercontent.com/fa6d5c12609ed8a3ba1163b96f9e9979b8f59b0d/687474703a2f2f7765732e696f2f566663732f636f6e74656e74</PackageIconUrl>
<Copyright>Copyright (c) .NET Foundation and Contributors</Copyright>
<PackageTags>Docker Container C# .NET</PackageTags>
Expand Down
4 changes: 3 additions & 1 deletion src/Docker.DotNet/Docker.DotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
<AssemblyName>Docker.DotNet</AssemblyName>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Buffers" Version="4.5.1" />
<PackageReference Include="System.IO.Pipelines" Version="8.0.0" />
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.2" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
</ItemGroup>
</Project>
17 changes: 0 additions & 17 deletions src/Docker.DotNet/DockerApiResponse.cs

This file was deleted.

73 changes: 61 additions & 12 deletions src/Docker.DotNet/DockerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -157,49 +158,92 @@ public void Dispose()
_client.Dispose();
}

internal Task<DockerApiResponse> MakeRequestAsync(
internal Task MakeRequestAsync(
IEnumerable<ApiResponseErrorHandlingDelegate> errorHandlers,
HttpMethod method,
string path,
CancellationToken token)
{
return MakeRequestAsync(errorHandlers, method, path, null, null, token);
return MakeRequestAsync<NoContent>(errorHandlers, method, path, null, null, token);
}

internal Task<DockerApiResponse> MakeRequestAsync(
internal Task<T> MakeRequestAsync<T>(
IEnumerable<ApiResponseErrorHandlingDelegate> errorHandlers,
HttpMethod method,
string path,
CancellationToken token)
{
return MakeRequestAsync<T>(errorHandlers, method, path, null, null, token);
}

internal Task MakeRequestAsync(
IEnumerable<ApiResponseErrorHandlingDelegate> errorHandlers,
HttpMethod method,
string path,
IQueryString queryString,
CancellationToken token)
{
return MakeRequestAsync<NoContent>(errorHandlers, method, path, queryString, null, token);
}

internal Task<T> MakeRequestAsync<T>(
IEnumerable<ApiResponseErrorHandlingDelegate> errorHandlers,
HttpMethod method,
string path,
IQueryString queryString,
CancellationToken token)
{
return MakeRequestAsync<T>(errorHandlers, method, path, queryString, null, token);
}

internal Task MakeRequestAsync(
IEnumerable<ApiResponseErrorHandlingDelegate> errorHandlers,
HttpMethod method,
string path,
IQueryString queryString,
IRequestContent body,
CancellationToken token)
{
return MakeRequestAsync<NoContent>(errorHandlers, method, path, queryString, body, null, token);
}

internal Task<T> MakeRequestAsync<T>(
IEnumerable<ApiResponseErrorHandlingDelegate> errorHandlers,
HttpMethod method,
string path,
IQueryString queryString,
IRequestContent body,
CancellationToken token)
{
return MakeRequestAsync(errorHandlers, method, path, queryString, null, token);
return MakeRequestAsync<T>(errorHandlers, method, path, queryString, body, null, token);
}

internal Task<DockerApiResponse> MakeRequestAsync(
internal Task<T> MakeRequestAsync<T>(
IEnumerable<ApiResponseErrorHandlingDelegate> errorHandlers,
HttpMethod method,
string path,
IQueryString queryString,
IRequestContent body,
IDictionary<string, string> headers,
CancellationToken token)
{
return MakeRequestAsync(errorHandlers, method, path, queryString, body, null, token);
return MakeRequestAsync<T>(errorHandlers, method, path, queryString, body, headers, DefaultTimeout, token);
}

internal Task<DockerApiResponse> MakeRequestAsync(
internal Task MakeRequestAsync(
IEnumerable<ApiResponseErrorHandlingDelegate> errorHandlers,
HttpMethod method,
string path,
IQueryString queryString,
IRequestContent body,
IDictionary<string, string> headers,
TimeSpan timeout,
CancellationToken token)
{
return MakeRequestAsync(errorHandlers, method, path, queryString, body, headers, DefaultTimeout, token);
return MakeRequestAsync<NoContent>(errorHandlers, method, path, queryString, body, headers, timeout, token);
}

internal async Task<DockerApiResponse> MakeRequestAsync(
internal async Task<T> MakeRequestAsync<T>(
IEnumerable<ApiResponseErrorHandlingDelegate> errorHandlers,
HttpMethod method,
string path,
Expand All @@ -217,10 +261,13 @@ internal async Task<DockerApiResponse> MakeRequestAsync(
await HandleIfErrorResponseAsync(response.StatusCode, response, errorHandlers)
.ConfigureAwait(false);

var responseBody = await response.Content.ReadAsStringAsync()
.ConfigureAwait(false);
if (typeof(T) == typeof(NoContent))
{
return default;
}

return new DockerApiResponse(response.StatusCode, responseBody);
return await JsonSerializer.DeserializeAsync<T>(response.Content, token)
.ConfigureAwait(false);
}
}

Expand Down Expand Up @@ -469,6 +516,8 @@ private async Task HandleIfErrorResponseAsync(HttpStatusCode statusCode, HttpRes
throw new DockerApiException(statusCode, responseBody);
}
}

private struct NoContent {}
}

internal delegate void ApiResponseErrorHandlingDelegate(HttpStatusCode statusCode, string responseBody);
Expand Down
9 changes: 3 additions & 6 deletions src/Docker.DotNet/Endpoints/ConfigsOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ internal ConfigOperations(DockerClient client)

async Task<IList<SwarmConfig>> IConfigOperations.ListConfigsAsync(CancellationToken cancellationToken)
{
var response = await this._client.MakeRequestAsync(this._client.NoErrorHandlers, HttpMethod.Get, "configs", cancellationToken).ConfigureAwait(false);
return this._client.JsonSerializer.DeserializeObject<IList<SwarmConfig>>(response.Body);
return await this._client.MakeRequestAsync<IList<SwarmConfig>>(this._client.NoErrorHandlers, HttpMethod.Get, "configs", cancellationToken).ConfigureAwait(false);
}

async Task<SwarmCreateConfigResponse> IConfigOperations.CreateConfigAsync(SwarmCreateConfigParameters body, CancellationToken cancellationToken)
Expand All @@ -30,8 +29,7 @@ async Task<SwarmCreateConfigResponse> IConfigOperations.CreateConfigAsync(SwarmC
}

var data = new JsonRequestContent<SwarmConfigSpec>(body.Config, this._client.JsonSerializer);
var response = await this._client.MakeRequestAsync(this._client.NoErrorHandlers, HttpMethod.Post, "configs/create", null, data, cancellationToken).ConfigureAwait(false);
return this._client.JsonSerializer.DeserializeObject<SwarmCreateConfigResponse>(response.Body);
return await this._client.MakeRequestAsync<SwarmCreateConfigResponse>(this._client.NoErrorHandlers, HttpMethod.Post, "configs/create", null, data, cancellationToken).ConfigureAwait(false);
}

async Task<SwarmConfig> IConfigOperations.InspectConfigAsync(string id, CancellationToken cancellationToken)
Expand All @@ -41,8 +39,7 @@ async Task<SwarmConfig> IConfigOperations.InspectConfigAsync(string id, Cancella
throw new ArgumentNullException(nameof(id));
}

var response = await this._client.MakeRequestAsync(this._client.NoErrorHandlers, HttpMethod.Get, $"configs/{id}", cancellationToken).ConfigureAwait(false);
return this._client.JsonSerializer.DeserializeObject<SwarmConfig>(response.Body);
return await this._client.MakeRequestAsync<SwarmConfig>(this._client.NoErrorHandlers, HttpMethod.Get, $"configs/{id}", cancellationToken).ConfigureAwait(false);
}

Task IConfigOperations.RemoveConfigAsync(string id, CancellationToken cancellationToken)
Expand Down
39 changes: 15 additions & 24 deletions src/Docker.DotNet/Endpoints/ContainerOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -43,8 +42,7 @@ internal ContainerOperations(DockerClient client)
}

IQueryString queryParameters = new QueryString<ContainersListParameters>(parameters);
var response = await this._client.MakeRequestAsync(this._client.NoErrorHandlers, HttpMethod.Get, "containers/json", queryParameters, cancellationToken).ConfigureAwait(false);
return this._client.JsonSerializer.DeserializeObject<ContainerListResponse[]>(response.Body);
return await this._client.MakeRequestAsync<ContainerListResponse[]>(this._client.NoErrorHandlers, HttpMethod.Get, "containers/json", queryParameters, cancellationToken).ConfigureAwait(false);
}

public async Task<CreateContainerResponse> CreateContainerAsync(CreateContainerParameters parameters, CancellationToken cancellationToken = default(CancellationToken))
Expand All @@ -62,8 +60,7 @@ internal ContainerOperations(DockerClient client)
}

var data = new JsonRequestContent<CreateContainerParameters>(parameters, this._client.JsonSerializer);
var response = await this._client.MakeRequestAsync(new[] { NoSuchImageHandler }, HttpMethod.Post, "containers/create", qs, data, cancellationToken).ConfigureAwait(false);
return this._client.JsonSerializer.DeserializeObject<CreateContainerResponse>(response.Body);
return await this._client.MakeRequestAsync<CreateContainerResponse>(new[] { NoSuchImageHandler }, HttpMethod.Post, "containers/create", qs, data, cancellationToken).ConfigureAwait(false);
}

public async Task<ContainerInspectResponse> InspectContainerAsync(string id, CancellationToken cancellationToken = default(CancellationToken))
Expand All @@ -73,8 +70,7 @@ internal ContainerOperations(DockerClient client)
throw new ArgumentNullException(nameof(id));
}

var response = await this._client.MakeRequestAsync(new[] { NoSuchContainerHandler }, HttpMethod.Get, $"containers/{id}/json", cancellationToken).ConfigureAwait(false);
return this._client.JsonSerializer.DeserializeObject<ContainerInspectResponse>(response.Body);
return await this._client.MakeRequestAsync<ContainerInspectResponse>(new[] { NoSuchContainerHandler }, HttpMethod.Get, $"containers/{id}/json", cancellationToken).ConfigureAwait(false);
}

public async Task<ContainerProcessesResponse> ListProcessesAsync(string id, ContainerListProcessesParameters parameters, CancellationToken cancellationToken = default(CancellationToken))
Expand All @@ -90,8 +86,7 @@ internal ContainerOperations(DockerClient client)
}

IQueryString queryParameters = new QueryString<ContainerListProcessesParameters>(parameters);
var response = await this._client.MakeRequestAsync(new[] { NoSuchContainerHandler }, HttpMethod.Get, $"containers/{id}/top", queryParameters, cancellationToken).ConfigureAwait(false);
return this._client.JsonSerializer.DeserializeObject<ContainerProcessesResponse>(response.Body);
return await this._client.MakeRequestAsync<ContainerProcessesResponse>(new[] { NoSuchContainerHandler }, HttpMethod.Get, $"containers/{id}/top", queryParameters, cancellationToken).ConfigureAwait(false);
}

public Task<Stream> GetContainerLogsAsync(string id, ContainerLogsParameters parameters, CancellationToken cancellationToken = default(CancellationToken))
Expand Down Expand Up @@ -145,8 +140,7 @@ public Task GetContainerLogsAsync(string id, ContainerLogsParameters parameters,
throw new ArgumentNullException(nameof(id));
}

var response = await this._client.MakeRequestAsync(new[] { NoSuchContainerHandler }, HttpMethod.Get, $"containers/{id}/changes", cancellationToken).ConfigureAwait(false);
return this._client.JsonSerializer.DeserializeObject<ContainerFileSystemChangeResponse[]>(response.Body);
return await this._client.MakeRequestAsync<ContainerFileSystemChangeResponse[]>(new[] { NoSuchContainerHandler }, HttpMethod.Get, $"containers/{id}/changes", cancellationToken).ConfigureAwait(false);
}

public Task<Stream> ExportContainerAsync(string id, CancellationToken cancellationToken)
Expand Down Expand Up @@ -208,8 +202,9 @@ public Task<Stream> GetContainerStatsAsync(string id, ContainerStatsParameters p
}

var queryParams = parameters == null ? null : new QueryString<ContainerStartParameters>(parameters);
var response = await this._client.MakeRequestAsync(new[] { NoSuchContainerHandler }, HttpMethod.Post, $"containers/{id}/start", queryParams, cancellationToken).ConfigureAwait(false);
return response.StatusCode != HttpStatusCode.NotModified;
bool? result = null;
await this._client.MakeRequestAsync(new[] { NoSuchContainerHandler, (statusCode, _) => result = statusCode != HttpStatusCode.NotModified }, HttpMethod.Post, $"containers/{id}/start", queryParams, cancellationToken).ConfigureAwait(false);
return result ?? throw new InvalidOperationException();
}

public async Task<bool> StopContainerAsync(string id, ContainerStopParameters parameters, CancellationToken cancellationToken = default(CancellationToken))
Expand All @@ -227,8 +222,9 @@ public Task<Stream> GetContainerStatsAsync(string id, ContainerStatsParameters p
IQueryString queryParameters = new QueryString<ContainerStopParameters>(parameters);
// since specified wait timespan can be greater than HttpClient's default, we set the
// client timeout to infinite and provide a cancellation token.
var response = await this._client.MakeRequestAsync(new[] { NoSuchContainerHandler }, HttpMethod.Post, $"containers/{id}/stop", queryParameters, null, null, TimeSpan.FromMilliseconds(Timeout.Infinite), cancellationToken).ConfigureAwait(false);
return response.StatusCode != HttpStatusCode.NotModified;
bool? result = null;
await this._client.MakeRequestAsync(new[] { NoSuchContainerHandler, (statusCode, _) => result = statusCode != HttpStatusCode.NotModified }, HttpMethod.Post, $"containers/{id}/stop", queryParameters, null, null, TimeSpan.FromMilliseconds(Timeout.Infinite), cancellationToken).ConfigureAwait(false);
return result ?? throw new InvalidOperationException();
}

public Task RestartContainerAsync(string id, ContainerRestartParameters parameters, CancellationToken cancellationToken = default(CancellationToken))
Expand Down Expand Up @@ -327,8 +323,7 @@ public Task RenameContainerAsync(string id, ContainerRenameParameters parameters
throw new ArgumentNullException(nameof(id));
}

var response = await this._client.MakeRequestAsync(new[] { NoSuchContainerHandler }, HttpMethod.Post, $"containers/{id}/wait", null, null, null, TimeSpan.FromMilliseconds(Timeout.Infinite), cancellationToken).ConfigureAwait(false);
return this._client.JsonSerializer.DeserializeObject<ContainerWaitResponse>(response.Body);
return await this._client.MakeRequestAsync<ContainerWaitResponse>(new[] { NoSuchContainerHandler }, HttpMethod.Post, $"containers/{id}/wait", null, null, null, TimeSpan.FromMilliseconds(Timeout.Infinite), cancellationToken).ConfigureAwait(false);
}

public Task RemoveContainerAsync(string id, ContainerRemoveParameters parameters, CancellationToken cancellationToken = default(CancellationToken))
Expand Down Expand Up @@ -367,9 +362,7 @@ public Task RenameContainerAsync(string id, ContainerRenameParameters parameters

var bytes = Convert.FromBase64String(statHeader);

var stat = Encoding.UTF8.GetString(bytes, 0, bytes.Length);

var pathStat = this._client.JsonSerializer.DeserializeObject<ContainerPathStatResponse>(stat);
var pathStat = this._client.JsonSerializer.DeserializeObject<ContainerPathStatResponse>(bytes);

return new GetArchiveFromContainerResponse
{
Expand Down Expand Up @@ -399,8 +392,7 @@ public Task RenameContainerAsync(string id, ContainerRenameParameters parameters
public async Task<ContainersPruneResponse> PruneContainersAsync(ContainersPruneParameters parameters, CancellationToken cancellationToken)
{
var queryParameters = parameters == null ? null : new QueryString<ContainersPruneParameters>(parameters);
var response = await this._client.MakeRequestAsync(this._client.NoErrorHandlers, HttpMethod.Post, "containers/prune", queryParameters, cancellationToken).ConfigureAwait(false);
return this._client.JsonSerializer.DeserializeObject<ContainersPruneResponse>(response.Body);
return await this._client.MakeRequestAsync<ContainersPruneResponse>(this._client.NoErrorHandlers, HttpMethod.Post, "containers/prune", queryParameters, cancellationToken).ConfigureAwait(false);
}

public async Task<ContainerUpdateResponse> UpdateContainerAsync(string id, ContainerUpdateParameters parameters, CancellationToken cancellationToken = default(CancellationToken))
Expand All @@ -416,8 +408,7 @@ public async Task<ContainersPruneResponse> PruneContainersAsync(ContainersPruneP
}

var data = new JsonRequestContent<ContainerUpdateParameters>(parameters, this._client.JsonSerializer);
var response = await this._client.MakeRequestAsync(new[] { NoSuchContainerHandler }, HttpMethod.Post, $"containers/{id}/update", null, data, cancellationToken);
return this._client.JsonSerializer.DeserializeObject<ContainerUpdateResponse>(response.Body);
return await this._client.MakeRequestAsync<ContainerUpdateResponse>(new[] { NoSuchContainerHandler }, HttpMethod.Post, $"containers/{id}/update", null, data, cancellationToken);
}
}
}
Loading
Loading