Skip to content

Commit 384f120

Browse files
committed
feat: Added ApiVersion for global field to fetch the nested ones and clean the header after the fetch
1 parent d5b673c commit 384f120

File tree

8 files changed

+151
-16
lines changed

8 files changed

+151
-16
lines changed

Contentstack.Management.Core.Tests/IntegrationTest/Contentstack011_GlobalFieldTest.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,19 @@ public void Test006_Should_Query_Global_Field()
107107

108108
[TestMethod]
109109
[DoNotParallelize]
110-
public async System.Threading.Tasks.Task Test007_Should_Update_Async_Global_Field()
110+
public void Test006a_Should_Query_Global_Field_With_ApiVersion()
111+
{
112+
ContentstackResponse response = _stack.GlobalField(apiVersion: "3.2").Query().Find();
113+
GlobalFieldsModel globalField = response.OpenTResponse<GlobalFieldsModel>();
114+
Assert.IsNotNull(response);
115+
Assert.IsNotNull(globalField);
116+
Assert.IsNotNull(globalField.Modellings);
117+
Assert.AreEqual(1, globalField.Modellings.Count);
118+
}
119+
120+
[TestMethod]
121+
[DoNotParallelize]
122+
public async System.Threading.Tasks.Task Test007_Should_Query_Async_Global_Field()
111123
{
112124
ContentstackResponse response = await _stack.GlobalField().Query().FindAsync();
113125
GlobalFieldsModel globalField = response.OpenTResponse<GlobalFieldsModel>();
@@ -116,5 +128,17 @@ public async System.Threading.Tasks.Task Test007_Should_Update_Async_Global_Fiel
116128
Assert.IsNotNull(globalField.Modellings);
117129
Assert.AreEqual(1, globalField.Modellings.Count);
118130
}
131+
132+
[TestMethod]
133+
[DoNotParallelize]
134+
public async System.Threading.Tasks.Task Test007a_Should_Query_Async_Global_Field_With_ApiVersion()
135+
{
136+
ContentstackResponse response = await _stack.GlobalField(apiVersion: "3.2").Query().FindAsync();
137+
GlobalFieldsModel globalField = response.OpenTResponse<GlobalFieldsModel>();
138+
Assert.IsNotNull(response);
139+
Assert.IsNotNull(globalField);
140+
Assert.IsNotNull(globalField.Modellings);
141+
Assert.AreEqual(1, globalField.Modellings.Count);
142+
}
119143
}
120144
}

Contentstack.Management.Core.Tests/IntegrationTest/Contentstack012_NestedGlobalFieldTest.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Threading.Tasks;
56
using AutoFixture;
67
using Contentstack.Management.Core.Models;
@@ -239,6 +240,42 @@ public void Test007_Should_Query_Nested_Global_Fields()
239240
Assert.AreEqual("nested_global_field_test", nestedGlobalField.Uid);
240241
}
241242

243+
[TestMethod]
244+
[DoNotParallelize]
245+
public void Test007a_Should_Query_Nested_Global_Fields_With_ApiVersion()
246+
{
247+
ContentstackResponse response = _stack.GlobalField(apiVersion: "3.2").Query().Find();
248+
GlobalFieldsModel globalFields = response.OpenTResponse<GlobalFieldsModel>();
249+
var jsonResponse = response.OpenJObjectResponse();
250+
251+
Assert.IsNotNull(response);
252+
Assert.IsNotNull(globalFields);
253+
Assert.IsNotNull(globalFields.Modellings);
254+
Assert.IsTrue(globalFields.Modellings.Count >= 1);
255+
256+
var nestedGlobalField = globalFields.Modellings.Find(gf => gf.Uid == "nested_global_field_test");
257+
Assert.IsNotNull(nestedGlobalField);
258+
Assert.AreEqual("nested_global_field_test", nestedGlobalField.Uid);
259+
260+
// Find the global field reference and check its reference_to from the raw JSON
261+
var globalFieldReference = nestedGlobalField.Schema
262+
.FirstOrDefault(f => f.DataType == "global_field" && f.Uid == "global_field_reference");
263+
264+
Assert.IsNotNull(globalFieldReference);
265+
Assert.AreEqual("global_field", globalFieldReference.DataType);
266+
Assert.AreEqual("global_field_reference", globalFieldReference.Uid);
267+
Assert.AreEqual("Global Field Reference", globalFieldReference.DisplayName);
268+
269+
// Check the reference_to value from the raw JSON
270+
var globalFieldsArray = jsonResponse["global_fields"] as Newtonsoft.Json.Linq.JArray;
271+
var nestedGlobalFieldJson = globalFieldsArray?.FirstOrDefault(gf => gf["uid"]?.ToString() == "nested_global_field_test");
272+
var schemaArray = nestedGlobalFieldJson?["schema"] as Newtonsoft.Json.Linq.JArray;
273+
var globalFieldRefJson = schemaArray?.FirstOrDefault(f => f["uid"]?.ToString() == "global_field_reference");
274+
var referenceTo = globalFieldRefJson?["reference_to"]?.ToString();
275+
276+
Assert.AreEqual("referenced_global_field", referenceTo);
277+
}
278+
242279
[TestMethod]
243280
[DoNotParallelize]
244281
public void Test009_Should_Delete_Referenced_Global_Field()

Contentstack.Management.Core.Unit.Tests/Models/GlobalFieldTest.cs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using AutoFixture;
33
using Contentstack.Management.Core.Models;
44
using Contentstack.Management.Core.Queryable;
@@ -41,6 +41,23 @@ public void Initialize_GlobalField()
4141
Assert.AreEqual(globalField.Query().GetType(), typeof(Query));
4242
}
4343

44+
[TestMethod]
45+
public void Initialize_GlobalField_With_ApiVersion()
46+
{
47+
string apiVersion = "3.2";
48+
GlobalField globalField = new GlobalField(_stack, apiVersion: apiVersion);
49+
50+
Assert.IsNull(globalField.Uid);
51+
Assert.AreEqual($"/global_fields", globalField.resourcePath);
52+
Assert.ThrowsException<InvalidOperationException>(() => globalField.Fetch());
53+
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => globalField.FetchAsync());
54+
Assert.ThrowsException<InvalidOperationException>(() => globalField.Update(new ContentModelling()));
55+
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => globalField.UpdateAsync(new ContentModelling()));
56+
Assert.ThrowsException<InvalidOperationException>(() => globalField.Delete());
57+
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => globalField.DeleteAsync());
58+
Assert.AreEqual(globalField.Query().GetType(), typeof(Query));
59+
}
60+
4461
[TestMethod]
4562
public void Initialize_GlobalField_With_Uid()
4663
{
@@ -53,6 +70,20 @@ public void Initialize_GlobalField_With_Uid()
5370
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => globalField.CreateAsync(new ContentModelling()));
5471
Assert.ThrowsException<InvalidOperationException>(() => globalField.Query());
5572
}
73+
74+
[TestMethod]
75+
public void Initialize_GlobalField_With_Uid_And_ApiVersion()
76+
{
77+
string uid = _fixture.Create<string>();
78+
string apiVersion = "3.2";
79+
GlobalField globalField = new GlobalField(_stack, uid, apiVersion);
80+
81+
Assert.AreEqual(uid, globalField.Uid);
82+
Assert.AreEqual($"/global_fields/{globalField.Uid}", globalField.resourcePath);
83+
Assert.ThrowsException<InvalidOperationException>(() => globalField.Create(new ContentModelling()));
84+
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => globalField.CreateAsync(new ContentModelling()));
85+
Assert.ThrowsException<InvalidOperationException>(() => globalField.Query());
86+
}
5687
[TestMethod]
5788
public void Should_Create_Content_Type()
5889
{
@@ -80,6 +111,15 @@ public void Should_Query_Content_Type()
80111
Assert.AreEqual(_contentstackResponse.OpenJObjectResponse().ToString(), response.OpenJObjectResponse().ToString());
81112
}
82113

114+
[TestMethod]
115+
public void Should_Query_Content_Type_With_ApiVersion()
116+
{
117+
ContentstackResponse response = _stack.GlobalField(apiVersion: "3.2").Query().Find();
118+
119+
Assert.AreEqual(_contentstackResponse.OpenResponse(), response.OpenResponse());
120+
Assert.AreEqual(_contentstackResponse.OpenJObjectResponse().ToString(), response.OpenJObjectResponse().ToString());
121+
}
122+
83123
[TestMethod]
84124
public async System.Threading.Tasks.Task Should_Query_Content_Type_Async()
85125
{
@@ -89,6 +129,15 @@ public async System.Threading.Tasks.Task Should_Query_Content_Type_Async()
89129
Assert.AreEqual(_contentstackResponse.OpenJObjectResponse().ToString(), response.OpenJObjectResponse().ToString());
90130
}
91131

132+
[TestMethod]
133+
public async System.Threading.Tasks.Task Should_Query_Content_Type_Async_With_ApiVersion()
134+
{
135+
ContentstackResponse response = await _stack.GlobalField(apiVersion: "3.2").Query().FindAsync();
136+
137+
Assert.AreEqual(_contentstackResponse.OpenResponse(), response.OpenResponse());
138+
Assert.AreEqual(_contentstackResponse.OpenJObjectResponse().ToString(), response.OpenJObjectResponse().ToString());
139+
}
140+
92141
[TestMethod]
93142
public void Should_Fetch_Content_Type()
94143
{

Contentstack.Management.Core.Unit.Tests/Queryable/QueryTest.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ public void Initialize_Query()
4141
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => query.FindAsync());
4242
}
4343

44+
[TestMethod]
45+
public void Initialize_Query_With_ApiVersion()
46+
{
47+
string apiVersion = "3.2";
48+
Query query = new Query(new Stack(new ContentstackClient(authtoken: _fixture.Create<string>())), _fixture.Create<string>(), apiVersion);
49+
50+
Assert.ThrowsException<InvalidOperationException>(() => query.Find());
51+
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => query.FindAsync());
52+
}
53+
4454
[TestMethod]
4555
public void Query_Pagination_Parameters()
4656
{
@@ -49,5 +59,15 @@ public void Query_Pagination_Parameters()
4959
query.Skip(10);
5060
query.IncludeCount();
5161
}
62+
63+
[TestMethod]
64+
public void Query_Pagination_Parameters_With_ApiVersion()
65+
{
66+
string apiVersion = "3.2";
67+
Query query = new Query(_stack, _fixture.Create<string>(), apiVersion);
68+
query.Limit(10);
69+
query.Skip(10);
70+
query.IncludeCount();
71+
}
5272
}
5373
}

Contentstack.Management.Core/Models/Fields/Field.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class Field
2222
[JsonProperty(propertyName: "data_type")]
2323
public string DataType { get; set; }
2424
/// <summary>
25-
/// Allows you to enter additional data about a field. Also, you can add additional values under field_metadata.
25+
/// Allows you to enter additional data about a field. Also, you can add additional values under 'field_metadata'.
2626
/// </summary>
2727
[JsonProperty(propertyName: "field_metadata")]
2828
public FieldMetadata FieldMetadata { get; set; }

Contentstack.Management.Core/Models/GlobalField.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ namespace Contentstack.Management.Core.Models
55
{
66
public class GlobalField : BaseModel<ContentModelling>
77
{
8-
internal GlobalField(Stack stack, string uid = null)
8+
private readonly string apiVersion;
9+
10+
internal GlobalField(Stack stack, string uid = null, string apiVersion = null)
911
: base(stack, "global_field", uid)
1012
{
1113
resourcePath = uid == null ? "/global_fields" : $"/global_fields/{uid}";
14+
this.apiVersion = apiVersion;
1215
}
1316

1417
/// <summary>
@@ -24,7 +27,7 @@ internal GlobalField(Stack stack, string uid = null)
2427
public Query Query()
2528
{
2629
ThrowIfUidNotEmpty();
27-
return new Query(stack, resourcePath);
30+
return new Query(stack, resourcePath, apiVersion);
2831
}
2932

3033
/// <summary>

Contentstack.Management.Core/Models/Stack.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -643,24 +643,23 @@ public Asset Asset(string uid = null)
643643
}
644644

645645
/// <summary>
646-
/// A <see cref="Models.GlobalField" /> is a reusable field (or group of fields) that you can define once and reuse in any content type within your stack.
647-
/// This eliminates the need (and thereby time and efforts) to create the same set of fields repeatedly in multiple content types.
646+
/// <see cref="Models.GlobalField" /> defines the structure or schema of a page or a section of your web or mobile property. To create global Fields for your application, you are required to first create a global field. Read more about <a href='https://www.contentstack.com/docs/guide/global-fields'>Global Fields</a>.
648647
/// </summary>
649-
/// <param name="uid"> Optional global field uid.</param>
648+
/// <param name="uid">Optional, global field uid.</param>
649+
/// <param name="apiVersion">Optional, API version for nested global field support.</param>
650650
/// <example>
651651
/// <pre><code>
652652
/// ContentstackClient client = new ContentstackClient("<AUTHTOKEN>", "<API_HOST>");
653-
/// Stack stack = client.Stack("<API_KEY>");
654-
/// ContentstackResponse contentstackResponse = stack.GlobalField("<GLOBAL_FIELD_UID>").Fetch();
653+
/// ContentstackResponse contentstackResponse = client.Stack("<API_KEY>").GlobalField("<GLOBAL_FIELD_UID>").Fetch();
655654
/// </code></pre>
656655
/// </example>
657-
/// <returns>The <see cref="Models.GlobalField"/></returns>
658-
public GlobalField GlobalField(string uid = null)
656+
/// <returns>The <see cref="Models.GlobalField" /></returns>
657+
public GlobalField GlobalField(string uid = null, string apiVersion = null)
659658
{
660659
ThrowIfNotLoggedIn();
661660
ThrowIfAPIKeyEmpty();
662661

663-
return new GlobalField(this, uid);
662+
return new GlobalField(this, uid, apiVersion);
664663
}
665664

666665
/// <summary>

Contentstack.Management.Core/Queryable/Query.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ public class Query
1111
private readonly Stack _stack;
1212
private readonly string _resourcePath;
1313
private readonly ParameterCollection _collection = new ParameterCollection();
14-
internal Query(Stack stack, string resourcePath)
14+
private readonly string _apiVersion;
15+
16+
internal Query(Stack stack, string resourcePath, string apiVersion = null)
1517
{
1618
if(stack == null)
1719
{
@@ -24,6 +26,7 @@ internal Query(Stack stack, string resourcePath)
2426
}
2527
_stack = stack;
2628
_resourcePath = resourcePath;
29+
_apiVersion = apiVersion;
2730
}
2831
#region Public
2932
/// <summary>
@@ -93,7 +96,7 @@ public ContentstackResponse Find(ParameterCollection collection = null)
9396
}
9497

9598
var service = new QueryService(_stack, _collection, _resourcePath);
96-
return _stack.client.InvokeSync(service);
99+
return _stack.client.InvokeSync(service, false, _apiVersion);
97100
}
98101

99102
/// <summary>
@@ -113,7 +116,7 @@ public Task<ContentstackResponse> FindAsync(ParameterCollection collection = nul
113116
}
114117
var service = new QueryService(_stack, _collection, _resourcePath);
115118

116-
return _stack.client.InvokeAsync<QueryService, ContentstackResponse>(service);
119+
return _stack.client.InvokeAsync<QueryService, ContentstackResponse>(service, false, _apiVersion);
117120
}
118121
#endregion
119122
#region Throw Error

0 commit comments

Comments
 (0)