Skip to content
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ jobs:
run: |
./test/Docker.DotNet.Tests/bin/Release/${{ matrix.dotnet.tfm }}/linux-x64/publish/Docker.DotNet.Tests
./test/Docker.DotNet.TestsV2/bin/Release/${{ matrix.dotnet.tfm }}/linux-x64/publish/Docker.DotNet.TestsV2
./test/Docker.DotNet.Tests.StrictJson/bin/Release/${{ matrix.dotnet.tfm }}/linux-x64/publish/Docker.DotNet.Tests.StrictJson
env:
DOCKER_HOST: ${{ matrix.docker.docker_host }}
DOCKER_TLS_VERIFY: ${{ matrix.docker.tls_verify }}
Expand Down
1 change: 1 addition & 0 deletions Docker.DotNet.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<Folder Name="/test/">
<Project Path="test/Docker.DotNet.Benchmarks/Docker.DotNet.Benchmarks.csproj" />
<Project Path="test/Docker.DotNet.Tests/Docker.DotNet.Tests.csproj" />
<Project Path="test/Docker.DotNet.Tests.StrictJson/Docker.DotNet.Tests.StrictJson.csproj" />
<Project Path="test/Docker.DotNet.TestsV2/Docker.DotNet.TestsV2.csproj" />
</Folder>
<Folder Name="/tools/">
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,21 @@ var client = new DockerClientBuilder()
.Build();
```

### Strict JSON deserialization

Docker-compatible container runtimes and older Docker Engine versions do not always send what the Docker Engine API declares.
That is why the client ignores the global System.Text.Json `RespectNullableAnnotationsDefault` and `RespectRequiredConstructorParametersDefault` feature switches and deserializes responses leniently.
On .NET 9 and later, you can opt in to strict deserialization with the client's own switches:

```xml
<ItemGroup>
<RuntimeHostConfigurationOption Include="Docker.DotNet.Serialization.RespectNullableAnnotations" Value="true" />
<RuntimeHostConfigurationOption Include="Docker.DotNet.Serialization.RespectRequiredConstructorParameters" Value="true" />
</ItemGroup>
```

The switches apply to the whole process and are read once. If you set them with `AppContext.SetSwitch` instead, call it before you create the first client.

### Error Handling

Here are typical exceptions thrown from the client library:
Expand Down
1 change: 1 addition & 0 deletions src/Docker.DotNet/Docker.DotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="Docker.DotNet.Tests" />
<InternalsVisibleTo Include="Docker.DotNet.Tests.StrictJson" />
</ItemGroup>
<ItemGroup>
<Using Include="System" />
Expand Down
20 changes: 20 additions & 0 deletions src/Docker.DotNet/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ private JsonSerializer()
_options.Converters.Add(new JsonEnumMemberConverter<TaskState>());
_options.Converters.Add(new JsonDateTimeConverter());
_options.Converters.Add(new JsonTimeSpanNanosecondsConverter());
#if NET9_0_OR_GREATER
// Docker-compatible container runtimes do not always send what the Docker
// Engine API declares. Ignore the global System.Text.Json feature switches
// and enable strict deserialization only on explicit opt-in:
// https://github.com/testcontainers/Docker.DotNet/issues/129
_options.RespectNullableAnnotations = IsSwitchEnabled("Docker.DotNet.Serialization.RespectNullableAnnotations");
_options.RespectRequiredConstructorParameters = IsSwitchEnabled("Docker.DotNet.Serialization.RespectRequiredConstructorParameters");
#else
// Applications can upgrade to System.Text.Json 9.0 or later, which honors
// the global feature switches, too. Disable the options if they exist.
typeof(JsonSerializerOptions).GetProperty("RespectNullableAnnotations")?.SetValue(_options, false);
typeof(JsonSerializerOptions).GetProperty("RespectRequiredConstructorParameters")?.SetValue(_options, false);
#endif
_options.MakeReadOnly();
}

Expand Down Expand Up @@ -98,6 +111,13 @@ public async IAsyncEnumerable<T> DeserializeAsync<T>(Stream stream, [EnumeratorC
await reader.CompleteAsync();
}

#if NET9_0_OR_GREATER
private static bool IsSwitchEnabled(string switchName)
{
return AppContext.TryGetSwitch(switchName, out var isEnabled) && isEnabled;
}
#endif

private static bool TryParseJson(ref ReadOnlySequence<byte> buffer, out JsonDocument? jsonDocument)
{
var reader = new Utf8JsonReader(buffer, isFinalBlock: false, default);
Expand Down
2 changes: 1 addition & 1 deletion src/Docker.DotNet/Models/Actor.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public class Actor // (events.Actor)
public string ID { get; set; } = string.Empty;

[JsonPropertyName("Attributes")]
public IDictionary<string, string> Attributes { get; set; } = default!;
public IDictionary<string, string>? Attributes { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Docker.DotNet/Models/Annotations.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public class Annotations // (swarm.Annotations)
public string? Name { get; set; }

[JsonPropertyName("Labels")]
public IDictionary<string, string> Labels { get; set; } = default!;
public IDictionary<string, string>? Labels { get; set; }
}
}
16 changes: 8 additions & 8 deletions src/Docker.DotNet/Models/BlkioStats.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ public class BlkioStats // (container.BlkioStats)
/// number of bytes transferred to and from the block device
/// </summary>
[JsonPropertyName("io_service_bytes_recursive")]
public IList<BlkioStatEntry> IoServiceBytesRecursive { get; set; } = default!;
public IList<BlkioStatEntry>? IoServiceBytesRecursive { get; set; }

[JsonPropertyName("io_serviced_recursive")]
public IList<BlkioStatEntry> IoServicedRecursive { get; set; } = default!;
public IList<BlkioStatEntry>? IoServicedRecursive { get; set; }

[JsonPropertyName("io_queue_recursive")]
public IList<BlkioStatEntry> IoQueuedRecursive { get; set; } = default!;
public IList<BlkioStatEntry>? IoQueuedRecursive { get; set; }

[JsonPropertyName("io_service_time_recursive")]
public IList<BlkioStatEntry> IoServiceTimeRecursive { get; set; } = default!;
public IList<BlkioStatEntry>? IoServiceTimeRecursive { get; set; }

[JsonPropertyName("io_wait_time_recursive")]
public IList<BlkioStatEntry> IoWaitTimeRecursive { get; set; } = default!;
public IList<BlkioStatEntry>? IoWaitTimeRecursive { get; set; }

[JsonPropertyName("io_merged_recursive")]
public IList<BlkioStatEntry> IoMergedRecursive { get; set; } = default!;
public IList<BlkioStatEntry>? IoMergedRecursive { get; set; }

[JsonPropertyName("io_time_recursive")]
public IList<BlkioStatEntry> IoTimeRecursive { get; set; } = default!;
public IList<BlkioStatEntry>? IoTimeRecursive { get; set; }

[JsonPropertyName("sectors_recursive")]
public IList<BlkioStatEntry> SectorsRecursive { get; set; } = default!;
public IList<BlkioStatEntry>? SectorsRecursive { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Docker.DotNet/Models/ClusterInfo.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ClusterInfo(Meta Meta)
public bool RootRotationInProgress { get; set; } = default!;

[JsonPropertyName("DefaultAddrPool")]
public IList<string> DefaultAddrPool { get; set; } = default!;
public IList<string>? DefaultAddrPool { get; set; }

[JsonPropertyName("SubnetSize")]
public uint SubnetSize { get; set; } = default!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ public CommitContainerChangesParameters(ContainerConfig Config)
/// List of environment variable to set in the container
/// </summary>
[JsonPropertyName("Env")]
public IList<string> Env { get; set; } = default!;
public IList<string>? Env { get; set; }

/// <summary>
/// Command to run when starting the container
/// </summary>
[JsonPropertyName("Cmd")]
public IList<string> Cmd { get; set; } = default!;
public IList<string>? Cmd { get; set; }

/// <summary>
/// Healthcheck describes how to check the container is healthy
Expand All @@ -153,7 +153,7 @@ public CommitContainerChangesParameters(ContainerConfig Config)
/// List of volumes (mounts) used for the container
/// </summary>
[JsonPropertyName("Volumes")]
public IDictionary<string, EmptyStruct> Volumes { get; set; } = default!;
public IDictionary<string, EmptyStruct>? Volumes { get; set; }

/// <summary>
/// Current directory (PWD) in the command will be launched
Expand All @@ -165,7 +165,7 @@ public CommitContainerChangesParameters(ContainerConfig Config)
/// Entrypoint to run when starting the container
/// </summary>
[JsonPropertyName("Entrypoint")]
public IList<string> Entrypoint { get; set; } = default!;
public IList<string>? Entrypoint { get; set; }

/// <summary>
/// Is network disabled
Expand All @@ -183,7 +183,7 @@ public CommitContainerChangesParameters(ContainerConfig Config)
/// List of labels set to this container
/// </summary>
[JsonPropertyName("Labels")]
public IDictionary<string, string> Labels { get; set; } = default!;
public IDictionary<string, string>? Labels { get; set; }

/// <summary>
/// Signal to stop a container
Expand Down
10 changes: 5 additions & 5 deletions src/Docker.DotNet/Models/ContainerConfig.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ public class ContainerConfig // (container.Config)
/// List of environment variable to set in the container
/// </summary>
[JsonPropertyName("Env")]
public IList<string> Env { get; set; } = default!;
public IList<string>? Env { get; set; }

/// <summary>
/// Command to run when starting the container
/// </summary>
[JsonPropertyName("Cmd")]
public IList<string> Cmd { get; set; } = default!;
public IList<string>? Cmd { get; set; }

/// <summary>
/// Healthcheck describes how to check the container is healthy
Expand All @@ -105,7 +105,7 @@ public class ContainerConfig // (container.Config)
/// List of volumes (mounts) used for the container
/// </summary>
[JsonPropertyName("Volumes")]
public IDictionary<string, EmptyStruct> Volumes { get; set; } = default!;
public IDictionary<string, EmptyStruct>? Volumes { get; set; }

/// <summary>
/// Current directory (PWD) in the command will be launched
Expand All @@ -117,7 +117,7 @@ public class ContainerConfig // (container.Config)
/// Entrypoint to run when starting the container
/// </summary>
[JsonPropertyName("Entrypoint")]
public IList<string> Entrypoint { get; set; } = default!;
public IList<string>? Entrypoint { get; set; }

/// <summary>
/// Is network disabled
Expand All @@ -135,7 +135,7 @@ public class ContainerConfig // (container.Config)
/// List of labels set to this container
/// </summary>
[JsonPropertyName("Labels")]
public IDictionary<string, string> Labels { get; set; } = default!;
public IDictionary<string, string>? Labels { get; set; }

/// <summary>
/// Signal to stop a container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public class ContainerExecCreateParameters // (main.ContainerExecCreateParameter
public string DetachKeys { get; set; } = string.Empty;

[JsonPropertyName("Env")]
public IList<string> Env { get; set; } = default!;
public IList<string>? Env { get; set; }

[JsonPropertyName("WorkingDir")]
public string WorkingDir { get; set; } = string.Empty;

[JsonPropertyName("Cmd")]
public IList<string> Cmd { get; set; } = default!;
public IList<string>? Cmd { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ContainerExecInspectResponse // (container.ExecInspectResponse)
public string ContainerID { get; set; } = string.Empty;

[JsonPropertyName("DetachKeys")]
public string DetachKeys { get; set; } = string.Empty;
public string? DetachKeys { get; set; }

[JsonPropertyName("Pid")]
public long Pid { get; set; } = default!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ContainerInspectResponse // (container.InspectResponse)
public string Path { get; set; } = string.Empty;

[JsonPropertyName("Args")]
public IList<string> Args { get; set; } = default!;
public IList<string>? Args { get; set; }

[JsonPropertyName("State")]
public State? State { get; set; }
Expand Down Expand Up @@ -59,7 +59,7 @@ public class ContainerInspectResponse // (container.InspectResponse)
public string AppArmorProfile { get; set; } = string.Empty;

[JsonPropertyName("ExecIDs")]
public IList<string> ExecIDs { get; set; } = default!;
public IList<string>? ExecIDs { get; set; }

[JsonPropertyName("HostConfig")]
public HostConfig? HostConfig { get; set; }
Expand All @@ -83,7 +83,7 @@ public class ContainerInspectResponse // (container.InspectResponse)
public long? SizeRootFs { get; set; }

[JsonPropertyName("Mounts")]
public IList<MountPoint> Mounts { get; set; } = default!;
public IList<MountPoint>? Mounts { get; set; }

[JsonPropertyName("Config")]
public ContainerConfig? Config { get; set; }
Expand Down
8 changes: 4 additions & 4 deletions src/Docker.DotNet/Models/ContainerListResponse.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ContainerListResponse // (container.Summary)
public string ID { get; set; } = string.Empty;

[JsonPropertyName("Names")]
public IList<string> Names { get; set; } = default!;
public IList<string>? Names { get; set; }

[JsonPropertyName("Image")]
public string Image { get; set; } = string.Empty;
Expand All @@ -29,7 +29,7 @@ public class ContainerListResponse // (container.Summary)
public DateTime Created { get; set; } = default!;

[JsonPropertyName("Ports")]
public IList<PortSummary> Ports { get; set; } = default!;
public IList<PortSummary>? Ports { get; set; }

[JsonPropertyName("SizeRw")]
public long? SizeRw { get; set; }
Expand All @@ -38,7 +38,7 @@ public class ContainerListResponse // (container.Summary)
public long? SizeRootFs { get; set; }

[JsonPropertyName("Labels")]
public IDictionary<string, string> Labels { get; set; } = default!;
public IDictionary<string, string>? Labels { get; set; }

[JsonPropertyName("State")]
public string State { get; set; } = string.Empty;
Expand All @@ -56,6 +56,6 @@ public class ContainerListResponse // (container.Summary)
public NetworkSettingsSummary? NetworkSettings { get; set; }

[JsonPropertyName("Mounts")]
public IList<MountPoint> Mounts { get; set; } = default!;
public IList<MountPoint>? Mounts { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ public class ContainerProcessesResponse // (container.TopResponse)
/// Example: {&quot;Processes&quot;:[[&quot;root&quot;,&quot;13642&quot;,&quot;882&quot;,&quot;0&quot;,&quot;17:03&quot;,&quot;pts/0&quot;,&quot;00:00:00&quot;,&quot;/bin/bash&quot;],[&quot;root&quot;,&quot;13735&quot;,&quot;13642&quot;,&quot;0&quot;,&quot;17:06&quot;,&quot;pts/0&quot;,&quot;00:00:00&quot;,&quot;sleep 10&quot;]]}
/// </summary>
[JsonPropertyName("Processes")]
public IList<IList<string>> Processes { get; set; } = default!;
public IList<IList<string>>? Processes { get; set; }

/// <summary>
/// The ps column titles
/// Example: {&quot;Titles&quot;:[&quot;UID&quot;,&quot;PID&quot;,&quot;PPID&quot;,&quot;C&quot;,&quot;STIME&quot;,&quot;TTY&quot;,&quot;TIME&quot;,&quot;CMD&quot;]}
/// </summary>
[JsonPropertyName("Titles")]
public IList<string> Titles { get; set; } = default!;
public IList<string>? Titles { get; set; }
}
}
18 changes: 9 additions & 9 deletions src/Docker.DotNet/Models/ContainerUpdateParameters.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,19 @@ public ContainerUpdateParameters(UpdateConfig UpdateConfig)
public ushort BlkioWeight { get; set; } = default!;

[JsonPropertyName("BlkioWeightDevice")]
public IList<WeightDevice> BlkioWeightDevice { get; set; } = default!;
public IList<WeightDevice>? BlkioWeightDevice { get; set; }

[JsonPropertyName("BlkioDeviceReadBps")]
public IList<ThrottleDevice> BlkioDeviceReadBps { get; set; } = default!;
public IList<ThrottleDevice>? BlkioDeviceReadBps { get; set; }

[JsonPropertyName("BlkioDeviceWriteBps")]
public IList<ThrottleDevice> BlkioDeviceWriteBps { get; set; } = default!;
public IList<ThrottleDevice>? BlkioDeviceWriteBps { get; set; }

[JsonPropertyName("BlkioDeviceReadIOps")]
public IList<ThrottleDevice> BlkioDeviceReadIOps { get; set; } = default!;
public IList<ThrottleDevice>? BlkioDeviceReadIOps { get; set; }

[JsonPropertyName("BlkioDeviceWriteIOps")]
public IList<ThrottleDevice> BlkioDeviceWriteIOps { get; set; } = default!;
public IList<ThrottleDevice>? BlkioDeviceWriteIOps { get; set; }

/// <summary>
/// CPU CFS (Completely Fair Scheduler) period
Expand Down Expand Up @@ -129,19 +129,19 @@ public ContainerUpdateParameters(UpdateConfig UpdateConfig)
/// List of devices to map inside the container
/// </summary>
[JsonPropertyName("Devices")]
public IList<DeviceMapping> Devices { get; set; } = default!;
public IList<DeviceMapping>? Devices { get; set; }

/// <summary>
/// List of rule to be added to the device cgroup
/// </summary>
[JsonPropertyName("DeviceCgroupRules")]
public IList<string> DeviceCgroupRules { get; set; } = default!;
public IList<string>? DeviceCgroupRules { get; set; }

/// <summary>
/// List of device requests for device drivers
/// </summary>
[JsonPropertyName("DeviceRequests")]
public IList<DeviceRequest> DeviceRequests { get; set; } = default!;
public IList<DeviceRequest>? DeviceRequests { get; set; }

/// <summary>
/// Memory soft limit (in bytes)
Expand Down Expand Up @@ -177,7 +177,7 @@ public ContainerUpdateParameters(UpdateConfig UpdateConfig)
/// List of ulimits to be set in the container
/// </summary>
[JsonPropertyName("Ulimits")]
public IList<Ulimit> Ulimits { get; set; } = default!;
public IList<Ulimit>? Ulimits { get; set; }

/// <summary>
/// Applicable to Windows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace Docker.DotNet.Models
public class ContainerUpdateResponse // (main.ContainerUpdateResponse)
{
[JsonPropertyName("Warnings")]
public IList<string> Warnings { get; set; } = default!;
public IList<string>? Warnings { get; set; }
}
}
Loading