Skip to content

Commit 33c1d4e

Browse files
authored
V14: Return all (even nested) compositions when getting a content type by id (#15800)
* Adding a generic implementation to get all content type compositions, even the nested ones * Modifying mappers to use the generic implementation
1 parent 1bcd45f commit 33c1d4e

File tree

4 files changed

+49
-23
lines changed

4 files changed

+49
-23
lines changed

src/Umbraco.Cms.Api.Management/Mapping/ContentType/ContentTypeMapDefinition.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,29 @@ protected IEnumerable<TPropertyTypeContainerModel> MapPropertyTypeContainers(TCo
7878
.ToArray();
7979
}
8080

81-
protected static CompositionType CalculateCompositionType(TContentType source, IContentTypeComposition contentTypeComposition)
82-
=> contentTypeComposition.Id == source.ParentId
81+
protected static CompositionType CalculateCompositionType(int contentTypeParentId, IContentTypeComposition contentTypeComposition)
82+
=> contentTypeComposition.Id == contentTypeParentId
8383
? CompositionType.Inheritance
8484
: CompositionType.Composition;
85+
86+
protected static IEnumerable<T> MapNestedCompositions<T>(IEnumerable<IContentTypeComposition> directCompositions, int contentTypeParentId, Func<ReferenceByIdModel, CompositionType, T> contentTypeCompositionFactory)
87+
{
88+
var allCompositions = new List<T>();
89+
90+
foreach (var composition in directCompositions)
91+
{
92+
CompositionType compositionType = CalculateCompositionType(contentTypeParentId, composition);
93+
T contentTypeComposition = contentTypeCompositionFactory(new ReferenceByIdModel(composition.Key), compositionType);
94+
allCompositions.Add(contentTypeComposition);
95+
96+
// When we have composition inheritance, we have to find all ancestor compositions recursively
97+
if (compositionType == CompositionType.Inheritance && composition.ContentTypeComposition.Any())
98+
{
99+
var nestedCompositions = MapNestedCompositions(composition.ContentTypeComposition, composition.ParentId, contentTypeCompositionFactory);
100+
allCompositions.AddRange(nestedCompositions);
101+
}
102+
}
103+
104+
return allCompositions;
105+
}
85106
}

src/Umbraco.Cms.Api.Management/Mapping/DocumentType/DocumentTypeMapDefinition.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Umbraco.Cms.Api.Management.Extensions;
2-
using Umbraco.Cms.Api.Management.Mapping.ContentType;
1+
using Umbraco.Cms.Api.Management.Mapping.ContentType;
32
using Umbraco.Cms.Api.Management.ViewModels;
43
using Umbraco.Cms.Api.Management.ViewModels.DocumentType;
54
using Umbraco.Cms.Core.Mapping;
@@ -37,11 +36,14 @@ private void Map(IContentType source, DocumentTypeResponseModel target, MapperCo
3736
target.AllowedDocumentTypes = source.AllowedContentTypes?.Select(ct =>
3837
new DocumentTypeSort { DocumentType = new ReferenceByIdModel(ct.Key), SortOrder = ct.SortOrder })
3938
.ToArray() ?? Enumerable.Empty<DocumentTypeSort>();
40-
target.Compositions = source.ContentTypeComposition.Select(contentTypeComposition => new DocumentTypeComposition
41-
{
42-
DocumentType = new ReferenceByIdModel(contentTypeComposition.Key),
43-
CompositionType = CalculateCompositionType(source, contentTypeComposition)
44-
}).ToArray();
39+
target.Compositions = MapNestedCompositions(
40+
source.ContentTypeComposition,
41+
source.ParentId,
42+
(referenceByIdModel, compositionType) => new DocumentTypeComposition
43+
{
44+
DocumentType = referenceByIdModel,
45+
CompositionType = compositionType,
46+
});
4547

4648
if (source.AllowedTemplates != null)
4749
{

src/Umbraco.Cms.Api.Management/Mapping/MediaType/MediaTypeMapDefinition.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Umbraco.Cms.Api.Management.Extensions;
2-
using Umbraco.Cms.Api.Management.Mapping.ContentType;
1+
using Umbraco.Cms.Api.Management.Mapping.ContentType;
32
using Umbraco.Cms.Api.Management.ViewModels;
43
using Umbraco.Cms.Api.Management.ViewModels.MediaType;
54
using Umbraco.Cms.Core.Mapping;
@@ -38,11 +37,14 @@ private void Map(IMediaType source, MediaTypeResponseModel target, MapperContext
3837
target.AllowedMediaTypes = source.AllowedContentTypes?.Select(ct =>
3938
new MediaTypeSort { MediaType = new ReferenceByIdModel(ct.Key), SortOrder = ct.SortOrder })
4039
.ToArray() ?? Enumerable.Empty<MediaTypeSort>();
41-
target.Compositions = source.ContentTypeComposition.Select(contentTypeComposition => new MediaTypeComposition
42-
{
43-
MediaType = new ReferenceByIdModel(contentTypeComposition.Key),
44-
CompositionType = CalculateCompositionType(source, contentTypeComposition)
45-
}).ToArray();
40+
target.Compositions = MapNestedCompositions(
41+
source.ContentTypeComposition,
42+
source.ParentId,
43+
(referenceByIdModel, compositionType) => new MediaTypeComposition
44+
{
45+
MediaType = referenceByIdModel,
46+
CompositionType = compositionType,
47+
});
4648
}
4749

4850
// Umbraco.Code.MapAll

src/Umbraco.Cms.Api.Management/Mapping/MemberType/MemberTypeMapDefinition.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Umbraco.Cms.Api.Management.Mapping.ContentType;
2-
using Umbraco.Cms.Api.Management.ViewModels;
32
using Umbraco.Cms.Api.Management.ViewModels.MemberType;
43
using Umbraco.Cms.Core.Mapping;
54
using Umbraco.Cms.Core.Models;
@@ -30,12 +29,14 @@ private void Map(IMemberType source, MemberTypeResponseModel target, MapperConte
3029
target.IsElement = source.IsElement;
3130
target.Containers = MapPropertyTypeContainers(source);
3231
target.Properties = MapPropertyTypes(source);
33-
34-
target.Compositions = source.ContentTypeComposition.Select(contentTypeComposition => new MemberTypeComposition
35-
{
36-
MemberType = new ReferenceByIdModel(contentTypeComposition.Key),
37-
CompositionType = CalculateCompositionType(source, contentTypeComposition)
38-
}).ToArray();
32+
target.Compositions = MapNestedCompositions(
33+
source.ContentTypeComposition,
34+
source.ParentId,
35+
(referenceByIdModel, compositionType) => new MemberTypeComposition
36+
{
37+
MemberType = referenceByIdModel,
38+
CompositionType = compositionType,
39+
});
3940
}
4041

4142
// Umbraco.Code.MapAll -Collection

0 commit comments

Comments
 (0)