Skip to content

Commit 7c1a35b

Browse files
committed
add missing dynamic conversions for elements
1 parent 682b987 commit 7c1a35b

File tree

6 files changed

+73
-7
lines changed

6 files changed

+73
-7
lines changed

Kontent.Ai.Management/Models/LanguageVariants/Elements/AssetElement.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Kontent.Ai.Management.Models.Shared;
22
using Newtonsoft.Json;
33
using System.Collections.Generic;
4+
using System.Linq;
45

56
namespace Kontent.Ai.Management.Models.LanguageVariants.Elements;
67

@@ -20,6 +21,6 @@ public class AssetElement : BaseElement
2021
/// </summary>
2122
public override dynamic ToDynamic() => new {
2223
element = Element.ToDynamic(),
23-
value = Value,
24+
value = Value?.Select(v => v.ToDynamic()),
2425
};
2526
}

Kontent.Ai.Management/Models/LanguageVariants/Elements/LinkedItemsElement.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Kontent.Ai.Management.Models.Shared;
22
using Newtonsoft.Json;
33
using System.Collections.Generic;
4+
using System.Linq;
45

56
namespace Kontent.Ai.Management.Models.LanguageVariants.Elements;
67

@@ -20,6 +21,6 @@ public class LinkedItemsElement : BaseElement
2021
/// </summary>
2122
public override dynamic ToDynamic() => new {
2223
element = Element.ToDynamic(),
23-
value = Value,
24+
value = Value?.Select(v => v.ToDynamic()),
2425
};
2526
}

Kontent.Ai.Management/Models/LanguageVariants/Elements/MultipleChoiceElement.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Kontent.Ai.Management.Models.Shared;
22
using Newtonsoft.Json;
33
using System.Collections.Generic;
4+
using System.Linq;
45

56
namespace Kontent.Ai.Management.Models.LanguageVariants.Elements;
67

@@ -20,6 +21,6 @@ public class MultipleChoiceElement : BaseElement
2021
/// </summary>
2122
public override dynamic ToDynamic() => new {
2223
element = Element.ToDynamic(),
23-
value = Value,
24+
value = Value?.Select(v => v.ToDynamic()),
2425
};
2526
}

Kontent.Ai.Management/Models/LanguageVariants/Elements/SubpagesElement.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Kontent.Ai.Management.Models.Shared;
22
using Newtonsoft.Json;
33
using System.Collections.Generic;
4+
using System.Linq;
45

56
namespace Kontent.Ai.Management.Models.LanguageVariants.Elements;
67

@@ -20,6 +21,6 @@ public class SubpagesElement : BaseElement
2021
/// </summary>
2122
public override dynamic ToDynamic() => new {
2223
element = Element.ToDynamic(),
23-
value = Value,
24+
value = Value?.Select(v => v.ToDynamic()),
2425
};
2526
}

Kontent.Ai.Management/Models/LanguageVariants/Elements/TaxonomyElement.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Kontent.Ai.Management.Models.Shared;
22
using Newtonsoft.Json;
33
using System.Collections.Generic;
4+
using System.Linq;
45

56
namespace Kontent.Ai.Management.Models.LanguageVariants.Elements;
67

@@ -20,6 +21,6 @@ public class TaxonomyElement : BaseElement
2021
/// </summary>
2122
public override dynamic ToDynamic() => new {
2223
element = Element.ToDynamic(),
23-
value = Value,
24+
value = Value?.Select(v => v.ToDynamic()),
2425
};
2526
}

Kontent.Ai.Management/Models/Shared/AssetWithRenditionsReference.cs

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Kontent.Ai.Management.Extensions;
12
using Kontent.Ai.Management.Modules.ActionInvoker;
23
using Newtonsoft.Json;
34
using System;
@@ -46,7 +47,7 @@ public sealed class AssetWithRenditionsReference
4647
/// <param name="assetReference">Asset reference.</param>
4748
/// <param name="renditionReference">Rendition reference.</param>
4849
public AssetWithRenditionsReference(Reference assetReference, Reference renditionReference)
49-
: this(assetReference, new[] { renditionReference })
50+
: this(assetReference, [renditionReference])
5051
{
5152
}
5253

@@ -58,6 +59,66 @@ public AssetWithRenditionsReference(Reference assetReference, Reference renditio
5859
public AssetWithRenditionsReference(Reference assetReference, IEnumerable<Reference> renditionReferences = null)
5960
{
6061
_assetReference = assetReference;
61-
_renditions = renditionReferences?.ToList() ?? new List<Reference>();
62+
_renditions = renditionReferences?.ToList() ?? [];
63+
}
64+
65+
/// <summary>
66+
/// Transforms the dynamic object to the <see cref="AssetWithRenditionsReference"/>
67+
/// </summary>
68+
public static AssetWithRenditionsReference FromDynamic(dynamic source)
69+
{
70+
try
71+
{
72+
var assetReference = Reference.FromDynamic(source);
73+
74+
IEnumerable<Reference> renditions = null;
75+
if (DynamicExtensions.HasProperty(source, "renditions") && source.renditions != null)
76+
{
77+
renditions = (source.renditions as IEnumerable<dynamic>)?.Select(Reference.FromDynamic);
78+
}
79+
80+
return new AssetWithRenditionsReference(assetReference, renditions);
81+
}
82+
catch (Exception exception)
83+
{
84+
throw new DataMisalignedException(
85+
"Object could not be converted to the strongly-typed AssetWithRenditionsReference. Please check if it has expected properties with expected type",
86+
exception);
87+
}
88+
}
89+
90+
/// <summary>
91+
/// Transforms the <see cref="AssetWithRenditionsReference"/> to the dynamic object.
92+
/// </summary>
93+
public dynamic ToDynamic()
94+
{
95+
if (Id != null)
96+
{
97+
return new
98+
{
99+
id = Id,
100+
renditions = (_renditions ?? []).Select(r => r.ToDynamic())
101+
};
102+
}
103+
104+
if (Codename != null)
105+
{
106+
return new
107+
{
108+
codename = Codename,
109+
renditions = (_renditions ?? []).Select(r => r.ToDynamic())
110+
};
111+
}
112+
113+
if (ExternalId != null)
114+
{
115+
return new
116+
{
117+
external_id = ExternalId,
118+
renditions = (_renditions ?? []).Select(r => r.ToDynamic())
119+
};
120+
}
121+
122+
return _assetReference.ToDynamic();
62123
}
63124
}

0 commit comments

Comments
 (0)