diff --git a/Kontent.Ai.Management.Tests/Modules/ModelBuilders/ElementModelProviderTests.cs b/Kontent.Ai.Management.Tests/Modules/ModelBuilders/ElementModelProviderTests.cs index bf4e6a01..89a7c078 100644 --- a/Kontent.Ai.Management.Tests/Modules/ModelBuilders/ElementModelProviderTests.cs +++ b/Kontent.Ai.Management.Tests/Modules/ModelBuilders/ElementModelProviderTests.cs @@ -39,16 +39,29 @@ public void GetStronglyTypedElements_ReturnsExpected() Assert.Single(actual.BodyCopy.Components); AssertIdentifiers(expected.BodyCopy.Components.Select(x => x.Id), actual.BodyCopy.Components.Select(x => x.Id)); AssertIdentifiers(expected.BodyCopy.Components.Select(x => x.Type.Id.Value), actual.BodyCopy.Components.Select(x => x.Type.Id.Value)); - AssertIdentifiers(expected.RelatedArticles.Value.Select(x => x.Id.Value), actual.RelatedArticles.Value.Select(x => x.Id.Value)); - AssertIdentifiers(expected.TeaserImage.Value.Select(x => x.Id.Value), actual.TeaserImage.Value.Select(x => x.Id.Value)); + + AssertIdentifiers(expected.RelatedArticles.Value.Where(x => x.Id != null).Select(x => x.Id.Value), actual.RelatedArticles.Value.Where(x => x.Id != null).Select(x => x.Id.Value)); + AssertIdentifiers(expected.RelatedArticles.Value.Where(x => !string.IsNullOrEmpty(x.Codename)).Select(x => x.Codename), actual.RelatedArticles.Value.Where(x => !string.IsNullOrEmpty(x.Codename)).Select(x => x.Codename)); + AssertIdentifiers(expected.RelatedArticles.Value.Where(x => !string.IsNullOrEmpty(x.ExternalId)).Select(x => x.ExternalId), actual.RelatedArticles.Value.Where(x => !string.IsNullOrEmpty(x.ExternalId)).Select(x => x.ExternalId)); + + AssertIdentifiers(expected.TeaserImage.Value.Where(x => x.Id != null).Select(x => x.Id.Value), actual.TeaserImage.Value.Where(x => x.Id != null).Select(x => x.Id.Value)); + AssertIdentifiers(expected.TeaserImage.Value.Where(x => !string.IsNullOrEmpty(x.Codename)).Select(x => x.Codename), actual.TeaserImage.Value.Where(x => !string.IsNullOrEmpty(x.Codename)).Select(x => x.Codename)); + AssertIdentifiers(expected.TeaserImage.Value.Where(x => !string.IsNullOrEmpty(x.ExternalId)).Select(x => x.ExternalId), actual.TeaserImage.Value.Where(x => !string.IsNullOrEmpty(x.ExternalId)).Select(x => x.ExternalId)); + for (var i = 0; i < expected.TeaserImage.Value.Count(); i++) { AssertIdentifiers( expected.TeaserImage.Value.ElementAt(i).Renditions.Select(x => x.Id.Value), actual.TeaserImage.Value.ElementAt(i).Renditions.Select(x => x.Id.Value)); } - AssertIdentifiers(expected.Options.Value.Select(x => x.Id.Value), actual.Options.Value.Select(x => x.Id.Value)); - AssertIdentifiers(expected.Personas.Value.Select(x => x.Id.Value), actual.Personas.Value?.Select(x => x.Id.Value)); + + AssertIdentifiers(expected.Personas.Value.Where(x => x.Id != null).Select(x => x.Id.Value), actual.Personas.Value.Where(x => x.Id != null).Select(x => x.Id.Value)); + AssertIdentifiers(expected.Personas.Value.Where(x => !string.IsNullOrEmpty(x.Codename)).Select(x => x.Codename), actual.Personas.Value.Where(x => !string.IsNullOrEmpty(x.Codename)).Select(x => x.Codename)); + AssertIdentifiers(expected.Personas.Value.Where(x => !string.IsNullOrEmpty(x.ExternalId)).Select(x => x.ExternalId), actual.Personas.Value.Where(x => !string.IsNullOrEmpty(x.ExternalId)).Select(x => x.ExternalId)); + + AssertIdentifiers(expected.Options.Value.Where(x => x.Id != null).Select(x => x.Id.Value), actual.Options.Value.Where(x => x.Id != null).Select(x => x.Id.Value)); + AssertIdentifiers(expected.Options.Value.Where(x => !string.IsNullOrEmpty(x.Codename)).Select(x => x.Codename), actual.Options.Value.Where(x => !string.IsNullOrEmpty(x.Codename)).Select(x => x.Codename)); + AssertIdentifiers(expected.Options.Value.Where(x => !string.IsNullOrEmpty(x.ExternalId)).Select(x => x.ExternalId), actual.Options.Value.Where(x => !string.IsNullOrEmpty(x.ExternalId)).Select(x => x.ExternalId)); } [Fact] @@ -59,61 +72,69 @@ public void GetLanguageVariantUpsertModel_ReturnsExpected() var dynamicElements = _elementModelProvider.GetDynamicElements(model); - var titleValue = dynamicElements.SingleOrDefault(elementObject => - elementObject.element.id == type.GetProperty(nameof(model.Title))?.GetKontentElementId() - ).value; + var title = dynamicElements.SingleOrDefault(elementObject => elementObject.element.id == type.GetProperty(nameof(model.Title))?.GetKontentElementId() ); + var titleValue = ((TextElement)TextElement.FromDynamic(title, Models.Types.Elements.ElementMetadataType.Text)).Value; - var ratingValue = dynamicElements.SingleOrDefault(elementObject => - elementObject.element.id == type.GetProperty(nameof(model.Rating))?.GetKontentElementId() - ).value; + var rating = dynamicElements.SingleOrDefault(elementObject => elementObject.element.id == type.GetProperty(nameof(model.Rating))?.GetKontentElementId()); + var ratingValue = ((NumberElement)NumberElement.FromDynamic(rating, Models.Types.Elements.ElementMetadataType.Number)).Value; - var selectedForm = dynamicElements.SingleOrDefault(elementObject => - elementObject.element.id == type.GetProperty(nameof(model.SelectedForm))?.GetKontentElementId() - ); + var selectedForm = dynamicElements.SingleOrDefault(elementObject => elementObject.element.id == type.GetProperty(nameof(model.SelectedForm))?.GetKontentElementId()); + var selectedFormElement = ((CustomElement)CustomElement.FromDynamic(selectedForm, Models.Types.Elements.ElementMetadataType.Custom)); - var postDateElement = dynamicElements.SingleOrDefault(elementObject => - elementObject.element.id == type.GetProperty(nameof(model.PostDate))?.GetKontentElementId() - ); + var postDate = dynamicElements.SingleOrDefault(elementObject => elementObject.element.id == type.GetProperty(nameof(model.PostDate))?.GetKontentElementId()); + var postDateElement = ((DateTimeElement)DateTimeElement.FromDynamic(postDate, Models.Types.Elements.ElementMetadataType.DateTime)); - var urlPatternElement = dynamicElements.SingleOrDefault(elementObject => - elementObject.element.id == type.GetProperty(nameof(model.UrlPattern))?.GetKontentElementId() - ); + var urlPattern = dynamicElements.SingleOrDefault(elementObject => elementObject.element.id == type.GetProperty(nameof(model.UrlPattern))?.GetKontentElementId()); + var urlPatternElement = ((UrlSlugElement)UrlSlugElement.FromDynamic(urlPattern, Models.Types.Elements.ElementMetadataType.UrlSlug)); - var bodyCopyElement = dynamicElements.SingleOrDefault(elementObject => - elementObject.element.id == type.GetProperty(nameof(model.BodyCopy))?.GetKontentElementId() - ); + var bodyCopyElement = dynamicElements.SingleOrDefault(elementObject => elementObject.element.id == type.GetProperty(nameof(model.BodyCopy))?.GetKontentElementId()); - var relatedArticlesValue = dynamicElements.SingleOrDefault(elementObject => - elementObject.element.id == type.GetProperty(nameof(model.RelatedArticles))?.GetKontentElementId() - ).value as IEnumerable; + var relatedArticles = dynamicElements.SingleOrDefault(elementObject => elementObject.element.id == type.GetProperty(nameof(model.RelatedArticles))?.GetKontentElementId()); + var relatedArticlesValue = ((LinkedItemsElement)LinkedItemsElement.FromDynamic(relatedArticles, Models.Types.Elements.ElementMetadataType.LinkedItems)).Value; - var teaserImageValue = dynamicElements.SingleOrDefault(elementObject => - elementObject.element.id == type.GetProperty(nameof(model.TeaserImage))?.GetKontentElementId() - ).value as IEnumerable; + var teaserImage = dynamicElements.SingleOrDefault(elementObject => elementObject.element.id == type.GetProperty(nameof(model.TeaserImage))?.GetKontentElementId()); + var teaserImageValue = ((AssetElement)AssetElement.FromDynamic(teaserImage, Models.Types.Elements.ElementMetadataType.Asset)).Value; - var personaValue = dynamicElements.SingleOrDefault(elementObject => - elementObject.element.id == type.GetProperty(nameof(model.Personas))?.GetKontentElementId() - ).value as IEnumerable; + var persona = dynamicElements.SingleOrDefault(elementObject => elementObject.element.id == type.GetProperty(nameof(model.Personas))?.GetKontentElementId()); + var personaValue = ((TaxonomyElement)TaxonomyElement.FromDynamic(persona, Models.Types.Elements.ElementMetadataType.Taxonomy)).Value; - var optionsValue = dynamicElements.SingleOrDefault(elementObject => - elementObject.element.id == type.GetProperty(nameof(model.Options))?.GetKontentElementId() - ).value as IEnumerable; + var options = dynamicElements.SingleOrDefault(elementObject => elementObject.element.id == type.GetProperty(nameof(model.Options))?.GetKontentElementId()); + var optionsValue = ((MultipleChoiceElement)MultipleChoiceElement.FromDynamic(options, Models.Types.Elements.ElementMetadataType.MultipleChoice)).Value; Assert.Equal(model.Title.Value, titleValue); Assert.Equal(model.Rating.Value, ratingValue); - Assert.Equal(model.SelectedForm.Value, selectedForm.value); - Assert.Equal(model.SelectedForm.SearchableValue, selectedForm.searchable_value); - Assert.Equal(model.PostDate.Value, postDateElement.value); - Assert.Equal(model.PostDate.DisplayTimeZone, postDateElement.display_timezone); - Assert.Equal(model.UrlPattern.Value, urlPatternElement.value); - Assert.Equal(model.UrlPattern.Mode, urlPatternElement.mode); + Assert.Equal(model.SelectedForm.Value, selectedFormElement.Value); + Assert.Equal(model.SelectedForm.SearchableValue, selectedFormElement.SearchableValue); + Assert.Equal(model.PostDate.Value, postDateElement.Value); + Assert.Equal(model.PostDate.DisplayTimeZone, postDateElement.DisplayTimeZone); + Assert.Equal(model.UrlPattern.Value, urlPatternElement.Value); + Assert.Equal(model.UrlPattern.Mode, urlPatternElement.Mode); Assert.Equal(model.BodyCopy.Value, bodyCopyElement.value); Assert.Single(bodyCopyElement.components as IEnumerable); AssertIdentifiers(model.BodyCopy.Components.Select(x => x.Id), (bodyCopyElement.components as IEnumerable)?.Select(x => x.Id)); - AssertIdentifiers(model.RelatedArticles.Value.Select(x => x.Id.Value), relatedArticlesValue.Select(x => x.Id.Value)); - AssertIdentifiers(model.TeaserImage.Value.Select(x => x.Id.Value), teaserImageValue.Select(x => x.Id.Value)); - AssertIdentifiers(model.Personas.Value.Select(x => x.Id.Value), personaValue.Select(x => x.Id.Value)); - AssertIdentifiers(model.Options.Value.Select(x => x.Id.Value), optionsValue.Select(x => x.Id.Value)); + + AssertIdentifiers(model.RelatedArticles.Value.Where(x => x.Id != null).Select(x => x.Id.Value), relatedArticlesValue.Where(x => x.Id != null).Select(x => x.Id.Value)); + AssertIdentifiers(model.RelatedArticles.Value.Where(x => !string.IsNullOrEmpty(x.Codename)).Select(x => x.Codename), relatedArticlesValue.Where(x => !string.IsNullOrEmpty(x.Codename)).Select(x => x.Codename)); + AssertIdentifiers(model.RelatedArticles.Value.Where(x => !string.IsNullOrEmpty(x.ExternalId)).Select(x => x.ExternalId), relatedArticlesValue.Where(x => !string.IsNullOrEmpty(x.ExternalId)).Select(x => x.ExternalId)); + + AssertIdentifiers(model.TeaserImage.Value.Where(x => x.Id != null).Select(x => x.Id.Value), teaserImageValue.Where(x => x.Id != null).Select(x => x.Id.Value)); + AssertIdentifiers(model.TeaserImage.Value.Where(x => !string.IsNullOrEmpty(x.Codename)).Select(x => x.Codename), teaserImageValue.Where(x => !string.IsNullOrEmpty(x.Codename)).Select(x => x.Codename)); + AssertIdentifiers(model.TeaserImage.Value.Where(x => !string.IsNullOrEmpty(x.ExternalId)).Select(x => x.ExternalId), teaserImageValue.Where(x => !string.IsNullOrEmpty(x.ExternalId)).Select(x => x.ExternalId)); + + for (var i = 0; i < model.TeaserImage.Value.Count(); i++) + { + AssertIdentifiers( + model.TeaserImage.Value.ElementAt(i).Renditions.Select(x => x.Id.Value), + teaserImageValue.ElementAt(i).Renditions.Select(x => x.Id.Value)); + } + + AssertIdentifiers(model.Personas.Value.Where(x => x.Id != null).Select(x => x.Id.Value), personaValue.Where(x => x.Id != null).Select(x => x.Id.Value)); + AssertIdentifiers(model.Personas.Value.Where(x => !string.IsNullOrEmpty(x.Codename)).Select(x => x.Codename), personaValue.Where(x => !string.IsNullOrEmpty(x.Codename)).Select(x => x.Codename)); + AssertIdentifiers(model.Personas.Value.Where(x => !string.IsNullOrEmpty(x.ExternalId)).Select(x => x.ExternalId), personaValue.Where(x => !string.IsNullOrEmpty(x.ExternalId)).Select(x => x.ExternalId)); + + AssertIdentifiers(model.Options.Value.Where(x => x.Id != null).Select(x => x.Id.Value), optionsValue.Where(x => x.Id != null).Select(x => x.Id.Value)); + AssertIdentifiers(model.Options.Value.Where(x => !string.IsNullOrEmpty(x.Codename)).Select(x => x.Codename), optionsValue.Where(x => !string.IsNullOrEmpty(x.Codename)).Select(x => x.Codename)); + AssertIdentifiers(model.Options.Value.Where(x => !string.IsNullOrEmpty(x.ExternalId)).Select(x => x.ExternalId), optionsValue.Where(x => !string.IsNullOrEmpty(x.ExternalId)).Select(x => x.ExternalId)); } private static ComplexTestModel GetTestModel() @@ -150,16 +171,45 @@ private static ComplexTestModel GetTestModel() }, TeaserImage = new AssetElement { - Value = new[] - { - new AssetWithRenditionsReference(Reference.ById(Guid.NewGuid()), new[] { Reference.ById(Guid.NewGuid()) }), - new AssetWithRenditionsReference(Reference.ById(Guid.NewGuid()), new[] { Reference.ById(Guid.NewGuid()), Reference.ById(Guid.NewGuid()), }), - new AssetWithRenditionsReference(Reference.ById(Guid.NewGuid()), Array.Empty()), - } + Value = + [ + new AssetWithRenditionsReference(Reference.ById(Guid.NewGuid()), [Reference.ById(Guid.NewGuid())]), + new AssetWithRenditionsReference(Reference.ById(Guid.NewGuid()), [Reference.ById(Guid.NewGuid()), Reference.ById(Guid.NewGuid()),]), + new AssetWithRenditionsReference(Reference.ById(Guid.NewGuid()), []), + new AssetWithRenditionsReference(Reference.ByCodename("asset-with-rendition"), []), + new AssetWithRenditionsReference(Reference.ByExternalId("asset-with-external-id"), []), + ] + }, + RelatedArticles = new LinkedItemsElement + { + Value = + [ + Reference.ById(Guid.NewGuid()), + Reference.ById(Guid.NewGuid()), + Reference.ByCodename("related-item-by-codename"), + Reference.ByExternalId("related-item-by-external-id") + ] + }, + Personas = new TaxonomyElement + { + Value = + [ + Reference.ById(Guid.NewGuid()), + Reference.ById(Guid.NewGuid()), + Reference.ByCodename("taxonomy-item-by-codename"), + Reference.ByExternalId("taxonomy-item-by-external-id") + ] + }, + Options = new MultipleChoiceElement + { + Value = + [ + Reference.ById(Guid.NewGuid()), + Reference.ById(Guid.NewGuid()), + Reference.ByCodename("multi-choice-item-by-codename"), + Reference.ByExternalId("multi-choice-item-by-external-id") + ] }, - RelatedArticles = new LinkedItemsElement { Value = new[] { Guid.NewGuid(), Guid.NewGuid() }.Select(Reference.ById).ToArray() }, - Personas = new TaxonomyElement { Value = new[] { Guid.NewGuid(), Guid.NewGuid() }.Select(Reference.ById).ToList() }, - Options = new MultipleChoiceElement { Value = new[] { Guid.NewGuid(), Guid.NewGuid() }.Select(Reference.ById).ToList() }, }; } @@ -208,17 +258,17 @@ private static IEnumerable PrepareMockDynamicResponse(ComplexTestModel value = model.RelatedArticles.Value }, new - { + { element = new { id = type.GetProperty(nameof(ComplexTestModel.TeaserImage))?.GetKontentElementId() }, value = model.TeaserImage.Value }, new - { + { element = new { id = type.GetProperty(nameof(ComplexTestModel.Personas))?.GetKontentElementId() }, value = model.Personas.Value }, new - { + { element = new { id = type.GetProperty(nameof(ComplexTestModel.Options))?.GetKontentElementId() }, value = model.Options.Value }, @@ -231,7 +281,7 @@ private static IEnumerable PrepareMockDynamicResponse(ComplexTestModel return JsonConvert.DeserializeObject>(serialized, new JsonSerializerSettings { Converters = new JsonConverter[] { new DynamicObjectJsonConverter() } }); } - private static void AssertIdentifiers(IEnumerable expected, IEnumerable actual) + private static void AssertIdentifiers(IEnumerable expected, IEnumerable actual) { if (expected == null && actual == null) { @@ -244,4 +294,4 @@ private static void AssertIdentifiers(IEnumerable expected, IEnumerable public override dynamic ToDynamic() => new { element = Element.ToDynamic(), - value = Value, + value = Value.Select(v => v.ToDynamic()) }; } diff --git a/Kontent.Ai.Management/Models/LanguageVariants/Elements/BaseElement.cs b/Kontent.Ai.Management/Models/LanguageVariants/Elements/BaseElement.cs index 3257f0d5..7ca38dec 100644 --- a/Kontent.Ai.Management/Models/LanguageVariants/Elements/BaseElement.cs +++ b/Kontent.Ai.Management/Models/LanguageVariants/Elements/BaseElement.cs @@ -87,11 +87,7 @@ public static BaseElement FromDynamic(dynamic source, Type type) return new AssetElement { Element = Reference.FromDynamic(source.element), - Value = (source.value as IEnumerable)? - .Select(assetWithRenditionsReferences => - new AssetWithRenditionsReference( - Reference.ById(Guid.Parse(assetWithRenditionsReferences.id)), - (assetWithRenditionsReferences.renditions as IEnumerable)?.Select(renditionIdentifier => Reference.ById(Guid.Parse(renditionIdentifier.id))))), + Value = (source.value as IEnumerable)?.Select(AssetWithRenditionsReference.FromDynamic) }; } else if (type == typeof(DateTimeElement)) @@ -108,7 +104,7 @@ public static BaseElement FromDynamic(dynamic source, Type type) return new LinkedItemsElement { Element = Reference.FromDynamic(source.element), - Value = (source.value as IEnumerable)?.Select(identifier => Reference.ById(Guid.Parse(identifier.id))) + Value = (source.value as IEnumerable)?.Select(Reference.FromDynamic) }; } else if (type == typeof(MultipleChoiceElement)) @@ -116,7 +112,7 @@ public static BaseElement FromDynamic(dynamic source, Type type) return new MultipleChoiceElement { Element = Reference.FromDynamic(source.element), - Value = (source.value as IEnumerable)?.Select(identifier => Reference.ById(Guid.Parse(identifier.id))) + Value = (source.value as IEnumerable)?.Select(Reference.FromDynamic) }; } else if (type == typeof(TaxonomyElement)) @@ -124,7 +120,7 @@ public static BaseElement FromDynamic(dynamic source, Type type) return new TaxonomyElement { Element = Reference.FromDynamic(source.element), - Value = (source.value as IEnumerable)?.Select(identifier => Reference.ById(Guid.Parse(identifier.id))) + Value = (source.value as IEnumerable)?.Select(Reference.FromDynamic) }; } else if (type == typeof(UrlSlugElement)) @@ -150,7 +146,7 @@ public static BaseElement FromDynamic(dynamic source, Type type) return new SubpagesElement { Element = Reference.FromDynamic(source.element), - Value = (source.value as IEnumerable)?.Select(identifier => Reference.ById(Guid.Parse(identifier.id))) + Value = (source.value as IEnumerable)?.Select(Reference.FromDynamic) }; } } diff --git a/Kontent.Ai.Management/Models/LanguageVariants/Elements/LinkedItemsElement.cs b/Kontent.Ai.Management/Models/LanguageVariants/Elements/LinkedItemsElement.cs index ba162227..a70ec656 100644 --- a/Kontent.Ai.Management/Models/LanguageVariants/Elements/LinkedItemsElement.cs +++ b/Kontent.Ai.Management/Models/LanguageVariants/Elements/LinkedItemsElement.cs @@ -1,6 +1,7 @@ using Kontent.Ai.Management.Models.Shared; using Newtonsoft.Json; using System.Collections.Generic; +using System.Linq; namespace Kontent.Ai.Management.Models.LanguageVariants.Elements; @@ -20,6 +21,6 @@ public class LinkedItemsElement : BaseElement /// public override dynamic ToDynamic() => new { element = Element.ToDynamic(), - value = Value, + value = Value.Select(v => v.ToDynamic()) }; } diff --git a/Kontent.Ai.Management/Models/LanguageVariants/Elements/MultipleChoiceElement.cs b/Kontent.Ai.Management/Models/LanguageVariants/Elements/MultipleChoiceElement.cs index cf03f9c5..9fe0bcb0 100644 --- a/Kontent.Ai.Management/Models/LanguageVariants/Elements/MultipleChoiceElement.cs +++ b/Kontent.Ai.Management/Models/LanguageVariants/Elements/MultipleChoiceElement.cs @@ -1,6 +1,7 @@ using Kontent.Ai.Management.Models.Shared; using Newtonsoft.Json; using System.Collections.Generic; +using System.Linq; namespace Kontent.Ai.Management.Models.LanguageVariants.Elements; @@ -20,6 +21,6 @@ public class MultipleChoiceElement : BaseElement /// public override dynamic ToDynamic() => new { element = Element.ToDynamic(), - value = Value, + value = Value.Select(v => v.ToDynamic()) }; } diff --git a/Kontent.Ai.Management/Models/LanguageVariants/Elements/SubpagesElement.cs b/Kontent.Ai.Management/Models/LanguageVariants/Elements/SubpagesElement.cs index 888fcff0..d0e9bc03 100644 --- a/Kontent.Ai.Management/Models/LanguageVariants/Elements/SubpagesElement.cs +++ b/Kontent.Ai.Management/Models/LanguageVariants/Elements/SubpagesElement.cs @@ -1,6 +1,7 @@ using Kontent.Ai.Management.Models.Shared; using Newtonsoft.Json; using System.Collections.Generic; +using System.Linq; namespace Kontent.Ai.Management.Models.LanguageVariants.Elements; @@ -20,6 +21,6 @@ public class SubpagesElement : BaseElement /// public override dynamic ToDynamic() => new { element = Element.ToDynamic(), - value = Value, + value = Value.Select(v => v.ToDynamic()) }; } diff --git a/Kontent.Ai.Management/Models/LanguageVariants/Elements/TaxonomyElement.cs b/Kontent.Ai.Management/Models/LanguageVariants/Elements/TaxonomyElement.cs index aec4838f..7a9f76a1 100644 --- a/Kontent.Ai.Management/Models/LanguageVariants/Elements/TaxonomyElement.cs +++ b/Kontent.Ai.Management/Models/LanguageVariants/Elements/TaxonomyElement.cs @@ -1,6 +1,7 @@ using Kontent.Ai.Management.Models.Shared; using Newtonsoft.Json; using System.Collections.Generic; +using System.Linq; namespace Kontent.Ai.Management.Models.LanguageVariants.Elements; @@ -20,6 +21,6 @@ public class TaxonomyElement : BaseElement /// public override dynamic ToDynamic() => new { element = Element.ToDynamic(), - value = Value, + value = Value.Select(v => v.ToDynamic()), }; } diff --git a/Kontent.Ai.Management/Models/Shared/AssetWithRenditionsReference.cs b/Kontent.Ai.Management/Models/Shared/AssetWithRenditionsReference.cs index b96702f1..630ce718 100644 --- a/Kontent.Ai.Management/Models/Shared/AssetWithRenditionsReference.cs +++ b/Kontent.Ai.Management/Models/Shared/AssetWithRenditionsReference.cs @@ -60,4 +60,58 @@ public AssetWithRenditionsReference(Reference assetReference, IEnumerable(); } + + /// + /// Transforms the dynamic object to the + /// + public static AssetWithRenditionsReference FromDynamic(dynamic source) + { + try + { + var assetReference = Reference.FromDynamic(source); + var renditions = (source.renditions as IEnumerable)?.Select(Reference.FromDynamic); + + return new AssetWithRenditionsReference(assetReference, renditions); + } + catch (Exception exception) + { + throw new DataMisalignedException( + "Object could not be converted to the strongly-typed reference. Please check if it has expected properties with expected type", + exception); + } + + throw new DataMisalignedException("Dynamic element reference does not contain any identifier."); + } + + /// + /// Transforms the to the dynamic object. + /// + public dynamic ToDynamic() + { + if (Id != null) + { + return new { + id = Id, + renditions = Renditions.Select(r => r.ToDynamic()) + }; + } + + if (Codename != null) + { + return new { + codename = Codename, + renditions = Renditions.Select(r => r.ToDynamic()) + }; + } + + if (ExternalId != null) + { + return new { + external_id = ExternalId, + renditions = Renditions.Select(r => r.ToDynamic()) + }; + } + + throw new DataMisalignedException("Element reference does not contain any identifier."); + } } \ No newline at end of file