Skip to content

Commit

Permalink
Add resolution for contentful urn entries
Browse files Browse the repository at this point in the history
  • Loading branch information
Roblinde committed Oct 26, 2023
1 parent 52ad271 commit c8f6620
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 16 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.4.1</VersionPrefix>
<VersionPrefix>7.4.2</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.4.1</Version>
<AssemblyVersion>7.4.1.0</AssemblyVersion>
<Version>7.4.2</Version>
<AssemblyVersion>7.4.2.0</AssemblyVersion>
<RepositoryUrl>https://github.com/contentful/contentful.net</RepositoryUrl>
<FileVersion>7.4.1.0</FileVersion>
<FileVersion>7.4.2.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.4.1" />
<PackageReference Include="contentful.csharp" Version="7.4.2" />
<PackageReference Include="gitlink" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand Down
2 changes: 1 addition & 1 deletion Contentful.Core.Tests/ClientTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class TestEntryModel : IMarker
public string ProductName { get; set; }
public string Slug { get; set; }
public string Title { get; set; }
public CrossSpaceReference Cross { get; set; }
public Author Cross { get; set; }
public string Metadata { get; set; }
[JsonProperty("$metadata")]
public ContentfulMetadata SystemMetadata { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions Contentful.Core.Tests/ContentfulClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public async Task GetEntriesShouldSerializeCorrectlyToAnEnumerableOfArbitraryTyp
//Assert
Assert.Equal(9, res.Count());
Assert.Equal("Home & Kitchen", res.First().Title);
Assert.Equal("crn:contentful:::content:spaces/537643esrtg/entries/435dfgsserte", res.First().Cross.Sys.Urn);
Assert.Equal("Mike Springer", res.First().Cross.Name);
}

[Fact]
Expand Down Expand Up @@ -435,7 +435,7 @@ public async Task GetEntriesShouldSerializeCorrectlyToAnEnumerableOfArbitraryTyp
Assert.Equal(9, res.Count());
Assert.Equal("eyJTcGFjZXMiOnsiMTIzIjoiNjY2IiwiMzMzIjoiOTk5IiwiZmRmZyI6ImdmNTY3OCJ9fQ==", crossRefHeader);
Assert.Equal("Home & Kitchen", res.First().Title);
Assert.Equal("crn:contentful:::content:spaces/537643esrtg/entries/435dfgsserte", res.First().Cross.Sys.Urn);
Assert.Equal("Mike Springer", res.First().Cross.Name);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,72 @@
}
],
"includes": {
"Entry": [
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SpaceId"
}
},
"id": "435dfgsserte",
"type": "Entry",
"createdAt": "2016-11-15T07:35:13.169Z",
"updatedAt": "2016-11-15T07:35:13.169Z",
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "ContentType2"
}
},
"locale": "en-US"
},
"metadata": {
"tags": [
{
"sys": {
"type": "Link",
"linkType": "Tag",
"id": "nyCampaign"
}
}
]
},
"fields": {
"name": "Mike Springer",
"website": "https://plus.google.com/+openculture/posts",
"metadata": "some data",
"profilePhoto": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "AssetId5"
}
},
"biography": "Mike Springer is a journalist living in Cambridge, Massachusetts, he writes daily for Open Culture.",
"createdEntries": [
{
"sys": {
"type": "Link",
"linkType": "Entry",
"id": "EntryId1"
}
}
],
"test": {
"sys": {
"type": "Link",
"linkType": "Entry",
"id": "Entry47"
}
}
}
}
],
"Asset": [
{
"sys": {
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.4.1</VersionPrefix>
<VersionPrefix>7.4.2</VersionPrefix>
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>Contentful</Authors>
<Copyright>Contentful GmbH.</Copyright>
Expand Down
26 changes: 19 additions & 7 deletions Contentful.Core/ContentfulClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,17 @@ private void ResolveLinks(JObject json, JObject entryToken, ISet<string> process
{
propName = propName.Substring(0, propName.IndexOf("["));
}
if(linkToken["type"]?.ToString() == "ResourceLink")
var linkId = "";
var linktype = linkToken["linkType"]?.ToString();
if (linkToken["type"]?.ToString() == "ResourceLink")
{
continue;
linkId = ((JValue)linkToken["urn"]).Value.ToString();
linkId = ParseIdFromContentfulUrn(linkId);
linktype = linktype.Contains("Entry") ? "Entry" : "Asset";
} else {
linkId = ((JValue)linkToken["id"]).Value.ToString();
}

var linkId = ((JValue)linkToken["id"]).Value.ToString();
JToken replacementToken = null;
if (processedIds.Contains(linkId))
{
Expand All @@ -369,9 +374,9 @@ private void ResolveLinks(JObject json, JObject entryToken, ISet<string> process
["$ref"] = linkId
};
}
else if (!string.IsNullOrEmpty(linkToken["linkType"]?.ToString()))
else if (!string.IsNullOrEmpty(linktype))
{
replacementToken = json.SelectTokens($"$.includes.{linkToken["linkType"]}[?(@.sys.id=='{linkId}')]").FirstOrDefault();
replacementToken = json.SelectTokens($"$.includes.{linktype}[?(@.sys.id=='{linkId}')]").FirstOrDefault();

if (replacementToken == null)
{
Expand All @@ -394,7 +399,7 @@ private void ResolveLinks(JObject json, JObject entryToken, ISet<string> process
{
prop = type?.GetRuntimeProperties().FirstOrDefault(p => (p.Name.Equals(propName, StringComparison.OrdinalIgnoreCase) ||
p.GetCustomAttribute<JsonPropertyAttribute>()?.PropertyName == propName));
if (prop == null && linkToken["linkType"]?.ToString() != "Asset")
if (prop == null && linktype?.ToString() != "Asset")
{
//the property does not exist in the entry. Skip it in resolving references.
continue;
Expand Down Expand Up @@ -451,7 +456,7 @@ private void ResolveLinks(JObject json, JObject entryToken, ISet<string> process
{
Type = "Link",
LinkType = itemToSkip.SelectToken("$..sys.linkType")?.Value<string>(),
Id = itemToSkip.SelectToken("$..sys.id")?.Value<string>()
Id = linkId
}
};

Expand Down Expand Up @@ -980,5 +985,12 @@ private async Task<HttpResponseMessage> Post(string url, object body, Cancellati
var bodyContent = new StringContent(bodyJson, Encoding.UTF8, "application/json");
return await SendHttpRequest(url, HttpMethod.Post, _options.UsePreviewApi ? _options.PreviewApiKey : _options.DeliveryApiKey, cancellationToken, bodyContent).ConfigureAwait(false);
}

private string ParseIdFromContentfulUrn(string s)
{
if (string.IsNullOrEmpty(s))
return s;
return s.Substring(s.LastIndexOf('/') + 1);
}
}
}

0 comments on commit c8f6620

Please sign in to comment.