Skip to content

Fixed: Paging links should not be rendered in POST response. #880

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
Nov 17, 2020
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
3 changes: 2 additions & 1 deletion src/JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ private static void SetupRequest(JsonApiRequest request, ResourceContext primary
}
}

request.IsCollection = request.PrimaryId == null || request.Relationship is HasManyAttribute;
var isGetAll = request.PrimaryId == null && request.IsReadOnly;
request.IsCollection = isGetAll || request.Relationship is HasManyAttribute;
}

private static string GetPrimaryRequestId(RouteValueDictionary routeValues)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public async Task Sets_location_header_for_created_resource()
var newWorkItemId = responseDocument.SingleData.Id;
httpResponse.Headers.Location.Should().Be("/workItems/" + newWorkItemId);

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

responseDocument.SingleData.Should().NotBeNull();
responseDocument.SingleData.Links.Self.Should().Be("http://localhost" + httpResponse.Headers.Location);
}
Expand Down
105 changes: 105 additions & 0 deletions test/UnitTests/Middleware/JsonApiRequestTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Middleware;
using JsonApiDotNetCoreExample.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;

namespace UnitTests.Middleware
{
public sealed class JsonApiRequestTests
{
[Theory]
[InlineData("GET", "/articles", true, EndpointKind.Primary, true)]
[InlineData("GET", "/articles/1", false, EndpointKind.Primary, true)]
[InlineData("GET", "/articles/1/author", false, EndpointKind.Secondary, true)]
[InlineData("GET", "/articles/1/revisions", true, EndpointKind.Secondary, true)]
[InlineData("GET", "/articles/1/relationships/author", false, EndpointKind.Relationship, true)]
[InlineData("GET", "/articles/1/relationships/revisions", true, EndpointKind.Relationship, true)]
[InlineData("POST", "/articles", false, EndpointKind.Primary, false)]
[InlineData("POST", "/articles/1/relationships/revisions", true, EndpointKind.Relationship, false)]
[InlineData("PATCH", "/articles/1", false, EndpointKind.Primary, false)]
[InlineData("PATCH", "/articles/1/relationships/author", false, EndpointKind.Relationship, false)]
[InlineData("PATCH", "/articles/1/relationships/revisions", true, EndpointKind.Relationship, false)]
[InlineData("DELETE", "/articles/1", false, EndpointKind.Primary, false)]
[InlineData("DELETE", "/articles/1/relationships/revisions", true, EndpointKind.Relationship, false)]
public async Task Sets_request_properties_correctly(string requestMethod, string requestPath, bool expectIsCollection, EndpointKind expectKind, bool expectIsReadOnly)
{
// Arrange
var options = new JsonApiOptions
{
UseRelativeLinks = true
};

var graphBuilder = new ResourceGraphBuilder(options, NullLoggerFactory.Instance);
graphBuilder.Add<Article>();
graphBuilder.Add<Author>();
graphBuilder.Add<Revision>();

var resourceGraph = graphBuilder.Build();

var controllerResourceMappingMock = new Mock<IControllerResourceMapping>();

controllerResourceMappingMock
.Setup(x => x.GetAssociatedResource(It.IsAny<string>()))
.Returns(typeof(Article));

var httpContext = new DefaultHttpContext();
SetupRoutes(httpContext, requestMethod, requestPath);

var request = new JsonApiRequest();

var middleware = new JsonApiMiddleware(_ => Task.CompletedTask);

// Act
await middleware.Invoke(httpContext, controllerResourceMappingMock.Object, options, request, resourceGraph);

// Assert
request.IsCollection.Should().Be(expectIsCollection);
request.Kind.Should().Be(expectKind);
request.IsReadOnly.Should().Be(expectIsReadOnly);
request.BasePath.Should().BeEmpty();
request.PrimaryResource.Should().NotBeNull();
request.PrimaryResource.PublicName.Should().Be("articles");
}

private static void SetupRoutes(HttpContext httpContext, string requestMethod, string requestPath)
{
httpContext.Request.Method = requestMethod;

var feature = new RouteValuesFeature
{
RouteValues =
{
["controller"] = "theController",
["action"] = "theAction"
}
};

var pathSegments = requestPath.Split("/", StringSplitOptions.RemoveEmptyEntries);
if (pathSegments.Length > 1)
{
feature.RouteValues["id"] = pathSegments[1];

if (pathSegments.Length >= 3)
{
feature.RouteValues["relationshipName"] = pathSegments.Last();
}
}

if (pathSegments.Contains("relationships"))
{
feature.RouteValues["action"] = "Relationship";
}

httpContext.Features.Set<IRouteValuesFeature>(feature);
httpContext.SetEndpoint(new Endpoint(null, new EndpointMetadataCollection(), null));
}
}
}