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

Make the API content response builder extendable #16056

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 20 additions & 7 deletions src/Umbraco.Cms.Api.Delivery/Json/DeliveryApiJsonTypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,36 @@
{
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))
{
ConfigureJsonPolymorphismOptions(jsonTypeInfo, typeof(ApiContent));
return new[] { typeof(ApiContent) };
}
else if (jsonTypeInfo.Type == typeof(IApiContentResponse))

if (jsonTypeInfo.Type == typeof(IApiContentResponse))
{
ConfigureJsonPolymorphismOptions(jsonTypeInfo, typeof(ApiContentResponse));
return new[] { typeof(ApiContentResponse) };
}
else if (jsonTypeInfo.Type == typeof(IRichTextElement))

if (jsonTypeInfo.Type == typeof(IRichTextElement))
{
ConfigureJsonPolymorphismOptions(jsonTypeInfo, typeof(RichTextRootElement), typeof(RichTextGenericElement), typeof(RichTextTextElement));
return new[] { typeof(RichTextRootElement), typeof(RichTextGenericElement), typeof(RichTextTextElement) };
}

return jsonTypeInfo;
return Array.Empty<Type>();

Check notice on line 41 in src/Umbraco.Cms.Api.Delivery/Json/DeliveryApiJsonTypeResolver.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (release/13.3.0)

✅ No longer an issue: Bumpy Road Ahead

GetTypeInfo is no longer above the threshold for logical blocks with deeply nested code
}

private void ConfigureJsonPolymorphismOptions(JsonTypeInfo jsonTypeInfo, params Type[] derivedTypes)
protected void ConfigureJsonPolymorphismOptions(JsonTypeInfo jsonTypeInfo, params Type[] derivedTypes)
{
jsonTypeInfo.PolymorphismOptions = new JsonPolymorphismOptions
{
Expand Down
11 changes: 8 additions & 3 deletions src/Umbraco.Core/DeliveryApi/ApiContentResponseBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using Umbraco.Cms.Core.Models.DeliveryApi;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Routing;
using Umbraco.Extensions;

namespace Umbraco.Cms.Core.DeliveryApi;

public sealed class ApiContentResponseBuilder : ApiContentBuilderBase<IApiContentResponse>, IApiContentResponseBuilder
public class ApiContentResponseBuilder : ApiContentBuilderBase<IApiContentResponse>, IApiContentResponseBuilder
{
private readonly IApiContentRouteBuilder _apiContentRouteBuilder;

Expand All @@ -14,6 +13,12 @@ public ApiContentResponseBuilder(IApiContentNameProvider apiContentNameProvider,
=> _apiContentRouteBuilder = apiContentRouteBuilder;

protected override IApiContentResponse Create(IPublishedContent content, string name, IApiContentRoute route, IDictionary<string, object?> properties)
{
IDictionary<string, IApiContentRoute> cultures = GetCultures(content);
return new ApiContentResponse(content.Key, name, content.ContentType.Alias, content.CreateDate, content.UpdateDate, route, properties, cultures);
}

protected virtual IDictionary<string, IApiContentRoute> GetCultures(IPublishedContent content)
{
var routesByCulture = new Dictionary<string, IApiContentRoute>();

Expand All @@ -35,6 +40,6 @@ protected override IApiContentResponse Create(IPublishedContent content, string
routesByCulture[publishedCultureInfo.Culture] = cultureRoute;
}

return new ApiContentResponse(content.Key, name, content.ContentType.Alias, content.CreateDate, content.UpdateDate, route, properties, routesByCulture);
return routesByCulture;
}
}
Loading