Skip to content

v2.1.4 Relative Links #159

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

Merged
merged 7 commits into from
Sep 17, 2017
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
36 changes: 36 additions & 0 deletions docs/Options.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,39 @@ public IServiceProvider ConfigureServices(IServiceCollection services) {
// ...
}
```

## Relative Links

All links are absolute by default. However, you can configure relative links:

```csharp
public IServiceProvider ConfigureServices(IServiceCollection services) {
services.AddJsonApi<AppDbContext>(
opt => opt.RelativeLinks = true);
// ...
}
```


```http
GET /api/v1/articles/4309 HTTP/1.1
Accept: application/vnd.api+json
```

```json
{
"type": "articles",
"id": "4309",
"attributes": {
"name": "Voluptas iure est molestias."
},
"relationships": {
"author": {
"links": {
"self": "/api/v1/articles/4309/relationships/author",
"related": "/api/v1/articles/4309/author"
}
}
}
}
```
14 changes: 8 additions & 6 deletions src/JsonApiDotNetCore/Builders/LinkBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,30 @@ public class LinkBuilder

public LinkBuilder(IJsonApiContext context)
{
_context = context;
_context = context;
}

public string GetBasePath(HttpContext context, string entityName)
{
var r = context.Request;
return $"{r.Scheme}://{r.Host}{GetNamespaceFromPath(r.Path, entityName)}";
return (_context.Options.RelativeLinks)
? $"{GetNamespaceFromPath(r.Path, entityName)}"
: $"{r.Scheme}://{r.Host}{GetNamespaceFromPath(r.Path, entityName)}";
}

private string GetNamespaceFromPath(string path, string entityName)
{
var nSpace = string.Empty;
var segments = path.Split('/');
for(var i = 1; i < segments.Length; i++)

for (var i = 1; i < segments.Length; i++)
{
if(segments[i].ToLower() == entityName)
if (segments[i].ToLower() == entityName)
break;

nSpace += $"/{segments[i]}";
}

return nSpace;
}

Expand Down
1 change: 1 addition & 0 deletions src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class JsonApiOptions
public bool IncludeTotalRecordCount { get; set; }
public bool AllowClientGeneratedIds { get; set; }
public IContextGraph ContextGraph { get; set; }
public bool RelativeLinks { get; set; }
public IContractResolver JsonContractResolver { get; set; } = new DasherizedResolver();
internal IContextGraphBuilder ContextGraphBuilder { get; } = new ContextGraphBuilder();

Expand Down
2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/JsonApiDotNetCore.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>2.1.3</VersionPrefix>
<VersionPrefix>2.1.4</VersionPrefix>
<TargetFrameworks>netstandard1.6</TargetFrameworks>
<AssemblyName>JsonApiDotNetCore</AssemblyName>
<PackageId>JsonApiDotNetCore</PackageId>
Expand Down
48 changes: 48 additions & 0 deletions test/UnitTests/Builders/LinkBuilder_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using JsonApiDotNetCore.Builders;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Services;
using Microsoft.AspNetCore.Http;
using Moq;
using Xunit;

namespace UnitTests
{
public class LinkBuilder_Tests
{
[Theory]
[InlineData("http", "localhost", "/api/v1/articles", false, "http://localhost/api/v1")]
[InlineData("https", "localhost", "/api/v1/articles", false, "https://localhost/api/v1")]
[InlineData("http", "example.com", "/api/v1/articles", false, "http://example.com/api/v1")]
[InlineData("https", "example.com", "/api/v1/articles", false, "https://example.com/api/v1")]
[InlineData("https", "example.com", "/articles", false, "https://example.com")]
[InlineData("https", "example.com", "/articles", true, "")]
[InlineData("https", "example.com", "/api/v1/articles", true, "/api/v1")]
public void GetBasePath_Returns_Path_Before_Resource(string scheme,
string host, string path, bool isRelative, string expectedPath)
{
// arrange
const string resource = "articles";
var jsonApiContextMock = new Mock<IJsonApiContext>();
jsonApiContextMock.Setup(m => m.Options).Returns(new JsonApiOptions
{
RelativeLinks = isRelative
});

var requestMock = new Mock<HttpRequest>();
requestMock.Setup(m => m.Scheme).Returns(scheme);
requestMock.Setup(m => m.Host).Returns(new HostString(host));
requestMock.Setup(m => m.Path).Returns(new PathString(path));

var contextMock = new Mock<HttpContext>();
contextMock.Setup(m => m.Request).Returns(requestMock.Object);

var linkBuilder = new LinkBuilder(jsonApiContextMock.Object);

// act
var basePath = linkBuilder.GetBasePath(contextMock.Object, resource);

// assert
Assert.Equal(expectedPath, basePath);
}
}
}