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

Enable nullable reference types for MediaBrowser.Common #5982

Merged
merged 2 commits into from May 6, 2021
Merged
Changes from 1 commit
Commits
File filter
Filter file types
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.

Always

Just for now

Next
Enable nullable reference types for MediaBrowser.Common
  • Loading branch information
Bond-009 committed May 5, 2021
commit 91c2a57b284011a21c926e9238dbc7edb5eaab13
@@ -77,8 +77,9 @@ public abstract class BaseAuthorizationHandler<T> : AuthorizationHandler<T>
return false;
}

var ip = _httpContextAccessor.HttpContext.GetNormalizedRemoteIp();
var isInLocalNetwork = _networkManager.IsInLocalNetwork(ip);
var isInLocalNetwork = _httpContextAccessor.HttpContext != null
&& _networkManager.IsInLocalNetwork(_httpContextAccessor.HttpContext.GetNormalizedRemoteIp());

// User cannot access remotely and user is remote
if (!user.HasPermission(PermissionKind.EnableRemoteAccess) && !isInLocalNetwork)
{
@@ -1,3 +1,4 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Net.Mime;
using System.Text.Json;
@@ -94,6 +95,11 @@ public async Task<ActionResult> UpdateNamedConfiguration([FromRoute, Required] s
{
var configurationType = _configurationManager.GetConfigurationType(key);
var configuration = await JsonSerializer.DeserializeAsync(Request.Body, configurationType, _serializerOptions).ConfigureAwait(false);
if (configuration == null)
{
throw new ArgumentException("Body doesn't contain a valid configuration");
}

_configurationManager.SaveConfiguration(key, configuration);
return NoContent();
}
@@ -1,3 +1,5 @@
#nullable disable

using System;

namespace MediaBrowser.Common.Configuration
@@ -1,3 +1,4 @@
#nullable disable
#pragma warning disable CS1591

using System;
@@ -1,3 +1,5 @@
#nullable disable

namespace MediaBrowser.Common.Configuration
{
/// <summary>
@@ -1,5 +1,4 @@
#pragma warning disable CS1591
#nullable enable

using System;
using System.Collections.Generic;
@@ -17,7 +17,7 @@ public static class EventHelper
/// <param name="sender">The sender.</param>
/// <param name="args">The <see cref="EventArgs" /> instance containing the event data.</param>
/// <param name="logger">The logger.</param>
public static void QueueEventIfNotNull(EventHandler handler, object sender, EventArgs args, ILogger logger)
public static void QueueEventIfNotNull(EventHandler? handler, object sender, EventArgs args, ILogger logger)
{
if (handler != null)
{
@@ -43,7 +43,7 @@ public static void QueueEventIfNotNull(EventHandler handler, object sender, Even
/// <param name="sender">The sender.</param>
/// <param name="args">The args.</param>
/// <param name="logger">The logger.</param>
public static void QueueEventIfNotNull<T>(EventHandler<T> handler, object sender, T args, ILogger logger)
public static void QueueEventIfNotNull<T>(EventHandler<T>? handler, object sender, T args, ILogger logger)
{
if (handler != null)
{
@@ -1,5 +1,3 @@
#nullable enable

using System;
using System.Security.Cryptography;
using System.Text;
@@ -1,5 +1,3 @@
#nullable enable

using System.Collections.Generic;

namespace MediaBrowser.Common.Extensions
@@ -17,7 +17,7 @@ public static bool IsLocal(this HttpContext context)
{
return (context.Connection.LocalIpAddress == null
&& context.Connection.RemoteIpAddress == null)
|| context.Connection.LocalIpAddress.Equals(context.Connection.RemoteIpAddress);
|| Equals(context.Connection.LocalIpAddress, context.Connection.RemoteIpAddress);
}

/// <summary>
@@ -1,5 +1,3 @@
#nullable enable

using System;

namespace MediaBrowser.Common.Extensions
@@ -1,5 +1,3 @@
#nullable enable

using System;
using System.Diagnostics;
using System.Threading;
@@ -1,4 +1,3 @@
#nullable enable
#pragma warning disable CS1591

using System;
@@ -1,5 +1,3 @@
#nullable enable

using System;

namespace MediaBrowser.Common.Extensions
@@ -1,5 +1,3 @@
#nullable enable

using System;
using System.Collections.Generic;

@@ -21,7 +21,7 @@ MIT License
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#nullable enable

#pragma warning disable CS1591
#pragma warning disable CA1034
using System;
@@ -1,5 +1,3 @@
#nullable enable

using System.Collections.Generic;
using System.IO;
using System.Linq;
@@ -1,3 +1,5 @@
#nullable disable

using System;
using System.Collections.Generic;
using System.Reflection;
@@ -9,7 +9,7 @@ namespace MediaBrowser.Common.Json.Converters
/// Convert comma delimited string to array of type.
/// </summary>
/// <typeparam name="T">Type to convert to.</typeparam>
public class JsonCommaDelimitedArrayConverter<T> : JsonConverter<T[]>
public class JsonCommaDelimitedArrayConverter<T> : JsonConverter<T[]?>
{
private readonly TypeConverter _typeConverter;

@@ -22,11 +22,17 @@ public JsonCommaDelimitedArrayConverter()
}

/// <inheritdoc />
public override T[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public override T[]? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return null;
This conversation was marked as resolved by Bond-009

This comment has been minimized.

@crobibero

crobibero May 5, 2021
Member

Suggested change
return null;
return Array.Empty<T>();

This comment has been minimized.

@Bond-009

Bond-009 May 5, 2021
Author Member

Would break the JsonCommaDelimitedArrayTests.Deserialize_String_Null_Success test

This comment has been minimized.

@crobibero

crobibero May 5, 2021
Member

Either the test needs to change, or JsonPipeDelimitedArrayConverter needs to change as it returns Array.Empty when given a null value

This comment has been minimized.

@Bond-009

Bond-009 May 5, 2021
Author Member

Looks like it never hits this block and I don't know why

This comment has been minimized.

@crobibero

crobibero May 6, 2021
Member

Interesting, I just tested it and also found that this block is never hit. Either way, I think that JsonCommaDelimitedArrayConverter and JsonPipeDelimitedArrayConverter should behave the same, whichever way we go in the end

}

if (reader.TokenType == JsonTokenType.String)
{
var stringEntries = reader.GetString().Split(',', StringSplitOptions.RemoveEmptyEntries);
// GetString can't return null here because we already handled it above
var stringEntries = reader.GetString()!.Split(',', StringSplitOptions.RemoveEmptyEntries);
if (stringEntries.Length == 0)
{
return Array.Empty<T>();
@@ -67,7 +73,7 @@ public override T[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSeri
}

/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, T[] value, JsonSerializerOptions options)
public override void Write(Utf8JsonWriter writer, T[]? value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
@@ -19,10 +19,10 @@ public override bool CanConvert(Type typeToConvert)
}

/// <inheritdoc />
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
{
var structType = typeToConvert.GetElementType() ?? typeToConvert.GenericTypeArguments[0];
return (JsonConverter)Activator.CreateInstance(typeof(JsonCommaDelimitedArrayConverter<>).MakeGenericType(structType));
return (JsonConverter?)Activator.CreateInstance(typeof(JsonCommaDelimitedArrayConverter<>).MakeGenericType(structType));
}
}
}
@@ -18,10 +18,10 @@ public override bool CanConvert(Type typeToConvert)
}

/// <inheritdoc />
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
{
var structType = typeToConvert.GenericTypeArguments[0];
return (JsonConverter)Activator.CreateInstance(typeof(JsonNullableStructConverter<>).MakeGenericType(structType));
return (JsonConverter?)Activator.CreateInstance(typeof(JsonNullableStructConverter<>).MakeGenericType(structType));
}
}
}
}
@@ -7,27 +7,33 @@ namespace MediaBrowser.Common.Json.Converters
/// <summary>
/// Converts a string <c>N/A</c> to <c>string.Empty</c>.
/// </summary>
public class JsonOmdbNotAvailableStringConverter : JsonConverter<string>
public class JsonOmdbNotAvailableStringConverter : JsonConverter<string?>
{
/// <inheritdoc />
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}

if (reader.TokenType == JsonTokenType.String)
{
var str = reader.GetString();
if (str != null && str.Equals("N/A", StringComparison.OrdinalIgnoreCase))
// GetString can't return null here because we already handled it above
var str = reader.GetString()!;
if (str.Equals("N/A", StringComparison.OrdinalIgnoreCase))
{
return null;
}

return str;
}

return JsonSerializer.Deserialize<string>(ref reader, options);
return JsonSerializer.Deserialize<string?>(ref reader, options);
}

/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
public override void Write(Utf8JsonWriter writer, string? value, JsonSerializerOptions options)
{
writer.WriteStringValue(value);
}
@@ -24,10 +24,16 @@ public JsonPipeDelimitedArrayConverter()
/// <inheritdoc />
public override T[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return Array.Empty<T>();
}

if (reader.TokenType == JsonTokenType.String)
{
var stringEntries = reader.GetString()?.Split('|', StringSplitOptions.RemoveEmptyEntries);
if (stringEntries == null || stringEntries.Length == 0)
// GetString can't return null here because we already handled it above
var stringEntries = reader.GetString()!.Split('|', StringSplitOptions.RemoveEmptyEntries);
if (stringEntries.Length == 0)
{
return Array.Empty<T>();
}
@@ -63,7 +69,8 @@ public override T[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSeri
return typedValues;
}

return JsonSerializer.Deserialize<T[]>(ref reader, options);
// can't return null here because we already handled it above
return JsonSerializer.Deserialize<T[]>(ref reader, options)!;
}

/// <inheritdoc />
@@ -19,10 +19,10 @@ public override bool CanConvert(Type typeToConvert)
}

/// <inheritdoc />
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
{
var structType = typeToConvert.GetElementType() ?? typeToConvert.GenericTypeArguments[0];
return (JsonConverter)Activator.CreateInstance(typeof(JsonPipeDelimitedArrayConverter<>).MakeGenericType(structType));
return (JsonConverter?)Activator.CreateInstance(typeof(JsonPipeDelimitedArrayConverter<>).MakeGenericType(structType));
}
}
}
@@ -9,10 +9,10 @@ namespace MediaBrowser.Common.Json.Converters
/// <summary>
/// Converter to allow the serializer to read strings.
/// </summary>
public class JsonStringConverter : JsonConverter<string>
public class JsonStringConverter : JsonConverter<string?>
{
/// <inheritdoc />
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return reader.TokenType switch
{
@@ -23,7 +23,7 @@ public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS
}

/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
public override void Write(Utf8JsonWriter writer, string? value, JsonSerializerOptions options)
{
writer.WriteStringValue(value);
}
@@ -36,4 +36,4 @@ private static string GetRawValue(Utf8JsonReader reader)
return Encoding.UTF8.GetString(utf8Bytes);
}
}
}
}
@@ -14,7 +14,7 @@ public class JsonVersionConverter : JsonConverter<Version>
{
/// <inheritdoc />
public override Version Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> new Version(reader.GetString());
=> new Version(reader.GetString()!); // Will throw ArgumentNullException on null

/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, Version value, JsonSerializerOptions options)
@@ -33,6 +33,7 @@
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Nullable>enable</Nullable>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<CodeAnalysisRuleSet>../jellyfin.ruleset</CodeAnalysisRuleSet>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
@@ -1,4 +1,3 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
@@ -1,4 +1,3 @@
#nullable enable
using System;
using System.Diagnostics;
using System.Linq;
@@ -1,4 +1,3 @@
#nullable enable
using System;
using System.Net;
using System.Net.Sockets;
@@ -1,4 +1,3 @@
#nullable enable
using System;
using System.Net;
using System.Net.Sockets;
@@ -1,3 +1,5 @@
#nullable disable

using System;
using System.IO;
using System.Reflection;
@@ -1,4 +1,6 @@
#nullable disable
#pragma warning disable SA1649 // File name should match first type name

using System;
using System.IO;
using System.Runtime.InteropServices;
@@ -1,3 +1,5 @@
#nullable disable

using System;
using MediaBrowser.Model.Plugins;

@@ -1,5 +1,3 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.Reflection;
@@ -1,4 +1,3 @@
#nullable enable
using System;
using System.Collections.Generic;
using MediaBrowser.Model.Plugins;
@@ -1,5 +1,3 @@
#nullable enable

using System;
using System.Text.Json.Serialization;
using MediaBrowser.Model.Plugins;
ProTip! Use n and p to navigate between commits in a pull request.