-# JSON API .Net Core
+# JSON API .Net Core
-[](https://ci.appveyor.com/project/jaredcnance/jsonapidotnetcore)
-[](https://travis-ci.org/json-api-dotnet/JsonApiDotNetCore)
-[](https://www.nuget.org/packages/JsonApiDotNetCore/)
-[](https://gitter.im/json-api-dotnet-core/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
-[](http://www.firsttimersonly.com/)
+[](https://ci.appveyor.com/project/jaredcnance/jsonapidotnetcore) [](https://travis-ci.org/json-api-dotnet/JsonApiDotNetCore) [](https://www.nuget.org/packages/JsonApiDotNetCore/) [](https://gitter.im/json-api-dotnet-core/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [](http://www.firsttimersonly.com/)
A framework for building [json:api](http://jsonapi.org/) compliant web APIs. The ultimate goal of this library is to eliminate as much boilerplate as possible by offering out-of-the-box features such as sorting, filtering and pagination. You just need to focus on defining the resources and implementing your custom business logic. This library has been designed around dependency injection making extensibility incredibly easy.
+## Table of Contents
+- [Getting Started](#getting-started)
+- [Related Projects](#related-projects)
+- [Examples](#examples)
+- [Compatibility](#compatibility)
+- [Installation And Usage](#installation-and-usage)
+ - [Models](#models)
+ - [Controllers](#controllers)
+ - [Middleware](#middleware)
+- [Development](#development)
+ - [Testing](#testing)
+ - [Cleaning](#cleaning)
+
## Getting Started
These are some steps you can take to help you understand what this project is and how you can use it:
@@ -34,6 +43,15 @@ These are some steps you can take to help you understand what this project is an
See the [examples](https://github.com/json-api-dotnet/JsonApiDotNetCore/tree/master/src/Examples) directory for up-to-date sample applications. There is also a [Todo List App](https://github.com/json-api-dotnet/TodoListExample) that includes a JADNC API and an EmberJs client.
+## Compatibility
+
+A lot of changes were introduced in v4.0.0, the following chart should help you with compatibility issues between .NET Core versions
+
+| .NET Core Version | JADNC Version |
+| ----------------- | ------------- |
+| 2.* | v3.* |
+| 3.* | v4.* |
+
## Installation And Usage
See [the documentation](https://json-api-dotnet.github.io/#/) for detailed usage.
@@ -79,7 +97,7 @@ public class Startup
}
```
-### Development
+## Development
Restore all NuGet packages with:
@@ -109,13 +127,5 @@ Sometimes the compiled files can be dirty / corrupt from other branches / failed
dotnet clean
```
-## Compatibility
-
-A lot of changes were introduced in v4.0.0, the following chart should help you with compatibility issues between .NET Core versions
-
-| .NET Core Version | JADNC Version |
-| ----------------- | ------------- |
-| 2.* | v3.* |
-| 3.* | v4.* |
diff --git a/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs b/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs
index c241c5b81d..f44927ccef 100644
--- a/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs
+++ b/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs
@@ -1,3 +1,5 @@
+using System.Collections.Generic;
+using System.Linq;
using System.Threading.Tasks;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Extensions;
@@ -23,7 +25,7 @@ public class BaseJsonApiController
private readonly IDeleteService _delete;
private readonly ILogger> _logger;
private readonly IJsonApiOptions _jsonApiOptions;
-
+
public BaseJsonApiController(
IJsonApiOptions jsonApiOptions,
IResourceService resourceService,
@@ -101,37 +103,68 @@ public virtual async Task GetAsync()
public virtual async Task GetAsync(TId id)
{
- if (_getById == null) throw Exceptions.UnSupportedRequestMethod;
+ if (_getById == null)
+ {
+ throw Exceptions.UnSupportedRequestMethod;
+ }
var entity = await _getById.GetAsync(id);
if (entity == null)
{
- // remove the null argument as soon as this has been resolved:
- // https://github.com/aspnet/AspNetCore/issues/16969
- return NotFound(null);
+ return NoResultFound();
}
return Ok(entity);
}
- public virtual async Task GetRelationshipsAsync(TId id, string relationshipName)
+ private NotFoundObjectResult NoResultFound()
{
- if (_getRelationships == null)
- throw Exceptions.UnSupportedRequestMethod;
- var relationship = await _getRelationships.GetRelationshipsAsync(id, relationshipName);
- if (relationship == null)
- {
- // remove the null argument as soon as this has been resolved:
- // https://github.com/aspnet/AspNetCore/issues/16969
- return NotFound(null);
- }
+ // remove the null argument as soon as this has been resolved:
+ // https://github.com/aspnet/AspNetCore/issues/16969
+ return NotFound(null);
+ }
- return Ok(relationship);
+ public virtual async Task GetRelationshipsAsync(TId id, string relationshipName)
+ {
+ return await GetRelationshipInternal(id, relationshipName, relationshipInUrl: true);
}
public virtual async Task GetRelationshipAsync(TId id, string relationshipName)
{
- if (_getRelationship == null) throw Exceptions.UnSupportedRequestMethod;
- var relationship = await _getRelationship.GetRelationshipAsync(id, relationshipName);
+ return await GetRelationshipInternal(id, relationshipName, relationshipInUrl: false);
+ }
+
+ protected virtual async Task GetRelationshipInternal(TId id, string relationshipName, bool relationshipInUrl)
+ {
+
+ object relationship;
+ if (relationshipInUrl)
+ {
+ if (_getRelationships == null)
+ {
+ throw Exceptions.UnSupportedRequestMethod;
+ }
+ relationship = await _getRelationships.GetRelationshipsAsync(id, relationshipName);
+ }
+ else
+ {
+ if (_getRelationship == null)
+ {
+ throw Exceptions.UnSupportedRequestMethod;
+ }
+ relationship = await _getRelationship.GetRelationshipAsync(id, relationshipName);
+ }
+ if (relationship == null)
+ {
+ return Ok(relationship);
+ }
+
+ if (relationship.GetType() != typeof(T))
+ {
+ if (((IEnumerable