Skip to content

Commit

Permalink
fix #2917 add attribute and flag api controller that should still use…
Browse files Browse the repository at this point in the history
… NewtonsoftJson
  • Loading branch information
tvatavuk committed Nov 8, 2022
1 parent d0d70c5 commit f454dd5
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Src/Dnn/ToSic.Sxc.Dnn.WebApi/Custom/Dnn/Api12.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ToSic.Eav.Documentation;
using ToSic.Eav.WebApi.Helpers;
using ToSic.Sxc.Dnn.Code;
using ToSic.Sxc.Dnn.Run;
using ToSic.Sxc.Dnn.WebApi;
Expand All @@ -13,6 +14,7 @@ namespace Custom.Dnn
/// </summary>
[PublicApi_Stable_ForUseInYourCode("This is the official base class for v12+")]
[DnnLogExceptions]
[NewtonsoftJsonResponse]
public abstract class Api12 : Hybrid.Api12, IDnnDynamicWebApi, IDnnDynamicCodeAdditions
{
protected Api12() : base("Dnn12") { }
Expand Down
2 changes: 2 additions & 0 deletions Src/Dnn/ToSic.Sxc.Dnn.WebApi/Custom/Hybrid/Api12.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ToSic.Eav.Documentation;
using ToSic.Eav.WebApi.Helpers;
using ToSic.Sxc.Code;
using ToSic.Sxc.Dnn.WebApi.Logging;
using ToSic.Sxc.WebApi;
Expand All @@ -12,6 +13,7 @@ namespace Custom.Hybrid
/// </summary>
[PublicApi("This is the official base class for v12+")]
[DnnLogExceptions]
[NewtonsoftJsonResponse]
public abstract partial class Api12: ApiCoreShim, IDynamicCode12, IDynamicWebApi, IHasDynamicCodeRoot
{
protected Api12() : base("Hyb12") { }
Expand Down
3 changes: 3 additions & 0 deletions Src/Dnn/ToSic.Sxc.Dnn.WebApi/Custom/Hybrid/Api14.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Custom.Hybrid.Advanced;
using System.Net.Http.Formatting;
using ToSic.Eav.Documentation;
using ToSic.Eav.WebApi.Helpers;
using ToSic.Sxc.Code;
using ToSic.Sxc.Dnn.WebApi.Logging;
using ToSic.Sxc.Services;
Expand All @@ -18,6 +20,7 @@ namespace Custom.Hybrid
/// </remarks>
[PublicApi]
[DnnLogExceptions]
[NewtonsoftJsonResponse]
public abstract class Api14: Api14<dynamic, ServiceKit14>, IDynamicCode12, IDynamicWebApi, IHasDynamicCodeRoot
{
protected Api14() : base("Hyb12") { }
Expand Down
16 changes: 16 additions & 0 deletions Src/Dnn/ToSic.Sxc.Dnn.WebApi/JsonFormatters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using ToSic.Eav.Serialization;

// ReSharper disable once CheckNamespace
namespace System.Net.Http.Formatting
{
public class JsonFormatters
{
public static SystemTextJsonMediaTypeFormatter SystemTextJsonMediaTypeFormatter =>
_systemTextJsonMediaTypeFormatter ?? (_systemTextJsonMediaTypeFormatter =
new SystemTextJsonMediaTypeFormatter
{
JsonSerializerOptions = JsonOptions.UnsafeJsonWithoutEncodingHtml
});
private static SystemTextJsonMediaTypeFormatter _systemTextJsonMediaTypeFormatter;
}
}
1 change: 1 addition & 0 deletions Src/Dnn/ToSic.Sxc.Dnn.WebApi/ToSic.Sxc.Dnn.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<Compile Include="Custom\Dnn\Api12.cs" />
<Compile Include="Custom\Hybrid\Api14.cs" />
<Compile Include="Dnn\WebApi\Admin\AppInternalsController.cs" />
<Compile Include="JsonFormatters.cs" />
<Compile Include="System.Text.Json.Formatter.cs" />
<Compile Include="WebApi\ApiCoreShim.cs" />
<Compile Include="Custom\Hybrid\Api12_Api.cs" />
Expand Down
2 changes: 2 additions & 0 deletions Src/Dnn/ToSic.Sxc.Dnn.WebApi/WebApi/SxcApiControllerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ToSic.Eav.Documentation;
using ToSic.Eav.Logging;
using ToSic.Eav.Plumbing;
using ToSic.Eav.WebApi.Helpers;
using ToSic.Sxc.Blocks;
using ToSic.Sxc.Context;
using ToSic.Sxc.Dnn.WebApi;
Expand All @@ -16,6 +17,7 @@ namespace ToSic.Sxc.WebApi
/// </summary>
[DnnLogExceptions]
[PrivateApi("This was only ever used as an internal base class, so it can be modified as needed - just make sure the derived types don't break")]
[NewtonsoftJsonResponse]
public abstract class SxcApiControllerBase<TRealController>: DnnApiControllerWithFixes<TRealController> where TRealController : class, IHasLog<TRealController>
{
protected SxcApiControllerBase(string logSuffix) : base(logSuffix) { }
Expand Down
13 changes: 4 additions & 9 deletions Src/Dnn/ToSic.Sxc.Dnn/StartUp/StartupDnn.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DotNetNuke.Web.Api;
using Newtonsoft.Json;
using System.Configuration;
using System.Net.Http.Formatting;
using System.Web.Hosting;
Expand Down Expand Up @@ -40,8 +41,10 @@ public void Configure()

// Configure Newtonsoft Time zone handling
// Moved here in v12.05 - previously it was in the Pre-Serialization converter
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;

// System.Text.Json supports ISO 8601-1:2019, including the RFC 3339 profile
GlobalConfiguration.Configuration.Formatters.Add(SystemTextJsonMediaTypeFormatter);
GlobalConfiguration.Configuration.Formatters.Add(JsonFormatters.SystemTextJsonMediaTypeFormatter);
// Getting the service provider in Configure is tricky business, because
// of .net core 2.1 bugs
// ATM it appears that the service provider will get destroyed after startup, so we MUST get an additional one to use here
Expand Down Expand Up @@ -69,13 +72,5 @@ public void Configure()

_alreadyConfigured = true;
}

public static SystemTextJsonMediaTypeFormatter SystemTextJsonMediaTypeFormatter =>
_systemTextJsonMediaTypeFormatter ?? (_systemTextJsonMediaTypeFormatter =
new SystemTextJsonMediaTypeFormatter
{
JsonSerializerOptions = JsonOptions.UnsafeJsonWithoutEncodingHtml
});
private static SystemTextJsonMediaTypeFormatter _systemTextJsonMediaTypeFormatter;
}
}

0 comments on commit f454dd5

Please sign in to comment.