Make property container type parsing explicit (#15963)

This commit is contained in:
Kenn Jacobsen
2024-04-04 09:32:24 +02:00
committed by GitHub
parent d74b4d99d1
commit 70878f501e
5 changed files with 51 additions and 0 deletions

View File

@@ -44,6 +44,10 @@ public abstract class DocumentTypeControllerBase : ManagementApiControllerBase
.WithTitle("Invalid container name")
.WithDetail("One or more container names are invalid")
.Build()),
ContentTypeOperationStatus.InvalidContainerType => new BadRequestObjectResult(problemDetailsBuilder
.WithTitle("Invalid container type")
.WithDetail("One or more container types are invalid")
.Build()),
ContentTypeOperationStatus.MissingContainer => new BadRequestObjectResult(problemDetailsBuilder
.WithTitle("Missing container")
.WithDetail("One or more containers or properties are listed as parents to containers that are not defined.")

View File

@@ -343,6 +343,11 @@ internal abstract class ContentTypeEditingServiceBase<TContentType, TContentType
private ContentTypeOperationStatus ValidateContainers(ContentTypeEditingModelBase<TPropertyTypeModel, TPropertyTypeContainer> model, IContentTypeComposition[] allContentTypeCompositions)
{
if (model.Containers.Any(container => Enum.TryParse<PropertyGroupType>(container.Type, out _) is false))
{
return ContentTypeOperationStatus.InvalidContainerType;
}
// all property container keys must be present in the model
Guid[] modelContainerKeys = model.Containers.Select(c => c.Key).ToArray();
if (model.Properties.Any(p => p.ContainerKey is not null && modelContainerKeys.Contains(p.ContainerKey.Value) is false))

View File

@@ -12,6 +12,7 @@ public enum ContentTypeOperationStatus
InvalidComposition,
InvalidParent,
InvalidContainerName,
InvalidContainerType,
MissingContainer,
DuplicateContainer,
NotFound,

View File

@@ -1086,4 +1086,21 @@ public partial class ContentTypeEditingServiceTests
Assert.IsFalse(result.Success);
Assert.AreEqual(ContentTypeOperationStatus.InvalidComposition, result.Status);
}
[TestCase("something")]
[TestCase("tab")]
[TestCase("group")]
public async Task Cannot_Create_Container_With_Unknown_Type(string containerType)
{
var createModel = ContentTypeCreateModel("Test", "test");
var container = ContentTypePropertyContainerModel(name: containerType, type: containerType);
createModel.Containers = new[] { container };
var propertyType = ContentTypePropertyTypeModel("Test Property", "testProperty", containerKey: container.Key);
createModel.Properties = new[] { propertyType };
var result = await ContentTypeEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey);
Assert.IsFalse(result.Success);
Assert.AreEqual(ContentTypeOperationStatus.InvalidContainerType, result.Status);
}
}

View File

@@ -802,4 +802,28 @@ public partial class ContentTypeEditingServiceTests
Assert.IsFalse(result.Success);
Assert.AreEqual(ContentTypeOperationStatus.InvalidInheritance, result.Status);
}
[TestCase("something")]
[TestCase("tab")]
[TestCase("group")]
public async Task Cannot_Update_Container_Types_To_Unknown_Types(string containerType)
{
var createModel = ContentTypeCreateModel("Test", "test");
var container = ContentTypePropertyContainerModel("One");
createModel.Containers = new[] { container };
var property = ContentTypePropertyTypeModel("Test Property", "testProperty", containerKey: container.Key);
createModel.Properties = new[] { property };
var contentType = (await ContentTypeEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey)).Result!;
var updateModel = ContentTypeUpdateModel("Test", "test");
container.Type = containerType;
updateModel.Containers = new[] { container };
updateModel.Properties = new[] { property };
var result = await ContentTypeEditingService.UpdateAsync(contentType, updateModel, Constants.Security.SuperUserKey);
Assert.IsFalse(result.Success);
Assert.AreEqual(ContentTypeOperationStatus.InvalidContainerType, result.Status);
}
}