Skip to content

Commit b81f780

Browse files
committed
Fixed: send absolute/relative URL in location header, depending on options.UseRelativeLinks
1 parent 479066f commit b81f780

File tree

6 files changed

+22
-3
lines changed

6 files changed

+22
-3
lines changed

src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using JsonApiDotNetCore.Middleware;
44
using JsonApiDotNetCore.Resources;
55
using JsonApiDotNetCore.Services;
6+
using Microsoft.AspNetCore.Http;
7+
using Microsoft.AspNetCore.Http.Extensions;
68
using Microsoft.AspNetCore.Mvc;
79
using Microsoft.Extensions.Logging;
810

@@ -207,7 +209,7 @@ public virtual async Task<IActionResult> PostAsync([FromBody] TResource resource
207209
TResource? newResource = await _create.CreateAsync(resource, cancellationToken);
208210

209211
string resourceId = (newResource ?? resource).StringId!;
210-
string locationUrl = HttpContext.Request.Path.Add($"/{resourceId}");
212+
string locationUrl = GetLocationUrl(resourceId);
211213

212214
if (newResource == null)
213215
{
@@ -218,6 +220,15 @@ public virtual async Task<IActionResult> PostAsync([FromBody] TResource resource
218220
return Created(locationUrl, newResource);
219221
}
220222

223+
private string GetLocationUrl(string resourceId)
224+
{
225+
PathString locationPath = HttpContext.Request.Path.Add($"/{resourceId}");
226+
227+
return _options.UseRelativeLinks
228+
? UriHelper.BuildRelative(HttpContext.Request.PathBase, locationPath)
229+
: UriHelper.BuildAbsolute(HttpContext.Request.Scheme, HttpContext.Request.Host, HttpContext.Request.PathBase, locationPath);
230+
}
231+
221232
/// <summary>
222233
/// Adds resources to a to-many relationship. Example: <code><![CDATA[
223234
/// POST /articles/1/revisions HTTP/1.1

test/JsonApiDotNetCoreTests/IntegrationTests/Links/AbsoluteLinksWithNamespaceTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
394394
value.Links.Related.Should().Be($"{photoLink}/album");
395395
});
396396
});
397+
398+
httpResponse.Headers.Location.Should().Be(albumLink);
397399
}
398400

399401
[Fact]

test/JsonApiDotNetCoreTests/IntegrationTests/Links/AbsoluteLinksWithoutNamespaceTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
394394
value.Links.Related.Should().Be($"{photoLink}/album");
395395
});
396396
});
397+
398+
httpResponse.Headers.Location.Should().Be(albumLink);
397399
}
398400

399401
[Fact]

test/JsonApiDotNetCoreTests/IntegrationTests/Links/RelativeLinksWithNamespaceTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
394394
value.Links.Related.Should().Be($"{photoLink}/album");
395395
});
396396
});
397+
398+
httpResponse.Headers.Location.Should().Be(albumLink);
397399
}
398400

399401
[Fact]

test/JsonApiDotNetCoreTests/IntegrationTests/Links/RelativeLinksWithoutNamespaceTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
394394
value.Links.Related.Should().Be($"{photoLink}/album");
395395
});
396396
});
397+
398+
httpResponse.Headers.Location.Should().Be(albumLink);
397399
}
398400

399401
[Fact]

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Creating/CreateResourceTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ public async Task Sets_location_header_for_created_resource()
5858
httpResponse.ShouldHaveStatusCode(HttpStatusCode.Created);
5959

6060
string newWorkItemId = responseDocument.Data.SingleValue.ShouldNotBeNull().Id.ShouldNotBeNull();
61-
httpResponse.Headers.Location.Should().Be($"/workItems/{newWorkItemId}");
61+
httpResponse.Headers.Location.Should().Be($"http://localhost/workItems/{newWorkItemId}");
6262

6363
responseDocument.Links.ShouldNotBeNull();
6464
responseDocument.Links.Self.Should().Be("http://localhost/workItems/");
6565
responseDocument.Links.First.Should().BeNull();
6666

6767
responseDocument.Data.SingleValue.ShouldNotBeNull();
6868
responseDocument.Data.SingleValue.Links.ShouldNotBeNull();
69-
responseDocument.Data.SingleValue.Links.Self.Should().Be($"http://localhost{httpResponse.Headers.Location}");
69+
responseDocument.Data.SingleValue.Links.Self.Should().Be($"{httpResponse.Headers.Location}");
7070
}
7171

7272
[Fact]

0 commit comments

Comments
 (0)