Skip to content

Commit

Permalink
Update reference resolve (#344)
Browse files Browse the repository at this point in the history
* 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 <kim.heurter@incentro.com>
  • Loading branch information
Roblinde and kheurterincentro authored Aug 9, 2024
1 parent 69e9357 commit 681f214
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 57 deletions.
10 changes: 5 additions & 5 deletions Contentful.AspNetCore/Contentful.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Description>Official .NET SDK for the Contentful Content Delivery and Management API for ASP.NET core.</Description>
<PackageId>contentful.aspnetcore</PackageId>
<NeutralLanguage>en-US</NeutralLanguage>
<VersionPrefix>7.7.0</VersionPrefix>
<VersionPrefix>8.0.0</VersionPrefix>
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>Contentful</Authors>
<Copyright>Contentful GmbH.</Copyright>
Expand All @@ -13,10 +13,10 @@
<PackageProjectUrl>https://github.com/contentful/contentful.net</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryType>git</RepositoryType>
<Version>7.7.0</Version>
<AssemblyVersion>7.7.0.0</AssemblyVersion>
<Version>8.0.0</Version>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<RepositoryUrl>https://github.com/contentful/contentful.net</RepositoryUrl>
<FileVersion>7.7.0.0</FileVersion>
<FileVersion>8.0.0.0</FileVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>bin\Release\netstandard1.5\Contentful.AspNetCore.xml</DocumentationFile>
Expand All @@ -25,7 +25,7 @@
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="contentful.csharp" Version="7.7.0" />
<PackageReference Include="contentful.csharp" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor" Version="2.2.0" />
Expand Down
13 changes: 1 addition & 12 deletions Contentful.Core.Tests/ContentfulClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,6 @@ public async Task GetEntriesShouldSerializeCorrectlyWihtMultipleLocales()

//Act
_client.ContentTypeResolver = new TestLocalizedResolver();
_client.ResolveEntriesSelectively = true;
var res = await _client.GetEntries<MainContainerLocalized>();
var list = res.ToList();

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

//Act
_client.ResolveEntriesSelectively = true;
var entries = await _client.GetEntries<ContentfulEvent>();
_client.ResolveEntriesSelectively = false;
var nulls = entries.Where(c => c.Image == null).ToList();

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

//Act
_client.ResolveEntriesSelectively = true;
var entries = await _client.GetEntries<IMarker>();
_client.ResolveEntriesSelectively = false;
var nulls = entries.Where(c => (c as ContentfulEvent).Image == null).ToList();

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

//Act
_client.ResolveEntriesSelectively = true;
var res = await _client.GetEntries<TestNestedSharedItem>();
_client.ResolveEntriesSelectively = false;

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

//Act
_client.ResolveEntriesSelectively = true;
var res = await _client.GetEntries<IMarker>();
_client.ResolveEntriesSelectively = false;

//Assert
Assert.Single(res);
Expand Down Expand Up @@ -1409,9 +1400,7 @@ public async Task ComplexStructureWithSelfReferenceInArrayIsDeserializedCorrectl
_handler.Response = GetResponseFromFile(@"EntriesCollectionWithSelfreference.json");

//Act
_client.ResolveEntriesSelectively = true;
var res = await _client.GetEntries<SelfReferencerInArray>();
_client.ResolveEntriesSelectively = false;

//Assert
Assert.Equal(5, res.Count());
Expand Down Expand Up @@ -1645,7 +1634,7 @@ public async Task TurningRichTextContentIntoHtmlShouldYieldCorrectResultWithSele
//Arrange
_handler.Response = GetResponseFromFile(@"EntriesCollectionWithRichTextField.json");
_client.ContentTypeResolver = new RichTextResolver();
_client.ResolveEntriesSelectively = true;

var htmlrenderer = new HtmlRenderer();
htmlrenderer.AddRenderer(new RichTextContentRenderer() { Order = 10 });
//Act
Expand Down
2 changes: 1 addition & 1 deletion Contentful.Core/Contentful.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PackageId>contentful.csharp</PackageId>
<AssemblyTitle>contentful.net</AssemblyTitle>
<NeutralLanguage>en-US</NeutralLanguage>
<VersionPrefix>7.7.0</VersionPrefix>
<VersionPrefix>8.0.0</VersionPrefix>
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>Contentful</Authors>
<Copyright>Contentful GmbH.</Copyright>
Expand Down
43 changes: 9 additions & 34 deletions Contentful.Core/ContentfulClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public ContentfulClient(HttpClient httpClient, ContentfulOptions options)
{
BaseUrl = BaseUrl.Replace("cdn", "preview");
}
ResolveEntriesSelectively = _options.ResolveEntriesSelectively;
SerializerSettings.Converters.Add(new AssetJsonConverter());
SerializerSettings.Converters.Add(new ContentJsonConverter());
SerializerSettings.TypeNameHandling = TypeNameHandling.All;
Expand Down Expand Up @@ -87,10 +86,7 @@ public ContentfulClient(HttpClient httpClient, string deliveryApiKey, string pre
/// </summary>
public IContentTypeResolver ContentTypeResolver { get; set; }

/// <summary>
/// If set the GetEntries methods will evaluate the class to serialize into and only serialize the parts that are part of the class structure.
/// </summary>
public bool ResolveEntriesSelectively { get; set; }


/// <summary>
/// Settings for the spaces to resolve cross space references from.
Expand Down Expand Up @@ -393,8 +389,13 @@ private void ResolveLinks(JObject json, JObject entryToken, ISet<string> process
//This could be due to the referenced entry being part of the original request (circular reference), so scan through that as well.
replacementToken = json.SelectTokens($"$.items.[?(@.sys.id=='{linkId}')]").FirstOrDefault();
}


}
else if (processedIds.Contains(linkId))
{
replacementToken = new JObject
{
["$ref"] = linkId
};
}

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

if (ResolveEntriesSelectively)
{
prop = type?.GetRuntimeProperties().FirstOrDefault(p => (p.Name.Equals(propName, StringComparison.OrdinalIgnoreCase) ||
p.GetCustomAttribute<JsonPropertyAttribute>()?.PropertyName == propName));
if (prop == null && linktype?.ToString() != "Asset")
{
//the property does not exist in the entry. Skip it in resolving references.
continue;
}
}

if (!processedIds.Contains(linkId))
if (!scopedIds.Contains(linkId))
{
Type propType = null;

if (ResolveEntriesSelectively)
{
propType = prop?.PropertyType;

if (propType != null && propType.IsArray)
{
propType = propType.GetElementType();
}
else if (propType != null && typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(propType.GetTypeInfo()) && propType.IsConstructedGenericType)
{
propType = propType.GetTypeInfo().GenericTypeArguments[0];
}
}

ResolveLinks(json, grandParent, processedIds, new HashSet<string>(scopedIds), propType);
}
}
Expand Down
5 changes: 0 additions & 5 deletions Contentful.Core/IContentfulClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ public interface IContentfulClient
/// </summary>
public JsonSerializer Serializer { get; }

/// <summary>
/// If set the GetEntries methods will evaluate the class to serialize into and only serialize the parts that are part of the class structure.
/// </summary>
bool ResolveEntriesSelectively { get; set; }

/// <summary>
/// Settings for the spaces to resolve cross space references from.
/// </summary>
Expand Down

0 comments on commit 681f214

Please sign in to comment.