Skip to content

Commit

Permalink
Update Newtonsoft.Json to 13.0.3, adjust code for nullability
Browse files Browse the repository at this point in the history
  • Loading branch information
exyi committed Dec 10, 2023
1 parent 13f3cf4 commit bc5bf8d
Show file tree
Hide file tree
Showing 45 changed files with 182 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageReference Include="Microsoft.Net.Http" Version="2.2.29" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Swashbuckle.Core" Version="5.6.0" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/AutoUI/Core/Controls/BootstrapForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static readonly DotvvmProperty FormCheckInputCssClassProperty
/// </summary>
public bool WrapControlInDiv
{
get { return (bool)GetValue(WrapControlInDivProperty); }
get { return (bool)GetValue(WrapControlInDivProperty)!; }
set { SetValue(WrapControlInDivProperty, value); }
}
public static readonly DotvvmProperty WrapControlInDivProperty
Expand Down
2 changes: 1 addition & 1 deletion src/Framework/Core/DotVVM.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<EmbeddedResource Include="compiler\resources\**\*" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' ">
<PackageReference Include="System.ComponentModel.Annotations" Version="4.3.0" />
Expand Down
1 change: 0 additions & 1 deletion src/Framework/Core/Storage/ReturnedFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ public ReturnedFile(Stream stream, ReturnedFileMetadata metadata)
Stream = stream;
Metadata = metadata;
}

}
}
2 changes: 1 addition & 1 deletion src/Framework/Framework/Binding/BindingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public static T GetCommandDelegate<T>(this ICommandBinding<T> binding, DotvvmBin
/// <summary>
/// Finds expected DataContext, gets the delegate from command binding and evaluates it with `args`
/// </summary>
public static object? Evaluate(this ICommandBinding binding, DotvvmBindableObject control, params Func<Type, object>[] args)
public static object? Evaluate(this ICommandBinding binding, DotvvmBindableObject control, params Func<Type, object?>[] args)
{
var action = binding.GetCommandDelegate(control);
if (action is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ internal class BindingDebugJsonConverter: JsonConverter
{
public override bool CanConvert(Type objectType) =>
typeof(IBinding).IsAssignableFrom(objectType);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) =>
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) =>
throw new NotImplementedException("Deserializing dotvvm bindings from JSON is not supported.");
public override void WriteJson(JsonWriter w, object valueObj, JsonSerializer serializer)
public override void WriteJson(JsonWriter w, object? valueObj, JsonSerializer serializer)
{
var obj = valueObj;
w.WriteValue(obj.ToString());
w.WriteValue(obj?.ToString());

// w.WriteStartObject();
// w.WritePropertyName("ToString");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,7 @@ public Expression CallMethod(Type target, BindingFlags flags, string name, Type[
if (b is null) throw new ArgumentNullException(nameof(b));
if (operatorName is null) throw new ArgumentNullException(nameof(b));

var searchTypes = new [] { a.Type, b.Type, a.Type.UnwrapNullableType(), b.Type.UnwrapNullableType() }.OfType<Type>().Distinct().ToArray();

var searchTypes = new [] { a.Type, b.Type, a.Type.UnwrapNullableType(), b.Type.UnwrapNullableType() }.WhereNotNull().Distinct().ToArray();

// https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/expressions.md#1145-binary-operator-overload-resolution
// The set of candidate user-defined operators provided by X and Y for the operation operator «op»(x, y) is determined. The set consists of the union of the candidate operators provided by X and the candidate operators provided by Y, each determined using the rules of §11.4.6.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static JToken SerializePlan(StaticCommandInvocationPlan plan)
}
else if (arg.Type == StaticCommandParameterType.Constant)
{
array.Add(JToken.FromObject(arg.Arg));
array.Add(arg.Arg is null ? JValue.CreateNull() : JToken.FromObject(arg.Arg));
}
else if (arg.Type == StaticCommandParameterType.DefaultValue)
{
Expand All @@ -54,7 +54,7 @@ public static JToken SerializePlan(StaticCommandInvocationPlan plan)
}
else throw new NotSupportedException(arg.Type.ToString());
}
while (array.Last.Type == JTokenType.Null)
while (array.Last!.Type == JTokenType.Null)
array.Last.Remove();
return array;
}
Expand Down Expand Up @@ -84,19 +84,21 @@ public static string[] GetEncryptionPurposes()
public static StaticCommandInvocationPlan DeserializePlan(JToken planInJson)
{
var jarray = (JArray)planInJson;
var typeName = jarray[0].Value<string>();
var methodName = jarray[1].Value<string>();
var genericTypeNames = jarray[2].Value<JArray>();
var parametersCount = jarray[3].Value<int>();
var parameterTypeNames = jarray[4].Value<JArray>();
if (jarray.Count < 6)
throw new NotSupportedException("Invalid static command plan.");
var typeName = jarray[0]!.Value<string>()!;
var methodName = jarray[1]!.Value<string>()!;
var genericTypeNames = jarray[2]!.Value<JArray>();
var parametersCount = jarray[3]!.Value<int>();
var parameterTypeNames = jarray[4]!.Value<JArray>();
var hasOtherOverloads = parameterTypeNames != null;
var argTypes = jarray[5].ToObject<byte[]>().Select(a => (StaticCommandParameterType)a).ToArray();
var argTypes = jarray[5]!.ToObject<byte[]>()!.Select(a => (StaticCommandParameterType)a).ToArray();

MethodInfo? method;
if (hasOtherOverloads)
{
// There are multiple overloads available, therefore exact parameters need to be resolved first
var parameters = parameterTypeNames!.Select(n => Type.GetType(n.Value<string>()).NotNull()).ToArray();
var parameters = parameterTypeNames!.Select(n => Type.GetType(n.Value<string>()!).NotNull()).ToArray();
method = Type.GetType(typeName)?.GetMethod(methodName, parameters);
}
else
Expand All @@ -110,7 +112,7 @@ public static StaticCommandInvocationPlan DeserializePlan(JToken planInJson)

if (method.IsGenericMethod)
{
var generics = genericTypeNames.Select(n => Type.GetType(n.Value<string>()).NotNull()).ToArray();
var generics = genericTypeNames.NotNull().Select(n => Type.GetType(n.Value<string>()!).NotNull()).ToArray();
method = method.MakeGenericMethod(generics);
}

Expand All @@ -123,11 +125,11 @@ public static StaticCommandInvocationPlan DeserializePlan(JToken planInJson)
case StaticCommandParameterType.Argument:
case StaticCommandParameterType.Inject:
if (a.arg.Type == JTokenType.Null)
return new StaticCommandParameterPlan(a.type, a.parameter?.ParameterType ?? method.DeclaringType);
return new StaticCommandParameterPlan(a.type, a.parameter?.ParameterType ?? method.DeclaringType.NotNull());
else
return new StaticCommandParameterPlan(a.type, a.arg.Value<string>().Apply(Type.GetType));
return new StaticCommandParameterPlan(a.type, a.arg.Value<string>()!.Apply(Type.GetType));
case StaticCommandParameterType.Constant:
return new StaticCommandParameterPlan(a.type, a.arg.ToObject(a.parameter?.ParameterType ?? method.DeclaringType));
return new StaticCommandParameterPlan(a.type, a.arg.ToObject(a.parameter?.ParameterType ?? method.DeclaringType.NotNull()));
case StaticCommandParameterType.DefaultValue:
return new StaticCommandParameterPlan(a.type, a.parameter?.DefaultValue);
case StaticCommandParameterType.Invocation:
Expand Down
6 changes: 3 additions & 3 deletions src/Framework/Framework/Configuration/HtmlTagAttributePair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public override bool Equals(object? obj)

public class HtmlTagAttributePairToStringConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
writer.WriteValue(value.ToString());
writer.WriteValue(value?.ToString());
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
var value = reader.ReadAsString();
if (value == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ internal class DotvvmControlDebugJsonConverter : JsonConverter

public override bool CanConvert(Type objectType) =>
typeof(DotvvmBindableObject).IsAssignableFrom(objectType);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) =>
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) =>
throw new NotImplementedException("Deserializing dotvvm control from JSON is not supported.");
public override void WriteJson(JsonWriter w, object valueObj, JsonSerializer serializer)
public override void WriteJson(JsonWriter w, object? valueObj, JsonSerializer serializer)
{
if (valueObj is null)
{
w.WriteNull();
return;
}
var obj = (DotvvmBindableObject)valueObj;
w.WriteStartObject();

Expand Down
2 changes: 1 addition & 1 deletion src/Framework/Framework/Diagnostics/JsonSizeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ AtomicSizeProfile analyzeToken(JToken token)
return r;
}
case JTokenType.String:
return new (((string)token).Length + 2);
return new ((((string?)token)?.Length ?? 4) + 2);
case JTokenType.Integer:
// This should be the same as token.ToString().Length, but I didn't want to allocate the string unnecesarily
return new((int)Math.Log10(Math.Abs((long)token) + 1) + 1);
Expand Down
4 changes: 2 additions & 2 deletions src/Framework/Framework/DotVVM.Framework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
<ItemGroup>
<PackageReference Include="Ben.Demystifier" Version="0.4.1" />
<PackageReference Include="FastExpressionCompiler" Version="3.3.3" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.2" />
<PackageReference Include="RecordException" Version="0.1.2" />
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
<PackageReference Include="System.Security.Claims" Version="4.3.0" />
Expand Down
3 changes: 2 additions & 1 deletion src/Framework/Framework/Hosting/AssemblySerializableList.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DotVVM.Framework.Compilation;
using DotVVM.Framework.Utils;
using Microsoft.Extensions.DependencyModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
Expand Down Expand Up @@ -28,7 +29,7 @@ public static AssemblySerializableList CreateFromCache(CompiledAssemblyCache cac
if (mainAssembly is {})
assemblies.Add(mainAssembly);
return new AssemblySerializableList(
assemblyDirs: assemblies.Select(a => IO.Path.GetDirectoryName(a.Location)).OfType<string>().Distinct().OrderBy(x => x).ToArray(),
assemblyDirs: assemblies.Select(a => IO.Path.GetDirectoryName(a.Location)).WhereNotNull().Distinct().OrderBy(x => x).ToArray(),
assemblyNames: assemblies.Select(getName).OrderBy(x => x).ToArray()!,
mainAssembly: getName(mainAssembly)
);
Expand Down
10 changes: 5 additions & 5 deletions src/Framework/Framework/Hosting/DotvvmPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,18 +350,18 @@ public async Task ProcessStaticCommandRequest(IDotvvmRequestContext context)
}

// validate csrf token
context.CsrfToken = postData["$csrfToken"].Value<string>();
context.CsrfToken = (postData["$csrfToken"]?.Value<string>()).NotNull("$csrfToken is required");
CsrfProtector.VerifyToken(context, context.CsrfToken);

var knownTypes = postData["knownTypeMetadata"].Values<string>().ToArray();
var knownTypes = postData["knownTypeMetadata"]?.Values<string>().WhereNotNull().ToArray() ?? Array.Empty<string>();
var argumentPaths = postData["argumentPaths"]?.Values<string?>().ToArray();
var command = postData["command"].Value<string>();
var arguments = postData["args"] as JArray;
var command = (postData["command"]?.Value<string>()).NotNull("command is required");
var arguments = (postData["args"] as JArray).NotNull("args is required");
var executionPlan = StaticCommandExecutor.DecryptPlan(command);

var actionInfo = new ActionInfo(
binding: null,
() => { return StaticCommandExecutor.Execute(executionPlan, arguments.NotNull(), argumentPaths, context); },
() => { return StaticCommandExecutor.Execute(executionPlan, arguments, argumentPaths, context); },
false,
executionPlan.Method,
argumentPaths
Expand Down
16 changes: 13 additions & 3 deletions src/Framework/Framework/Hosting/ErrorPages/ErrorPageTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ public string TransformText()

public void ObjectBrowser(object? obj)
{
if (obj is null)
{
WriteText("null");
return;
}

var settings = new JsonSerializerSettings() {
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore,
Expand Down Expand Up @@ -227,7 +233,11 @@ public void ObjectBrowser(JObject obj)
Write("<div class='prop'><span class='propname'>");
WriteText(p.Key);
Write("</span>:");
if (p.Value is JObject)
if (p.Value is null)
{
WriteText("null");
}
else if (p.Value is JObject)
{
ObjectBrowser((JObject)p.Value);
}
Expand Down Expand Up @@ -360,9 +370,9 @@ class IgnoreStuffJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType) =>
objectType.IsDelegate();
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) =>
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) =>
throw new NotImplementedException();
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
writer.WriteValue("<delegate>");
}
Expand Down
2 changes: 1 addition & 1 deletion src/Framework/Framework/Hosting/ModelState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ModelState
/// <summary>
/// Gets the validation target path relative to the command target.
/// </summary>
internal string ValidationTargetPath { get; set; }
internal string? ValidationTargetPath { get; set; }

/// <summary>
/// Gets the object that was validated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ private static JObject CreateNumberInfoJson(NumberFormatInfo ni)
var jobj = JObject.FromObject(numberFormat);
jobj[","] = ni.NumberGroupSeparator;
jobj["."] = ni.NumberDecimalSeparator;
jobj["percent"][","] = ni.PercentGroupSeparator;
jobj["percent"]["."] = ni.PercentDecimalSeparator;
jobj["percent"]![","] = ni.PercentGroupSeparator;
jobj["percent"]!["."] = ni.PercentDecimalSeparator;

jobj["currency"][","] = ni.CurrencyGroupSeparator;
jobj["currency"]["."] = ni.CurrencyDecimalSeparator;
jobj["currency"]![","] = ni.CurrencyGroupSeparator;
jobj["currency"]!["."] = ni.CurrencyDecimalSeparator;
return jobj;
}

Expand Down
Loading

0 comments on commit bc5bf8d

Please sign in to comment.