Skip to content
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<PackageIconUrl>https://github.com/testcontainers/Docker.DotNet/raw/main/icon.png</PackageIconUrl>
</PropertyGroup>

<PropertyGroup>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
<PackageId>Docker.DotNet.Enhanced.Handler.Abstractions</PackageId>
<Description>An abstraction layer for Docker.DotNet that defines the classes and interfaces for implementing Docker Engine handlers.</Description>
</PropertyGroup>
<PropertyGroup>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
</ItemGroup>
Expand Down
7 changes: 5 additions & 2 deletions src/Docker.DotNet.X509/CertificateCredentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public CertificateCredentials(X509Certificate2 certificate)

public bool TlsEnabled => true;

public RemoteCertificateValidationCallback ServerCertificateValidationCallback { get; set; }
public RemoteCertificateValidationCallback? ServerCertificateValidationCallback { get; set; }

public HttpMessageHandler ConfigureHandler(HttpMessageHandler handler)
{
Expand All @@ -35,7 +35,10 @@ public HttpMessageHandler ConfigureHandler(HttpMessageHandler handler)
httpHandler.ClientCertificates.Add(_certificate);
}

httpHandler.ServerCertificateCustomValidationCallback = (message, certificate, chain, sslPolicyErrors) => ServerCertificateValidationCallback(message, certificate, chain, sslPolicyErrors);
if (ServerCertificateValidationCallback is { } serverCertificateValidationCallback)
{
httpHandler.ServerCertificateCustomValidationCallback = (message, certificate, chain, sslPolicyErrors) => serverCertificateValidationCallback(message, certificate, chain, sslPolicyErrors);
}
return httpHandler;
}
#endif
Expand Down
3 changes: 0 additions & 3 deletions src/Docker.DotNet/Docker.DotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
<PackageId>Docker.DotNet.Enhanced</PackageId>
<Description>A .NET client for the Docker Engine API with fully asynchronous, non-blocking, object-oriented APIs.</Description>
</PropertyGroup>
<PropertyGroup>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/Docker.DotNet/EnumerableQueryStringConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal class EnumerableQueryStringConverter : IQueryStringConverter
{
public bool CanConvert(Type t)
{
return typeof (IEnumerable).GetTypeInfo().IsAssignableFrom(t.GetTypeInfo());
return typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(t.GetTypeInfo());
}

public string[] Convert(object o)
Expand All @@ -16,12 +16,12 @@ public string[] Convert(object o)
Debug.Assert(o is IEnumerable);

var items = new List<string>();
foreach (var e in ((IEnumerable) o))
foreach (var e in (IEnumerable)o!)
{
if (e is ValueType ||
e is string)
{
items.Add(e.ToString());
items.Add(e.ToString()!);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/Docker.DotNet/JsonEnumMemberConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ internal sealed class JsonEnumMemberConverter<TEnum> : JsonConverter<TEnum> wher
private readonly Dictionary<string, string> _enumFields = typeof(TEnum).GetFields(BindingFlags.Public | BindingFlags.Static)
.Select(field => (Name: field.Name, Attribute: field.GetCustomAttribute<EnumMemberAttribute>()))
.Where(item => item.Attribute != null && item.Attribute.Value != null)
.ToDictionary(item => item.Name, item => item.Attribute.Value);
.ToDictionary(item => item.Name, item => item.Attribute!.Value!);

public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Docker.DotNet/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public byte[] SerializeToUtf8Bytes<T>(T value)

public T Deserialize<T>(byte[] json)
{
return System.Text.Json.JsonSerializer.Deserialize<T>(json, _options);
return System.Text.Json.JsonSerializer.Deserialize<T>(json, _options)!;
}

public Task<T> DeserializeAsync<T>(HttpContent content, CancellationToken cancellationToken)
{
return content.ReadFromJsonAsync<T>(_options, cancellationToken);
return content.ReadFromJsonAsync<T>(_options, cancellationToken)!;
}

public async IAsyncEnumerable<T> DeserializeAsync<T>(Stream stream, [EnumeratorCancellation] CancellationToken cancellationToken)
Expand All @@ -69,7 +69,7 @@ public async IAsyncEnumerable<T> DeserializeAsync<T>(Stream stream, [EnumeratorC

while (!buffer.IsEmpty && TryParseJson(ref buffer, out var jsonDocument))
{
yield return jsonDocument.Deserialize<T>(_options);
yield return jsonDocument!.Deserialize<T>(_options)!;
}

if (result.IsCompleted)
Expand All @@ -83,7 +83,7 @@ public async IAsyncEnumerable<T> DeserializeAsync<T>(Stream stream, [EnumeratorC
await reader.CompleteAsync();
}

private static bool TryParseJson(ref ReadOnlySequence<byte> buffer, out JsonDocument jsonDocument)
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/ObjectExtensionData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ namespace Docker.DotNet.Models;
public class ObjectExtensionData
{
[JsonExtensionData]
public IDictionary<string, object> ExtensionData { get; set; }
public IDictionary<string, object>? ExtensionData { get; set; }
}
41 changes: 22 additions & 19 deletions src/Docker.DotNet/QueryString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ internal class QueryString<T> : IQueryString where T : class
{
private T Object { get; }

private Tuple<PropertyInfo, QueryStringParameterAttribute>[] AttributedPublicProperties { get; }
private Dictionary<PropertyInfo, QueryStringParameterAttribute> AttributedPublicProperties { get; }

private IQueryStringConverterInstanceFactory QueryStringConverterInstanceFactory { get; }

Expand All @@ -25,8 +25,8 @@ public IDictionary<string, string[]> GetKeyValuePairs()
var queryParameters = new Dictionary<string, string[]>();
foreach (var pair in AttributedPublicProperties)
{
var property = pair.Item1;
var attribute = pair.Item2;
var property = pair.Key;
var attribute = pair.Value;
var value = property.GetValue(Object, null);

// 'Required' check
Expand All @@ -43,12 +43,12 @@ public IDictionary<string, string[]> GetKeyValuePairs()
string[] valueStr;
if (attribute.ConverterType == null)
{
valueStr = new[] { value.ToString() };
valueStr = [value!.ToString()!];
}
else
{
var converter = QueryStringConverterInstanceFactory.GetConverterInstance(attribute.ConverterType);
valueStr = ConvertValue(converter, value);
valueStr = ConvertValue(converter, value!);

if (valueStr == null)
{
Expand Down Expand Up @@ -76,7 +76,7 @@ public string GetQueryString()
v => $"{Uri.EscapeDataString(pair.Key)}={Uri.EscapeDataString(v)}"))));
}

private string[] ConvertValue(IQueryStringConverter converter, object value)
private static string[] ConvertValue(IQueryStringConverter converter, object value)
{
if (!converter.CanConvert(value.GetType()))
{
Expand All @@ -86,30 +86,33 @@ private string[] ConvertValue(IQueryStringConverter converter, object value)
return converter.Convert(value);
}

private Tuple<PropertyInfo, TAttribType>[] FindAttributedPublicProperties<TValue, TAttribType>() where TAttribType : Attribute
private static Dictionary<PropertyInfo, TAttribType> FindAttributedPublicProperties<TValue, TAttribType>() where TAttribType : Attribute
{
var t = typeof(TValue);
var ofAttributeType = typeof(TAttribType);
Dictionary<PropertyInfo, TAttribType>? attributedPublicProperties = null;

var properties = t.GetProperties();
var publicProperties = properties.Where(p => p.GetGetMethod(false).IsPublic);
if (!publicProperties.Any())
foreach (var prop in t.GetProperties())
{
throw new InvalidOperationException($"No public property getters found on type {t.FullName}.");
if (prop.GetGetMethod(false)?.IsPublic == true)
{
var attribute = prop.GetCustomAttribute<TAttribType>();
if (attribute != null)
{
attributedPublicProperties ??= new Dictionary<PropertyInfo, TAttribType>();
attributedPublicProperties.Add(prop, attribute);
}
}
}

var attributedPublicProperties = properties.Where(p => p.GetCustomAttribute<TAttribType>() != null).ToArray();
Copy link
Author

Choose a reason for hiding this comment

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

This was wrong before because it used the properties variable instead of publicProperties.

if (!attributedPublicProperties.Any())
if (attributedPublicProperties == null)
{
throw new InvalidOperationException(
$"No public properties attributed with [{ofAttributeType.FullName}] found on type {t.FullName}.");
throw new InvalidOperationException($"No public properties attributed with [{typeof(TAttribType).FullName}] found on type {t.FullName}.");
}

return attributedPublicProperties.Select(pi =>
new Tuple<PropertyInfo, TAttribType>(pi, pi.GetCustomAttribute<TAttribType>())).ToArray();
return attributedPublicProperties;
}

private static bool IsDefaultOfType(object o)
private static bool IsDefaultOfType(object? o)
{
if (o is ValueType)
{
Expand Down
10 changes: 5 additions & 5 deletions src/Microsoft.Net.Http.Client/BufferedReadStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ internal sealed class BufferedReadStream : WriteClosableStream, IPeekableStream
{
private readonly Stream _inner;

private readonly Socket _socket;
private readonly Socket? _socket;

private readonly byte[] _buffer;

Expand All @@ -16,14 +16,14 @@ internal sealed class BufferedReadStream : WriteClosableStream, IPeekableStream

private int _bufferCount;

private MemoryStream _readLineBuffer;
private MemoryStream? _readLineBuffer;

public BufferedReadStream(Stream inner, Socket socket, ILogger logger)
public BufferedReadStream(Stream inner, Socket? socket, ILogger logger)
: this(inner, socket, 8192, logger)
{
}

public BufferedReadStream(Stream inner, Socket socket, int bufferLength, ILogger logger)
public BufferedReadStream(Stream inner, Socket? socket, int bufferLength, ILogger logger)
{
_inner = inner ?? throw new ArgumentNullException(nameof(inner));
_socket = socket;
Expand Down Expand Up @@ -146,7 +146,7 @@ public bool Peek(byte[] buffer, uint toPeek, out uint peeked, out uint available
throw new NotSupportedException("_inner stream isn't a peekable stream");
}

public async Task<string> ReadLineAsync(CancellationToken cancellationToken)
public async Task<string?> ReadLineAsync(CancellationToken cancellationToken)
{
if (_readLineBuffer == null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Net.Http.Client/HttpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private async Task<List<string>> ReadResponseLinesAsync(CancellationToken cancel
break;
}

lines.Add(line);
lines.Add(line!);
}
while (true);

Expand Down
14 changes: 7 additions & 7 deletions src/Microsoft.Net.Http.Client/HttpConnectionResponseContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Microsoft.Net.Http.Client;
public class HttpConnectionResponseContent : HttpContent
{
private readonly HttpConnection _connection;
private Stream _responseStream;
private Stream? _responseStream;

internal HttpConnectionResponseContent(HttpConnection connection)
{
Expand Down Expand Up @@ -40,21 +40,21 @@ public WriteClosableStream HijackStream()
return _connection.Transport;
}

protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context)
{
return _responseStream.CopyToAsync(stream);
return _responseStream!.CopyToAsync(stream);
}

#if NET6_0_OR_GREATER
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context, CancellationToken cancellationToken)
protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cancellationToken)
{
return _responseStream.CopyToAsync(stream, cancellationToken);
return _responseStream!.CopyToAsync(stream, cancellationToken);
}
#endif

protected override Task<Stream> CreateContentReadStreamAsync()
{
return Task.FromResult(_responseStream);
return Task.FromResult(_responseStream!);
}

protected override bool TryComputeLength(out long length)
Expand All @@ -69,7 +69,7 @@ protected override void Dispose(bool disposing)
{
if (disposing)
{
_responseStream.Dispose();
_responseStream?.Dispose();
_connection.Dispose();
}
}
Expand Down
Loading
Loading