Skip to content

Fixed: send absolute/relative URL in location header, depending on options.UseRelativeLinks #1582

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 1 commit into from
Jun 30, 2024
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
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
Loading