Skip to content

Commit

Permalink
Make the API content response builder extendable (#16056)
Browse files Browse the repository at this point in the history
* Make the API content response builder extendable

* DeliveryApiJsonTypeResolver needs to be extendable too
  • Loading branch information
kjac committed Apr 16, 2024
1 parent 6379f2f commit a6a76d1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
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 @@ public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions option
{
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>();
}

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;
}
}

0 comments on commit a6a76d1

Please sign in to comment.