Skip to content

Commit 32ec84f

Browse files
authored
Merge pull request #50 from contentstack/enhc/DX-5736
feat(5736): add GetVariantMetadataTags; deprecate GetDataCsvariantsAttribute
2 parents 119a1de + d3642e5 commit 32ec84f

File tree

9 files changed

+804
-41
lines changed

9 files changed

+804
-41
lines changed

.github/workflows/unit-test.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Unit Test
2+
on:
3+
pull_request:
4+
push:
5+
6+
jobs:
7+
unit-test:
8+
runs-on: windows-latest
9+
steps:
10+
- name: Checkout repository
11+
uses: actions/checkout@v4.2.2
12+
- name: Setup .NET Core @ Latest
13+
uses: actions/setup-dotnet@v4.3.0
14+
- name: Build solution and run unit tests
15+
run: sh ./Scripts/run-unit-test-case.sh

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### Version: 1.2.0
2+
#### Date: March-31-2026
3+
- Added `GetVariantMetadataTags(JObject, string)` and `GetVariantMetadataTags(JArray, string)` as the canonical API for building the `data-csvariants` payload (same behavior as the previous helpers).
4+
15
### Version: 1.1.0
26
#### Date: March-24-2026
37
- Added `GetVariantAliases` and `GetDataCsvariantsAttribute` for variant alias extraction and `data-csvariants` serialization; Invalid arguments throw `ArgumentException`.

Contentstack.Utils.Tests/Contentstack.Utils.Tests.csproj

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
12-
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
13-
<PackageReference Include="coverlet.collector" Version="6.0.2">
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
12+
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
13+
<PackageReference Include="coverlet.collector" Version="6.0.4">
1414
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1515
<PrivateAssets>all</PrivateAssets>
1616
</PackageReference>
@@ -21,11 +21,6 @@
2121
<PrivateAssets>all</PrivateAssets>
2222
</PackageReference>
2323
</ItemGroup>
24-
<ItemGroup>
25-
<Reference Include="Contentstack.Utils">
26-
<HintPath>..\Contentstack.Utils\bin\Debug\netstandard2.0\Contentstack.Utils.dll</HintPath>
27-
</Reference>
28-
</ItemGroup>
2924
<ItemGroup>
3025
<ProjectReference Include="..\Contentstack.Utils\Contentstack.Utils.csproj" />
3126
</ItemGroup>

Contentstack.Utils.Tests/VariantAliasesTest.cs

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ public void GetVariantAliases_SingleEntry_ReturnsAliases()
4444
}
4545

4646
[Fact]
47-
public void GetDataCsvariantsAttribute_SingleEntry_ReturnsJsonArrayString()
47+
public void GetVariantMetadataTags_SingleEntry_ReturnsJsonArrayString()
4848
{
4949
JObject full = ReadJsonRoot("variantsSingleEntry.json");
5050
JObject entry = (JObject)full["entry"];
5151
const string contentTypeUid = "movie";
5252

53-
JObject result = Utils.GetDataCsvariantsAttribute(entry, contentTypeUid);
53+
JObject result = Utils.GetVariantMetadataTags(entry, contentTypeUid);
5454

5555
Assert.True(result["data-csvariants"] != null);
5656
string dataCsvariantsStr = result["data-csvariants"].ToString();
@@ -96,13 +96,13 @@ public void GetVariantAliases_MultipleEntries_ReturnsOneResultPerEntryWithUid()
9696
}
9797

9898
[Fact]
99-
public void GetDataCsvariantsAttribute_MultipleEntries_ReturnsJsonArrayString()
99+
public void GetVariantMetadataTags_MultipleEntries_ReturnsJsonArrayString()
100100
{
101101
JObject full = ReadJsonRoot("variantsEntries.json");
102102
JArray entries = (JArray)full["entries"];
103103
const string contentTypeUid = "movie";
104104

105-
JObject result = Utils.GetDataCsvariantsAttribute(entries, contentTypeUid);
105+
JObject result = Utils.GetVariantMetadataTags(entries, contentTypeUid);
106106

107107
Assert.True(result["data-csvariants"] != null);
108108
string dataCsvariantsStr = result["data-csvariants"].ToString();
@@ -139,9 +139,9 @@ public void GetVariantAliases_ThrowsWhenContentTypeUidEmpty()
139139
}
140140

141141
[Fact]
142-
public void GetDataCsvariantsAttribute_WhenEntryNull_ReturnsEmptyArrayString()
142+
public void GetVariantMetadataTags_WhenEntryNull_ReturnsEmptyArrayString()
143143
{
144-
JObject result = Utils.GetDataCsvariantsAttribute((JObject)null, "landing_page");
144+
JObject result = Utils.GetVariantMetadataTags((JObject)null, "landing_page");
145145
Assert.True(result["data-csvariants"] != null);
146146
Assert.Equal("[]", result["data-csvariants"].ToString());
147147
}
@@ -175,24 +175,52 @@ public void GetVariantAliases_Batch_ThrowsWhenContentTypeUidEmpty()
175175
}
176176

177177
[Fact]
178-
public void GetDataCsvariantsAttribute_WhenEntriesArrayNull_ReturnsEmptyArrayString()
178+
public void GetVariantMetadataTags_WhenEntriesArrayNull_ReturnsEmptyArrayString()
179179
{
180-
JObject result = Utils.GetDataCsvariantsAttribute((JArray)null, "movie");
180+
JObject result = Utils.GetVariantMetadataTags((JArray)null, "movie");
181181
Assert.Equal("[]", result["data-csvariants"].ToString());
182182
}
183183

184184
[Fact]
185-
public void GetDataCsvariantsAttribute_Batch_ThrowsWhenContentTypeUidNull()
185+
public void GetVariantMetadataTags_Batch_ThrowsWhenContentTypeUidNull()
186186
{
187187
var entries = new JArray { new JObject { ["uid"] = "a" } };
188-
Assert.Throws<ArgumentException>(() => Utils.GetDataCsvariantsAttribute(entries, null));
188+
Assert.Throws<ArgumentException>(() => Utils.GetVariantMetadataTags(entries, null));
189189
}
190190

191191
[Fact]
192-
public void GetDataCsvariantsAttribute_Batch_ThrowsWhenContentTypeUidEmpty()
192+
public void GetVariantMetadataTags_Batch_ThrowsWhenContentTypeUidEmpty()
193193
{
194194
var entries = new JArray { new JObject { ["uid"] = "a" } };
195-
Assert.Throws<ArgumentException>(() => Utils.GetDataCsvariantsAttribute(entries, ""));
195+
Assert.Throws<ArgumentException>(() => Utils.GetVariantMetadataTags(entries, ""));
196+
}
197+
198+
[Fact]
199+
public void GetDataCsvariantsAttribute_DelegatesToGetVariantMetadataTags()
200+
{
201+
#pragma warning disable CS0618 // Type or member is obsolete — intentional coverage of backward-compatible alias
202+
JObject full = ReadJsonRoot("variantsSingleEntry.json");
203+
JObject entry = (JObject)full["entry"];
204+
const string contentTypeUid = "movie";
205+
206+
JObject canonical = Utils.GetVariantMetadataTags(entry, contentTypeUid);
207+
JObject legacy = Utils.GetDataCsvariantsAttribute(entry, contentTypeUid);
208+
Assert.True(JToken.DeepEquals(canonical, legacy));
209+
210+
JObject fullMulti = ReadJsonRoot("variantsEntries.json");
211+
JArray entries = (JArray)fullMulti["entries"];
212+
JObject canonicalBatch = Utils.GetVariantMetadataTags(entries, contentTypeUid);
213+
JObject legacyBatch = Utils.GetDataCsvariantsAttribute(entries, contentTypeUid);
214+
Assert.True(JToken.DeepEquals(canonicalBatch, legacyBatch));
215+
216+
JObject nullEntryLegacy = Utils.GetDataCsvariantsAttribute((JObject)null, "x");
217+
JObject nullEntryCanonical = Utils.GetVariantMetadataTags((JObject)null, "x");
218+
Assert.True(JToken.DeepEquals(nullEntryCanonical, nullEntryLegacy));
219+
220+
JObject nullArrLegacy = Utils.GetDataCsvariantsAttribute((JArray)null, "x");
221+
JObject nullArrCanonical = Utils.GetVariantMetadataTags((JArray)null, "x");
222+
Assert.True(JToken.DeepEquals(nullArrCanonical, nullArrLegacy));
223+
#pragma warning restore CS0618
196224
}
197225

198226
[Fact]

Contentstack.Utils/Utils.cs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,13 @@ public static JArray GetVariantAliases(JArray entries, string contentTypeUid)
337337
return variantResults;
338338
}
339339

340-
public static JObject GetDataCsvariantsAttribute(JObject entry, string contentTypeUid)
340+
/// <summary>
341+
/// Builds the JSON object used for the <c>data-csvariants</c> HTML attribute payload from a single entry.
342+
/// </summary>
343+
/// <param name="entry">Entry JSON (e.g. from the Delivery API), or <c>null</c> to produce an empty payload.</param>
344+
/// <param name="contentTypeUid">Content type UID for the entry.</param>
345+
/// <returns>A <see cref="JObject"/> with a <c>data-csvariants</c> key whose value is a compact JSON array string.</returns>
346+
public static JObject GetVariantMetadataTags(JObject entry, string contentTypeUid)
341347
{
342348
if (entry == null)
343349
{
@@ -347,10 +353,16 @@ public static JObject GetDataCsvariantsAttribute(JObject entry, string contentTy
347353
}
348354
JArray entries = new JArray();
349355
entries.Add(entry);
350-
return GetDataCsvariantsAttribute(entries, contentTypeUid);
356+
return GetVariantMetadataTags(entries, contentTypeUid);
351357
}
352358

353-
public static JObject GetDataCsvariantsAttribute(JArray entries, string contentTypeUid)
359+
/// <summary>
360+
/// Builds the JSON object used for the <c>data-csvariants</c> HTML attribute payload from multiple entries.
361+
/// </summary>
362+
/// <param name="entries">Array of entry JSON objects, or <c>null</c> to produce an empty payload.</param>
363+
/// <param name="contentTypeUid">Content type UID shared by these entries.</param>
364+
/// <returns>A <see cref="JObject"/> with a <c>data-csvariants</c> key whose value is a compact JSON array string.</returns>
365+
public static JObject GetVariantMetadataTags(JArray entries, string contentTypeUid)
354366
{
355367
JObject result = new JObject();
356368
if (entries == null)
@@ -368,6 +380,24 @@ public static JObject GetDataCsvariantsAttribute(JArray entries, string contentT
368380
return result;
369381
}
370382

383+
/// <summary>
384+
/// Prefer <see cref="GetVariantMetadataTags(JObject, string)"/>. This alias exists for backward compatibility and will be removed in a future major release.
385+
/// </summary>
386+
[Obsolete("Use GetVariantMetadataTags instead. This method will be removed in a future major release.")]
387+
public static JObject GetDataCsvariantsAttribute(JObject entry, string contentTypeUid)
388+
{
389+
return GetVariantMetadataTags(entry, contentTypeUid);
390+
}
391+
392+
/// <summary>
393+
/// Prefer <see cref="GetVariantMetadataTags(JArray, string)"/>. This alias exists for backward compatibility and will be removed in a future major release.
394+
/// </summary>
395+
[Obsolete("Use GetVariantMetadataTags instead. This method will be removed in a future major release.")]
396+
public static JObject GetDataCsvariantsAttribute(JArray entries, string contentTypeUid)
397+
{
398+
return GetVariantMetadataTags(entries, contentTypeUid);
399+
}
400+
371401
private static JArray ExtractVariantAliasesFromEntry(JObject entry)
372402
{
373403
JArray variantArray = new JArray();

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>1.1.0</Version>
3+
<Version>1.2.0</Version>
44
</PropertyGroup>
55
</Project>

0 commit comments

Comments
 (0)