Skip to content

Commit

Permalink
Fixed: send absolute/relative URL in location header, depending on op…
Browse files Browse the repository at this point in the history
…tions.UseRelativeLinks
  • Loading branch information
bkoelman committed Jun 30, 2024
1 parent 479066f commit b81f780
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using JsonApiDotNetCore.Middleware;
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCore.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

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

string resourceId = (newResource ?? resource).StringId!;
string locationUrl = HttpContext.Request.Path.Add($"/{resourceId}");
string locationUrl = GetLocationUrl(resourceId);

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

private string GetLocationUrl(string resourceId)
{
PathString locationPath = HttpContext.Request.Path.Add($"/{resourceId}");

return _options.UseRelativeLinks
? UriHelper.BuildRelative(HttpContext.Request.PathBase, locationPath)
: UriHelper.BuildAbsolute(HttpContext.Request.Scheme, HttpContext.Request.Host, HttpContext.Request.PathBase, locationPath);
}

/// <summary>
/// Adds resources to a to-many relationship. Example: <code><![CDATA[
/// POST /articles/1/revisions HTTP/1.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
value.Links.Related.Should().Be($"{photoLink}/album");
});
});

httpResponse.Headers.Location.Should().Be(albumLink);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
value.Links.Related.Should().Be($"{photoLink}/album");
});
});

httpResponse.Headers.Location.Should().Be(albumLink);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
value.Links.Related.Should().Be($"{photoLink}/album");
});
});

httpResponse.Headers.Location.Should().Be(albumLink);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
value.Links.Related.Should().Be($"{photoLink}/album");
});
});

httpResponse.Headers.Location.Should().Be(albumLink);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ public async Task Sets_location_header_for_created_resource()
httpResponse.ShouldHaveStatusCode(HttpStatusCode.Created);

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

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

responseDocument.Data.SingleValue.ShouldNotBeNull();
responseDocument.Data.SingleValue.Links.ShouldNotBeNull();
responseDocument.Data.SingleValue.Links.Self.Should().Be($"http://localhost{httpResponse.Headers.Location}");
responseDocument.Data.SingleValue.Links.Self.Should().Be($"{httpResponse.Headers.Location}");
}

[Fact]
Expand Down

0 comments on commit b81f780

Please sign in to comment.