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

OSOE-795: Upgrade to latest OC preview to test System.Text.Json #159

Merged
merged 39 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
6e60e2e
Update NuGet versions to pre-release.
sarahelsaig Feb 22, 2024
8d251dc
Merge remote-tracking branch 'origin/dev' into issue/OSOE-795
sarahelsaig Feb 22, 2024
fc8bb6b
Remove "using Newtonsoft.Json"
sarahelsaig Feb 24, 2024
0e0d7c6
Use naming strategy.
sarahelsaig Feb 24, 2024
83afa77
Fix compilation errors.
sarahelsaig Feb 24, 2024
23f1f79
Replace JsonConvert.SerializeObject.
sarahelsaig Feb 24, 2024
dd79c82
Clarify SuppressMessage Justification.
sarahelsaig Feb 25, 2024
9f271ff
Fix OrderByColumn.
sarahelsaig Feb 25, 2024
f260ace
Simplify OrderByColumn.Selector.
sarahelsaig Feb 25, 2024
865e77f
Clean up unit test.
sarahelsaig Feb 25, 2024
b5c4d19
Make result mismatches clearer by only comparing the final matrix.
sarahelsaig Feb 25, 2024
3b7ff13
Sort numbers numerically.
sarahelsaig Feb 25, 2024
5d45583
More cleanup.
sarahelsaig Feb 25, 2024
53445f5
Fix newly introduced mistake.
sarahelsaig Feb 25, 2024
bcb0ebd
Add HasTypeProperty extension method.
sarahelsaig Feb 25, 2024
371f76e
Fix broken DateTime conversion during tests and broken mixed type num…
sarahelsaig Feb 25, 2024
0109937
Fix warnings
sarahelsaig Feb 25, 2024
7e6019c
unusing
sarahelsaig Feb 25, 2024
b8f1cf6
Fix data table request serialization.
sarahelsaig Feb 26, 2024
9126e1f
Fix JsonExtensionData related problems.
sarahelsaig Feb 26, 2024
2bdcdc5
Code cleanup in Lombiq.DataTable.cshtml
sarahelsaig Feb 26, 2024
9a7ce3a
View bug fix.
sarahelsaig Feb 26, 2024
3ca9a63
Unusing.
sarahelsaig Feb 26, 2024
1c1f758
Code cleanup.
sarahelsaig Feb 26, 2024
c6006d4
Add missing using.
sarahelsaig Feb 26, 2024
b8967e1
Update OC to latest (because of bug fix for WorkflowTypeStep)
sarahelsaig Mar 3, 2024
3a53d30
Update OC preview version.
sarahelsaig Mar 7, 2024
5f58178
Update OC package
sarahelsaig Mar 13, 2024
06a84d2
Replace AdminRouteAttribute with AdminAttribute.
sarahelsaig Mar 18, 2024
da4fa13
Use JObject.FromObject instead of JsonSerializer.SerializeToNode wher…
sarahelsaig Apr 2, 2024
013408f
Update OC preview version.
sarahelsaig Apr 23, 2024
ecbad33
Update OC package version.
sarahelsaig Apr 27, 2024
9c42051
Update OC versions
sarahelsaig May 4, 2024
6f68666
Update OC to the latest preview.
sarahelsaig May 7, 2024
5a47856
Update Lombiq.DataTables/Models/VueModel.cs
sarahelsaig May 12, 2024
18aef9f
Update Lombiq.DataTables/Tests/Lombiq.DataTables.Tests/MockDataProvid…
sarahelsaig May 12, 2024
0d0ec50
Update Lombiq.DataTables/Extensions/JsonNodeExtensions.cs
sarahelsaig May 12, 2024
80e1773
Rename HasTypeProperty to HasMatchingTypeProperty
sarahelsaig May 12, 2024
9384469
Get rid of `[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonK…
sarahelsaig May 12, 2024
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: 2 additions & 2 deletions Lombiq.DataTables.Samples/Lombiq.DataTables.Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OrchardCore.ContentFields" Version="1.8.2" />
<PackageReference Include="OrchardCore.Module.Targets" Version="1.8.2" />
<PackageReference Include="OrchardCore.ContentFields" Version="2.0.0-preview-18200" />
<PackageReference Include="OrchardCore.Module.Targets" Version="2.0.0-preview-18200" />
</ItemGroup>

<ItemGroup>
Expand Down
12 changes: 5 additions & 7 deletions Lombiq.DataTables/Controllers/Api/RowsController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Lombiq.DataTables.Models;
using Lombiq.DataTables.Services;
using Lombiq.HelpfulLibraries.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -32,7 +32,7 @@ public RowsController(
/// <summary>
/// Gets the current table view's rows.
/// </summary>
/// <param name="requestJson">The request to fulfill serialized as JSON.</param>
/// <param name="request">The request to fulfill serialized as JSON.</param>
/// <returns>The response for this API call.</returns>
/// <remarks>
/// <list type="bullet">
Expand All @@ -53,11 +53,10 @@ public RowsController(
/// </remarks>
[IgnoreAntiforgeryToken]
[HttpGet]
public async Task<ActionResult<DataTableDataResponse>> Get(string requestJson)
public async Task<ActionResult<DataTableDataResponse>> Get([FromJsonQueryString(Name = "requestJson")] DataTableDataRequest request)
{
if (string.IsNullOrEmpty(requestJson)) return BadRequest();
if (request == null) return BadRequest();

var request = JsonConvert.DeserializeObject<DataTableDataRequest>(requestJson);
var dataProvider = _dataTableDataProviderAccessor.GetDataProvider(request.DataProvider);
if (dataProvider == null)
{
Expand Down Expand Up @@ -86,11 +85,10 @@ public async Task<ActionResult<DataTableDataResponse>> Get(string requestJson)
}

public async Task<ActionResult<DataTableDataResponse>> Export(
string requestJson,
[FromJsonQueryString(Name = "requestJson")] DataTableDataRequest request,
string name = null,
bool exportAll = true)
{
var request = JsonConvert.DeserializeObject<DataTableDataRequest>(requestJson);
if (exportAll)
{
request.Start = 0;
Expand Down
9 changes: 5 additions & 4 deletions Lombiq.DataTables/Controllers/TableController.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using Lombiq.DataTables.Services;
using Lombiq.DataTables.ViewModels;
using Lombiq.HelpfulLibraries.OrchardCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using OrchardCore.Admin;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using System.Threading.Tasks;

namespace Lombiq.DataTables.Controllers;
Expand All @@ -18,12 +17,14 @@ public class TableController : Controller
public TableController(IEnumerable<IDataTableDataProvider> dataTableDataProviders) =>
_dataTableDataProviders = dataTableDataProviders;

[AdminRoute("DataTable/{providerName}/{queryId?}")]
[Admin("DataTable/{providerName}/{queryId?}")]
public async Task<IActionResult> Get(string providerName, string queryId = null, bool paging = true, bool viewAction = false)
{
var provider = _dataTableDataProviders.Single(provider => provider.Name == providerName);
if (string.IsNullOrEmpty(queryId)) queryId = providerName;
var definition = new DataTableDefinitionViewModel(JObject.FromObject(new { paging, viewAction }))

var additionalDatatableOptions = JObject.FromObject(new { paging, viewAction })!.AsObject();
var definition = new DataTableDefinitionViewModel(additionalDatatableOptions)
{
DataProvider = providerName,
QueryId = queryId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Localization;
using Newtonsoft.Json;
using OrchardCore.Mvc.Core.Utilities;
using OrchardCore.Security.Permissions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text.Json;
using System.Threading.Tasks;
using static Lombiq.DataTables.Constants.SortingDirection;

Expand Down Expand Up @@ -167,7 +167,7 @@ public static string GetCustomActionsJson(
IHttpContextAccessor hca,
LinkGenerator linkGenerator,
IStringLocalizer<ActionsDescriptor> actionsStringLocalizer) =>
JsonConvert.SerializeObject(GetCustomActions(
JsonSerializer.Serialize(GetCustomActions(
dataProvider,
contentItemId,
canDelete,
Expand Down
11 changes: 11 additions & 0 deletions Lombiq.DataTables/Extensions/JsonNodeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace System.Text.Json.Nodes;

public static class JsonNodeExtensions
{
/// <summary>
/// Checks if the provided <paramref name="node"/> is object and has a <c>Type</c> property, and if its value
/// matches <typeparamref name="T"/>.
/// </summary>
public static bool HasMatchingTypeProperty<T>(this JsonNode node) =>
node is JsonObject jsonObject && jsonObject["Type"]?.ToString() == typeof(T).Name;
}
4 changes: 2 additions & 2 deletions Lombiq.DataTables/Liquid/ActionsLiquidFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Localization;
using Newtonsoft.Json.Linq;
using OrchardCore.ContentManagement;
using OrchardCore.DisplayManagement;
using OrchardCore.Liquid;
using System;
using System.IO;
using System.Text.Encodings.Web;
using System.Text.Json.Nodes;
using System.Threading.Tasks;

namespace Lombiq.DataTables.Liquid;
Expand Down Expand Up @@ -71,7 +71,7 @@ public ValueTask<FluidValue> ProcessAsync(FluidValue input, FilterArguments argu
FluidValues.Object => input!.ToObjectValue() switch
{
ActionsDescriptor model => FromObjectAsync(model, title, returnUrl),
JToken jToken => FromObjectAsync(jToken.ToObject<ActionsDescriptor>(), title, returnUrl),
JsonNode jsonNode => FromObjectAsync(jsonNode.ToObject<ActionsDescriptor>(), title, returnUrl),
{ } unknown => throw GetException(unknown),
_ => throw new ArgumentNullException(nameof(input)),
},
Expand Down
7 changes: 3 additions & 4 deletions Lombiq.DataTables/Lombiq.DataTables.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@

<ItemGroup>
<PackageReference Include="ClosedXML" Version="0.102.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Nito.AsyncEx.Tasks" Version="5.1.2" />
<PackageReference Include="OrchardCore.Contents" Version="1.8.2" />
<PackageReference Include="OrchardCore.Module.Targets" Version="1.8.2" />
<PackageReference Include="OrchardCore.Search.Lucene" Version="1.8.2" />
<PackageReference Include="OrchardCore.Contents" Version="2.0.0-preview-18200" />
<PackageReference Include="OrchardCore.Module.Targets" Version="2.0.0-preview-18200" />
<PackageReference Include="OrchardCore.Search.Lucene" Version="2.0.0-preview-18200" />
</ItemGroup>

<ItemGroup Condition="'$(NuGetBuild)' != 'true'">
Expand Down
7 changes: 4 additions & 3 deletions Lombiq.DataTables/Models/DataTableChildRowResponse.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Text.Json.Serialization;

namespace Lombiq.DataTables.Models;

[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class DataTableChildRowResponse
{
[JsonPropertyName("error")]
public string Error { get; set; }

[JsonPropertyName("content")]
public string Content { get; set; }

public static DataTableChildRowResponse ErrorResult(string errorText) => new() { Error = errorText };
Expand Down
13 changes: 10 additions & 3 deletions Lombiq.DataTables/Models/DataTableColumn.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Text.Json.Serialization;

namespace Lombiq.DataTables.Models;

[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class DataTableColumn
{
[JsonPropertyName("data")]
public string Data { get; set; }

[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("searchable")]
public bool Searchable { get; set; }

[JsonPropertyName("orderable")]
public bool Orderable { get; set; }

[JsonPropertyName("search")]
public DataTableSearchParameters Search { get; set; }
}
3 changes: 0 additions & 3 deletions Lombiq.DataTables/Models/DataTableDataRequest.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Collections.Generic;
using System.Linq;

namespace Lombiq.DataTables.Models;

[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class DataTableDataRequest
{
public string QueryId { get; set; }
Expand Down
11 changes: 7 additions & 4 deletions Lombiq.DataTables/Models/DataTableDataResponse.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Lombiq.DataTables.Models;

[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class DataTableDataResponse
{
/// <summary>
Expand All @@ -15,28 +13,33 @@ public class DataTableDataResponse
/// <remarks>
/// <para>For internal use only. It's overwritten during normal use.</para>
/// </remarks>
[JsonProperty]
[JsonPropertyName("draw")]
[JsonInclude]
internal int Draw { get; set; }

/// <summary>
/// Gets or sets the extra informational field that shows the actual total if filtering (such as keyword
/// search) is used. When not filtering it must be the same as <see cref="RecordsFiltered"/>.
/// </summary>
[JsonPropertyName("recordsTotal")]
public int RecordsTotal { get; set; }

/// <summary>
/// Gets or sets the total number of results; used for paging.
/// </summary>
[JsonPropertyName("recordsFiltered")]
public int RecordsFiltered { get; set; }

/// <summary>
/// Gets or sets the table contents of the current page.
/// </summary>
[JsonPropertyName("data")]
public IEnumerable<DataTableRow> Data { get; set; }

/// <summary>
/// Gets or sets the user-facing error message in case something went wrong.
/// </summary>
[JsonPropertyName("error")]
public string Error { get; set; }

/// <summary>
Expand Down
16 changes: 13 additions & 3 deletions Lombiq.DataTables/Models/DataTableOrder.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
using Lombiq.DataTables.Constants;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;

namespace Lombiq.DataTables.Models;

[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class DataTableOrder
{
public string Column { get; set; }

[JsonIgnore]
public SortingDirection Direction { get; set; }

[JsonPropertyName("direction")]
[JsonInclude]
[SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "It's used for JSON conversion.")]
private string DirectionString
{
get => IsAscending ? "ascending" : "descending";
set => Direction = value == "descending" ? SortingDirection.Descending : SortingDirection.Ascending;
dministro marked this conversation as resolved.
Show resolved Hide resolved
}

[JsonIgnore]
public bool IsAscending => Direction == SortingDirection.Ascending;
}
36 changes: 26 additions & 10 deletions Lombiq.DataTables/Models/DataTableRow.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,52 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace Lombiq.DataTables.Models;

[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class DataTableRow
{
[JsonExtensionData]
internal IDictionary<string, JToken> ValuesDictionary { get; set; }
[JsonInclude]
[JsonPropertyName("valuesDictionary")]
internal IDictionary<string, object> ValuesDictionary { get; set; } = new Dictionary<string, object>();

[JsonPropertyName("id")]
public int Id { get; set; }

public string this[string name]
{
get => ValuesDictionary.TryGetValue(name, out var value) ? value.Value<string>() : null;
get => ValuesDictionary.GetMaybe(name)?.ToString();
set => ValuesDictionary[name] = value;
}

public DataTableRow() => ValuesDictionary = new Dictionary<string, JToken>();
public DataTableRow() { }

public DataTableRow(int id, IDictionary<string, JToken> valuesDictionary)
public DataTableRow(int id, IDictionary<string, JsonNode> valuesDictionary)
{
Id = id;
ValuesDictionary = valuesDictionary;

if (valuesDictionary != null)
{
foreach (var (key, value) in valuesDictionary)
{
ValuesDictionary[key] = value;
}
}
}

public IEnumerable<string> GetValues() =>
ValuesDictionary.Values.Select(value => value.Value<string>());
ValuesDictionary.Values.Select(value => value.ToString());

public IEnumerable<string> GetValuesOrderedByColumns(IEnumerable<DataTableColumnDefinition> columnDefinitions) =>
columnDefinitions.Select(columnDefinition => this[columnDefinition.Name] ?? string.Empty);

internal JsonNode GetValueAsJsonNode(string name) =>
ValuesDictionary.GetMaybe(name) switch
{
JsonNode node => node,
{ } otherValue => JObject.FromObject(otherValue),
null => null,
};
}
7 changes: 3 additions & 4 deletions Lombiq.DataTables/Models/DataTableSearchParameters.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Text.Json.Serialization;

namespace Lombiq.DataTables.Models;

[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class DataTableSearchParameters
{
[JsonPropertyName("value")]
public string Value { get; set; }

[JsonProperty(PropertyName = "regex")]
[JsonPropertyName("regex")]
public bool IsRegex { get; set; }
}
9 changes: 5 additions & 4 deletions Lombiq.DataTables/Models/ExportDate.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Newtonsoft.Json.Linq;
using NodaTime;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Nodes;

namespace Lombiq.DataTables.Models;

Expand All @@ -21,10 +21,11 @@ public class ExportDate
public int Day { get; set; }
public string ExcelFormat { get; set; }

public static bool IsInstance(JObject jObject) =>
jObject[nameof(Type)]?.ToString() == nameof(ExportDate);
public static bool IsInstance(JsonObject jsonObject) =>
jsonObject.HasMatchingTypeProperty<ExportDate>();

public static string GetText(JObject jObject) => ((LocalDate)jObject.ToObject<ExportDate>()).ToShortDateString();
public static string GetText(JsonObject jsonObject) =>
((LocalDate)jsonObject.ToObject<ExportDate>()).ToShortDateString();

public static implicit operator ExportDate(LocalDate localDate) =>
new()
Expand Down
Loading