Skip to content

Commit 681f214

Browse files
Update reference resolve (#344)
* Applied a fix that more accurately resolves references that appear in more than once place * Prefer scopedIds over processedIds and added a replacementToken if-else branch to support cross space references * Remove resolve entries through reflection * Update package version and dependency --------- Co-authored-by: Kim Heurter <[email protected]>
1 parent 69e9357 commit 681f214

File tree

5 files changed

+16
-57
lines changed

5 files changed

+16
-57
lines changed

Contentful.AspNetCore/Contentful.AspNetCore.csproj

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<Description>Official .NET SDK for the Contentful Content Delivery and Management API for ASP.NET core.</Description>
44
<PackageId>contentful.aspnetcore</PackageId>
55
<NeutralLanguage>en-US</NeutralLanguage>
6-
<VersionPrefix>7.7.0</VersionPrefix>
6+
<VersionPrefix>8.0.0</VersionPrefix>
77
<TargetFramework>netstandard2.0</TargetFramework>
88
<Authors>Contentful</Authors>
99
<Copyright>Contentful GmbH.</Copyright>
@@ -13,10 +13,10 @@
1313
<PackageProjectUrl>https://github.com/contentful/contentful.net</PackageProjectUrl>
1414
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1515
<RepositoryType>git</RepositoryType>
16-
<Version>7.7.0</Version>
17-
<AssemblyVersion>7.7.0.0</AssemblyVersion>
16+
<Version>8.0.0</Version>
17+
<AssemblyVersion>8.0.0.0</AssemblyVersion>
1818
<RepositoryUrl>https://github.com/contentful/contentful.net</RepositoryUrl>
19-
<FileVersion>7.7.0.0</FileVersion>
19+
<FileVersion>8.0.0.0</FileVersion>
2020
</PropertyGroup>
2121
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
2222
<DocumentationFile>bin\Release\netstandard1.5\Contentful.AspNetCore.xml</DocumentationFile>
@@ -25,7 +25,7 @@
2525
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
2626
</PropertyGroup>
2727
<ItemGroup>
28-
<PackageReference Include="contentful.csharp" Version="7.7.0" />
28+
<PackageReference Include="contentful.csharp" Version="8.0.0" />
2929
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
3030
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
3131
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor" Version="2.2.0" />

Contentful.Core.Tests/ContentfulClientTests.cs

+1-12
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,6 @@ public async Task GetEntriesShouldSerializeCorrectlyWihtMultipleLocales()
612612

613613
//Act
614614
_client.ContentTypeResolver = new TestLocalizedResolver();
615-
_client.ResolveEntriesSelectively = true;
616615
var res = await _client.GetEntries<MainContainerLocalized>();
617616
var list = res.ToList();
618617

@@ -1301,9 +1300,7 @@ public async Task AllAssetsInACollectionShouldDeserializeCorrectly()
13011300
_handler.Response = GetResponseFromFile(@"EntryCollectionLoopedReferences.json");
13021301

13031302
//Act
1304-
_client.ResolveEntriesSelectively = true;
13051303
var entries = await _client.GetEntries<ContentfulEvent>();
1306-
_client.ResolveEntriesSelectively = false;
13071304
var nulls = entries.Where(c => c.Image == null).ToList();
13081305

13091306
//Assert
@@ -1324,9 +1321,7 @@ public async Task AllAssetsInACollectionShouldDeserializeCorrectlyWithResolver()
13241321
_client.ContentTypeResolver = new TestResolver();
13251322

13261323
//Act
1327-
_client.ResolveEntriesSelectively = true;
13281324
var entries = await _client.GetEntries<IMarker>();
1329-
_client.ResolveEntriesSelectively = false;
13301325
var nulls = entries.Where(c => (c as ContentfulEvent).Image == null).ToList();
13311326

13321327
//Assert
@@ -1360,9 +1355,7 @@ public async Task ComplexStructureIsDeserializedCorrectly()
13601355
_handler.Response = GetResponseFromFile(@"NestedSharedStructure.json");
13611356

13621357
//Act
1363-
_client.ResolveEntriesSelectively = true;
13641358
var res = await _client.GetEntries<TestNestedSharedItem>();
1365-
_client.ResolveEntriesSelectively = false;
13661359

13671360
//Assert
13681361
Assert.Single(res);
@@ -1378,9 +1371,7 @@ public async Task ComplexStructureIsDeserializedCorrectlyWithResolver()
13781371
_client.ContentTypeResolver = new TestResolver();
13791372

13801373
//Act
1381-
_client.ResolveEntriesSelectively = true;
13821374
var res = await _client.GetEntries<IMarker>();
1383-
_client.ResolveEntriesSelectively = false;
13841375

13851376
//Assert
13861377
Assert.Single(res);
@@ -1409,9 +1400,7 @@ public async Task ComplexStructureWithSelfReferenceInArrayIsDeserializedCorrectl
14091400
_handler.Response = GetResponseFromFile(@"EntriesCollectionWithSelfreference.json");
14101401

14111402
//Act
1412-
_client.ResolveEntriesSelectively = true;
14131403
var res = await _client.GetEntries<SelfReferencerInArray>();
1414-
_client.ResolveEntriesSelectively = false;
14151404

14161405
//Assert
14171406
Assert.Equal(5, res.Count());
@@ -1645,7 +1634,7 @@ public async Task TurningRichTextContentIntoHtmlShouldYieldCorrectResultWithSele
16451634
//Arrange
16461635
_handler.Response = GetResponseFromFile(@"EntriesCollectionWithRichTextField.json");
16471636
_client.ContentTypeResolver = new RichTextResolver();
1648-
_client.ResolveEntriesSelectively = true;
1637+
16491638
var htmlrenderer = new HtmlRenderer();
16501639
htmlrenderer.AddRenderer(new RichTextContentRenderer() { Order = 10 });
16511640
//Act

Contentful.Core/Contentful.Core.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<PackageId>contentful.csharp</PackageId>
55
<AssemblyTitle>contentful.net</AssemblyTitle>
66
<NeutralLanguage>en-US</NeutralLanguage>
7-
<VersionPrefix>7.7.0</VersionPrefix>
7+
<VersionPrefix>8.0.0</VersionPrefix>
88
<TargetFramework>netstandard2.0</TargetFramework>
99
<Authors>Contentful</Authors>
1010
<Copyright>Contentful GmbH.</Copyright>

Contentful.Core/ContentfulClient.cs

+9-34
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public ContentfulClient(HttpClient httpClient, ContentfulOptions options)
4848
{
4949
BaseUrl = BaseUrl.Replace("cdn", "preview");
5050
}
51-
ResolveEntriesSelectively = _options.ResolveEntriesSelectively;
5251
SerializerSettings.Converters.Add(new AssetJsonConverter());
5352
SerializerSettings.Converters.Add(new ContentJsonConverter());
5453
SerializerSettings.TypeNameHandling = TypeNameHandling.All;
@@ -87,10 +86,7 @@ public ContentfulClient(HttpClient httpClient, string deliveryApiKey, string pre
8786
/// </summary>
8887
public IContentTypeResolver ContentTypeResolver { get; set; }
8988

90-
/// <summary>
91-
/// If set the GetEntries methods will evaluate the class to serialize into and only serialize the parts that are part of the class structure.
92-
/// </summary>
93-
public bool ResolveEntriesSelectively { get; set; }
89+
9490

9591
/// <summary>
9692
/// Settings for the spaces to resolve cross space references from.
@@ -393,8 +389,13 @@ private void ResolveLinks(JObject json, JObject entryToken, ISet<string> process
393389
//This could be due to the referenced entry being part of the original request (circular reference), so scan through that as well.
394390
replacementToken = json.SelectTokens($"$.items.[?(@.sys.id=='{linkId}')]").FirstOrDefault();
395391
}
396-
397-
392+
}
393+
else if (processedIds.Contains(linkId))
394+
{
395+
replacementToken = new JObject
396+
{
397+
["$ref"] = linkId
398+
};
398399
}
399400

400401
var grandParent = (JObject)linkToken.Parent.Parent;
@@ -403,37 +404,11 @@ private void ResolveLinks(JObject json, JObject entryToken, ISet<string> process
403404
{
404405
grandParent.RemoveAll();
405406
grandParent.Add(replacementToken.Children());
406-
PropertyInfo prop = null;
407407

408-
if (ResolveEntriesSelectively)
409-
{
410-
prop = type?.GetRuntimeProperties().FirstOrDefault(p => (p.Name.Equals(propName, StringComparison.OrdinalIgnoreCase) ||
411-
p.GetCustomAttribute<JsonPropertyAttribute>()?.PropertyName == propName));
412-
if (prop == null && linktype?.ToString() != "Asset")
413-
{
414-
//the property does not exist in the entry. Skip it in resolving references.
415-
continue;
416-
}
417-
}
418-
419-
if (!processedIds.Contains(linkId))
408+
if (!scopedIds.Contains(linkId))
420409
{
421410
Type propType = null;
422411

423-
if (ResolveEntriesSelectively)
424-
{
425-
propType = prop?.PropertyType;
426-
427-
if (propType != null && propType.IsArray)
428-
{
429-
propType = propType.GetElementType();
430-
}
431-
else if (propType != null && typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(propType.GetTypeInfo()) && propType.IsConstructedGenericType)
432-
{
433-
propType = propType.GetTypeInfo().GenericTypeArguments[0];
434-
}
435-
}
436-
437412
ResolveLinks(json, grandParent, processedIds, new HashSet<string>(scopedIds), propType);
438413
}
439414
}

Contentful.Core/IContentfulClient.cs

-5
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ public interface IContentfulClient
3232
/// </summary>
3333
public JsonSerializer Serializer { get; }
3434

35-
/// <summary>
36-
/// If set the GetEntries methods will evaluate the class to serialize into and only serialize the parts that are part of the class structure.
37-
/// </summary>
38-
bool ResolveEntriesSelectively { get; set; }
39-
4035
/// <summary>
4136
/// Settings for the spaces to resolve cross space references from.
4237
/// </summary>

0 commit comments

Comments
 (0)