Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added LinkType.Top enum, so we can specify only top level links #202

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Saule/LinkType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public enum LinkType
/// </summary>
Related = 2,

/// <summary>
/// Only self links in the top section
/// </summary>
TopSelf = 4,

/// <summary>
/// Generate all possible links
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion Saule/Serialization/ResourceSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ private JToken CreateTopLevelLinks(int count)
{
var result = new JObject();

if (_resource.LinkType.HasFlag(LinkType.Self))
// to preserve back compatibility if Self is enabled, then we also render it. Or if TopSelf is enabled
if (_resource.LinkType.HasFlag(LinkType.TopSelf) || _resource.LinkType.HasFlag(LinkType.Self))
{
result.Add("self", _baseUrl);
}
Expand Down
23 changes: 23 additions & 0 deletions Tests/Models/PersonOnlyTopLinksResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Saule;

namespace Tests.Models
{
public class PersonOnlyTopLinksResource : ApiResource
{
public PersonOnlyTopLinksResource()
{
WithId(nameof(Person.Identifier));
WithLinks(LinkType.TopSelf);

Attribute(nameof(Person.FirstName));
Attribute(nameof(Person.LastName));
Attribute(nameof(Person.Age));
Attribute(nameof(Person.Address));

BelongsTo<CompanyResource>(nameof(Person.Job), "/employer");
BelongsTo<CarResource>(nameof(Person.Car));
HasMany<PersonResource>(nameof(Person.Friends));
HasMany<PersonResource>( nameof( Person.FamilyMembers));
}
}
}
15 changes: 15 additions & 0 deletions Tests/Serialization/UrlConstructionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ public void SelfLink()
Assert.Equal("/api/people/123", selfLink);
}

[Fact(DisplayName = "Adds top level self link if only LinkType.TopSelf is specified")]
public void TopLinkOnlyLink()
{
var target = new ResourceSerializer(Get.People(1), new PersonOnlyTopLinksResource(),
GetUri("123"), DefaultPathBuilder, null, null, null);
var result = target.Serialize();
_output.WriteLine(result.ToString());

var selfLink = result["links"].Value<Uri>("self").AbsolutePath;

Assert.Equal("/api/people/123", selfLink);

Assert.Null(result["data"][0]["links"]);
}

[Fact(DisplayName = "Omits top level links if so requested")]
public void NoTopLevelLinks()
{
Expand Down
1 change: 1 addition & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
<Compile Include="Models\PersonJobOnlyRelatedLinksResource.cs" />
<Compile Include="Models\PersonJobOnlySelfLinksResource.cs" />
<Compile Include="Models\PersonNoJobLinksResource.cs" />
<Compile Include="Models\PersonOnlyTopLinksResource.cs" />
<Compile Include="Models\PersonNoLinksResource.cs" />
<Compile Include="Models\PersonWithCompanyWithCustomersResource.cs" />
<Compile Include="Models\PersonWithGuidAsRelationsResource.cs" />
Expand Down