-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
V15: Fix webhook RTE serialization (#17656)
* Make base type resolver class * Add new webhook serializer * fix comment * Update src/Umbraco.Infrastructure/Serialization/ContentJsonTypeResolverBase.cs Co-authored-by: Elitsa Marinovska <[email protected]> * Update src/Umbraco.Core/Services/WebhookRequestService.cs --------- Co-authored-by: Elitsa <[email protected]> Co-authored-by: Elitsa Marinovska <[email protected]>
- Loading branch information
1 parent
49330b4
commit 6f08178
Showing
8 changed files
with
115 additions
and
59 deletions.
There are no files selected for viewing
55 changes: 3 additions & 52 deletions
55
src/Umbraco.Cms.Api.Delivery/Json/DeliveryApiJsonTypeResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,7 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Text.Json.Serialization.Metadata; | ||
using Umbraco.Cms.Core.Models.DeliveryApi; | ||
using Umbraco.Cms.Infrastructure.Serialization; | ||
|
||
namespace Umbraco.Cms.Api.Delivery.Json; | ||
|
||
// see https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism?pivots=dotnet-7-0 | ||
public class DeliveryApiJsonTypeResolver : DefaultJsonTypeInfoResolver | ||
{ | ||
public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options) | ||
{ | ||
JsonTypeInfo jsonTypeInfo = base.GetTypeInfo(type, options); | ||
|
||
Type[] derivedTypes = GetDerivedTypes(jsonTypeInfo); | ||
if (derivedTypes.Length > 0) | ||
{ | ||
ConfigureJsonPolymorphismOptions(jsonTypeInfo, derivedTypes); | ||
} | ||
|
||
return jsonTypeInfo; | ||
} | ||
|
||
protected virtual Type[] GetDerivedTypes(JsonTypeInfo jsonTypeInfo) | ||
{ | ||
if (jsonTypeInfo.Type == typeof(IApiContent)) | ||
{ | ||
return new[] { typeof(ApiContent) }; | ||
} | ||
|
||
if (jsonTypeInfo.Type == typeof(IApiContentResponse)) | ||
{ | ||
return new[] { typeof(ApiContentResponse) }; | ||
} | ||
|
||
if (jsonTypeInfo.Type == typeof(IRichTextElement)) | ||
{ | ||
return new[] { typeof(RichTextRootElement), typeof(RichTextGenericElement), typeof(RichTextTextElement) }; | ||
} | ||
|
||
return Array.Empty<Type>(); | ||
} | ||
|
||
protected void ConfigureJsonPolymorphismOptions(JsonTypeInfo jsonTypeInfo, params Type[] derivedTypes) | ||
{ | ||
jsonTypeInfo.PolymorphismOptions = new JsonPolymorphismOptions | ||
{ | ||
UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FailSerialization, | ||
}; | ||
|
||
foreach (Type derivedType in derivedTypes) | ||
{ | ||
jsonTypeInfo.PolymorphismOptions.DerivedTypes.Add(new JsonDerivedType(derivedType)); | ||
} | ||
} | ||
} | ||
public class DeliveryApiJsonTypeResolver : ContentJsonTypeResolverBase | ||
{ } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
namespace Umbraco.Cms.Core.Serialization; | ||
|
||
public interface IWebhookJsonSerializer : IJsonSerializer | ||
{ } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/Umbraco.Infrastructure/Serialization/ContentJsonTypeResolverBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Text.Json.Serialization.Metadata; | ||
using Umbraco.Cms.Core.Models.DeliveryApi; | ||
|
||
namespace Umbraco.Cms.Infrastructure.Serialization; | ||
|
||
public abstract class ContentJsonTypeResolverBase : DefaultJsonTypeInfoResolver | ||
{ | ||
public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options) | ||
{ | ||
JsonTypeInfo jsonTypeInfo = base.GetTypeInfo(type, options); | ||
|
||
Type[] derivedTypes = GetDerivedTypes(jsonTypeInfo); | ||
if (derivedTypes.Length > 0) | ||
{ | ||
ConfigureJsonPolymorphismOptions(jsonTypeInfo, derivedTypes); | ||
} | ||
|
||
return jsonTypeInfo; | ||
} | ||
|
||
public virtual Type[] GetDerivedTypes(JsonTypeInfo jsonTypeInfo) | ||
{ | ||
if (jsonTypeInfo.Type == typeof(IApiContent)) | ||
{ | ||
return new[] { typeof(ApiContent) }; | ||
} | ||
|
||
if (jsonTypeInfo.Type == typeof(IApiContentResponse)) | ||
{ | ||
return new[] { typeof(ApiContentResponse) }; | ||
} | ||
|
||
if (jsonTypeInfo.Type == typeof(IRichTextElement)) | ||
{ | ||
return new[] { typeof(RichTextRootElement), typeof(RichTextGenericElement), typeof(RichTextTextElement) }; | ||
} | ||
|
||
return Array.Empty<Type>(); | ||
} | ||
|
||
public void ConfigureJsonPolymorphismOptions(JsonTypeInfo jsonTypeInfo, params Type[] derivedTypes) | ||
{ | ||
jsonTypeInfo.PolymorphismOptions = new JsonPolymorphismOptions | ||
{ | ||
UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FailSerialization, | ||
}; | ||
|
||
foreach (Type derivedType in derivedTypes) | ||
{ | ||
jsonTypeInfo.PolymorphismOptions.DerivedTypes.Add(new JsonDerivedType(derivedType)); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Umbraco.Infrastructure/Serialization/SystemTextWebhookJsonSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Umbraco.Cms.Core.Serialization; | ||
|
||
namespace Umbraco.Cms.Infrastructure.Serialization; | ||
|
||
/// <inheritdoc /> | ||
public sealed class SystemTextWebhookJsonSerializer : SystemTextJsonSerializerBase, IWebhookJsonSerializer | ||
{ | ||
private readonly JsonSerializerOptions _jsonSerializerOptions; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SystemTextWebhookJsonSerializer" /> class. | ||
/// </summary> | ||
public SystemTextWebhookJsonSerializer() | ||
=> _jsonSerializerOptions = new JsonSerializerOptions() | ||
{ | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
Converters = | ||
{ | ||
new JsonStringEnumConverter(), | ||
new JsonUdiConverter(), | ||
new JsonUdiRangeConverter(), | ||
new JsonObjectConverter(), // Required for block editor values | ||
new JsonBlockValueConverter() | ||
}, | ||
TypeInfoResolver = new WebhookJsonTypeResolver(), | ||
}; | ||
|
||
protected override JsonSerializerOptions JsonSerializerOptions => _jsonSerializerOptions; | ||
} |
4 changes: 4 additions & 0 deletions
4
src/Umbraco.Infrastructure/Serialization/WebhookJsonTypeResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
namespace Umbraco.Cms.Infrastructure.Serialization; | ||
|
||
public class WebhookJsonTypeResolver : ContentJsonTypeResolverBase | ||
{ } |