diff --git a/LICENSE b/LICENSE index 9bab1d270d..c49362c460 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,5 @@ Copyright (c) 2017 Jared Nance +Copyright (c) 2020 Bart Koelman MIT License diff --git a/PackageReadme.md b/PackageReadme.md index a6d0017f6a..5bb1ac5342 100644 --- a/PackageReadme.md +++ b/PackageReadme.md @@ -1,5 +1,5 @@ -A framework for building [JSON:API](https://jsonapi.org/) compliant REST APIs using .NET Core and Entity Framework Core. Includes support for [Atomic Operations](https://jsonapi.org/ext/atomic/). +A framework for building [JSON:API](https://jsonapi.org/) compliant REST APIs using ASP.NET Core and Entity Framework Core. Includes support for the [Atomic Operations](https://jsonapi.org/ext/atomic/) extension. -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. +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, pagination, sparse fieldset selection, and side-loading related resources. 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. For more information, visit [www.jsonapi.net](https://www.jsonapi.net/). diff --git a/README.md b/README.md index d01f0b2dc7..af29c1a68f 100644 --- a/README.md +++ b/README.md @@ -1,73 +1,207 @@ # JsonApiDotNetCore -A framework for building [JSON:API](https://jsonapi.org/) compliant REST APIs using .NET Core and Entity Framework Core. Includes support for [Atomic Operations](https://jsonapi.org/ext/atomic/). [![Build](https://github.com/json-api-dotnet/JsonApiDotNetCore/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/json-api-dotnet/JsonApiDotNetCore/actions/workflows/build.yml?query=branch%3Amaster) [![Coverage](https://codecov.io/gh/json-api-dotnet/JsonApiDotNetCore/branch/master/graph/badge.svg?token=pn036tWV8T)](https://codecov.io/gh/json-api-dotnet/JsonApiDotNetCore) [![NuGet](https://img.shields.io/nuget/v/JsonApiDotNetCore.svg)](https://www.nuget.org/packages/JsonApiDotNetCore/) +[![GitHub License](https://img.shields.io/github/license/json-api-dotnet/JsonApiDotNetCore)](LICENSE) [![Chat](https://badges.gitter.im/json-api-dotnet-core/Lobby.svg)](https://gitter.im/json-api-dotnet-core/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![FIRST-TIMERS](https://img.shields.io/badge/first--timers--only-friendly-blue.svg)](https://www.firsttimersonly.com/) -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. +A framework for building [JSON:API](https://jsonapi.org/) compliant REST APIs using ASP.NET Core and Entity Framework Core. Includes support for the [Atomic Operations](https://jsonapi.org/ext/atomic/) extension. -## Getting Started +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, pagination, sparse fieldset selection, and side-loading related resources. 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. -These are some steps you can take to help you understand what this project is and how you can use it: +> [!NOTE] +> OpenAPI support is now [available](https://www.jsonapi.net/usage/openapi.html), currently in preview. Give it a try! -### About -- [What is JSON:API and why should I use it?](https://nordicapis.com/the-benefits-of-using-json-api/) (blog, 2017) -- [Pragmatic JSON:API Design](https://www.youtube.com/watch?v=3jBJOga4e2Y) (video, 2017) -- [JSON:API and JsonApiDotNetCore](https://www.youtube.com/watch?v=79Oq0HOxyeI) (video, 2021) -- [JsonApiDotNetCore Release 4.0](https://dev.to/wunki/getting-started-5dkl) (blog, 2020) -- [JSON:API, .Net Core, EmberJS](https://youtu.be/KAMuo6K7VcE) (video, 2017) -- [Embercasts: Full Stack Ember with ASP.NET Core](https://www.embercasts.com/course/full-stack-ember-with-dotnet/watch/whats-in-this-course-cs) (paid course, 2017) +## Getting started -### Official documentation -- [The JSON:API specification](https://jsonapi.org/format/) -- [JsonApiDotNetCore website](https://www.jsonapi.net/) -- [Roadmap](ROADMAP.md) +The following steps describe how to create a JSON:API project. -## Related Projects +1. Install the JsonApiDotNetCore package, along with your preferred Entity Framework Core provider: + ```bash + dotnet add package JsonApiDotNetCore + dotnet add package Microsoft.EntityFrameworkCore.Sqlite + ``` -- [Performance Reports](https://github.com/json-api-dotnet/PerformanceReports) -- [JsonApiDotNetCore.MongoDb](https://github.com/json-api-dotnet/JsonApiDotNetCore.MongoDb) -- [Ember.js Todo List App](https://github.com/json-api-dotnet/TodoListExample) +1. Declare your entities, annotated with JsonApiDotNetCore attributes: + ```c# + #nullable enable + + [Resource] + public class Person : Identifiable + { + [Attr] public string? FirstName { get; set; } + [Attr] public string LastName { get; set; } = null!; + [HasMany] public ISet Children { get; set; } = new HashSet(); + } + ``` -## Examples +1. Define your `DbContext`, seeding the database with sample data: + ```c# + public class AppDbContext(DbContextOptions options) : DbContext(options) + { + public DbSet People => Set(); + + protected override void OnConfiguring(DbContextOptionsBuilder builder) + { + builder.UseSqlite("Data Source=SampleDb.db"); + builder.UseAsyncSeeding(async (dbContext, _, cancellationToken) => + { + dbContext.Set().Add(new Person + { + FirstName = "John", + LastName = "Doe", + Children = + { + new Person + { + FirstName = "Baby", + LastName = "Doe" + } + } + }); + await dbContext.SaveChangesAsync(cancellationToken); + }); + } + } + ``` -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 JsonApiDotNetCore API and an EmberJs client. +1. Configure Entity Framework Core and JsonApiDotNetCore in `Program.cs`: + ```c# + var builder = WebApplication.CreateBuilder(args); + builder.Services.AddDbContext(); + builder.Services.AddJsonApi(options => + { + options.UseRelativeLinks = true; + options.IncludeTotalResourceCount = true; + }); + + var app = builder.Build(); + app.UseRouting(); + app.UseJsonApi(); + app.MapControllers(); + await CreateDatabaseAsync(app.Services); + app.Run(); + + static async Task CreateDatabaseAsync(IServiceProvider serviceProvider) + { + await using var scope = serviceProvider.CreateAsyncScope(); + var dbContext = scope.ServiceProvider.GetRequiredService(); + await dbContext.Database.EnsureDeletedAsync(); + await dbContext.Database.EnsureCreatedAsync(); + } + ``` -## Installation and Usage +1. Start your API + ```bash + dotnet run + ``` -See [our documentation](https://www.jsonapi.net/) for detailed usage. +1. Send a GET request to retrieve data: + ```bash + GET http://localhost:5000/people?filter=equals(firstName,'John')&include=children HTTP/1.1 + ``` -### Models +
+ Expand to view the JSON response + + ```json + { + "links": { + "self": "/people?filter=equals(firstName,%27John%27)&include=children", + "first": "/people?filter=equals(firstName,%27John%27)&include=children", + "last": "/people?filter=equals(firstName,%27John%27)&include=children" + }, + "data": [ + { + "type": "people", + "id": "1", + "attributes": { + "firstName": "John", + "lastName": "Doe" + }, + "relationships": { + "children": { + "links": { + "self": "/people/1/relationships/children", + "related": "/people/1/children" + }, + "data": [ + { + "type": "people", + "id": "2" + } + ] + } + }, + "links": { + "self": "/people/1" + } + } + ], + "included": [ + { + "type": "people", + "id": "2", + "attributes": { + "firstName": "Baby", + "lastName": "Doe" + }, + "relationships": { + "children": { + "links": { + "self": "/people/2/relationships/children", + "related": "/people/2/children" + } + } + }, + "links": { + "self": "/people/2" + } + } + ], + "meta": { + "total": 1 + } + } + ``` -```c# -#nullable enable +
-[Resource] -public class Article : Identifiable -{ - [Attr] - public string Name { get; set; } = null!; -} -``` +## Learn more -### Middleware +The following links explain what this project provides, why it exists, and how you can use it. -```c# -// Program.cs +### About -builder.Services.AddJsonApi(); +- [What is JSON:API and why should I use it?](https://nordicapis.com/the-benefits-of-using-json-api/) (blog, 2017) +- [Pragmatic JSON:API Design](https://www.youtube.com/watch?v=3jBJOga4e2Y) (video, 2017) +- [JSON:API and JsonApiDotNetCore](https://www.youtube.com/watch?v=79Oq0HOxyeI) (video, 2021) +- [JsonApiDotNetCore Release 4.0](https://dev.to/wunki/getting-started-5dkl) (blog, 2020) +- [JSON:API, ASP.NET Core, EmberJS](https://youtu.be/KAMuo6K7VcE) (video, 2017) +- [Embercasts: Full Stack Ember with ASP.NET Core](https://www.embercasts.com/course/full-stack-ember-with-dotnet/watch/whats-in-this-course-cs) (paid course, 2017) -// ... +### Official documentation -app.UseRouting(); -app.UseJsonApi(); -app.MapControllers(); -``` +- [JsonApiDotNetCore documentation](https://www.jsonapi.net/) +- [The JSON:API specification](https://jsonapi.org/format/) +- [JsonApiDotNetCore roadmap](ROADMAP.md) + +### Samples + +- The [examples](https://github.com/json-api-dotnet/JsonApiDotNetCore/tree/master/src/Examples) directory provides ready-to-run sample API projects +- Many advanced use cases are covered by integration tests, which can be found [here](https://github.com/json-api-dotnet/JsonApiDotNetCore/tree/master/test/JsonApiDotNetCoreTests/IntegrationTests). + This includes topics such as batching, multi-tenancy, authorization, soft-deletion, obfuscated IDs, resource inheritance, alternate routing, custom metadata, error handling and logging. +- The [Ember.js Todo List App](https://github.com/json-api-dotnet/TodoListExample) showcases a JsonApiDotNetCore API and an Ember.js client with token authentication. + +### Related projects + +- [JsonApiDotNetCore.MongoDb](https://github.com/json-api-dotnet/JsonApiDotNetCore.MongoDb) +- [Ember.js Todo List App](https://github.com/json-api-dotnet/TodoListExample) +- [Performance Reports](https://github.com/json-api-dotnet/PerformanceReports) ## Compatibility @@ -76,23 +210,21 @@ See also our [versioning policy](./VERSIONING_POLICY.md). | JsonApiDotNetCore | Status | .NET | Entity Framework Core | | ----------------- | ------------ | -------- | --------------------- | -| 3.x | Stable | Core 2.x | 2.x | -| 4.x | Stable | Core 3.1 | 3.1, 5 | -| | | 5 | 5 | -| | | 6 | 5 | -| 5.0.0-5.0.2 | Stable | 6 | 6 | -| 5.0.3-5.4.0 | Stable | 6 | 6, 7 | -| | | 7 | 7 | -| 5.5+ | Stable | 6 | 6, 7 | -| | | 7 | 7 | +| master | Preview | 9 | 9 | | | | 8 | 8, 9 | -| | | 9 | 9 | -| master | Preview | 8 | 8, 9 | -| | | 9 | 9 | - -## Contributing - -Have a question, found a bug or want to submit code changes? See our [contributing guidelines](./.github/CONTRIBUTING.md). +| 5.7.0+ | Stable | 9 | 9 | +| | | 8 | 8, 9 | +| 5.5.0-5.6.0 | Stable | 9 | 9 | +| | | 8 | 8, 9 | +| | | 7 | 7 | +| | | 6 | 6, 7 | +| 5.0.3-5.4.0 | Stable | 7 | 7 | +| | | 6 | 6, 7 | +| 5.0.0-5.0.2 | Stable | 6 | 6 | +| 4.x | Stable | 6 | 5 | +| | | 5 | 5 | +| | | Core 3.1 | 3.1, 5 | +| 3.x | Stable | Core 2.x | 2.x | ## Trying out the latest build @@ -115,7 +247,11 @@ To try it out, follow the steps below: and retry with the `--store-password-in-clear-text` switch added. 1. Restart your IDE, open your project, and browse the list of packages from the github-json-api feed (make sure pre-release packages are included). -## Development +## Contributing + +Have a question, found a bug or want to submit code changes? See our [contributing guidelines](./.github/CONTRIBUTING.md). + +## Build from source To build the code from this repository locally, run: @@ -123,7 +259,7 @@ To build the code from this repository locally, run: dotnet build ``` -Running tests locally requires access to a PostgreSQL database. If you have docker installed, this can be propped up via: +Running tests locally requires access to a PostgreSQL database. If you have docker installed, this can started via: ```bash pwsh run-docker-postgres.ps1 @@ -143,5 +279,9 @@ pwsh Build.ps1 ## Sponsors +We are very grateful to the sponsors below, who have provided us with a no-cost license for their tools. + JetBrains Logo   Araxis Logo + +Do you like this project? Consider to [sponsor](https://github.com/sponsors/json-api-dotnet), or just reward us by giving our repository a star. diff --git a/docs/home/index.html b/docs/home/index.html index e21d52ee9b..1593c915dd 100644 --- a/docs/home/index.html +++ b/docs/home/index.html @@ -4,7 +4,7 @@ JsonApiDotNetCore documentation - + @@ -12,7 +12,7 @@ - + @@ -51,8 +51,8 @@

JsonApiDotNetCore

- A framework for building JSON:API compliant REST APIs using .NET Core and Entity Framework Core. - Includes support for Atomic Operations. + A framework for building JSON:API compliant REST APIs using ASP.NET Core and Entity Framework Core. + Includes support for the Atomic Operations extension.

Read more Getting started @@ -82,7 +82,9 @@

Objectives

Eliminate boilerplate

-

We strive to eliminate as much boilerplate as possible by offering out-of-the-box features such as sorting, filtering and pagination.

+

+ 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, pagination, sparse fieldset selection, and side-loading related resources. +

@@ -112,7 +114,7 @@

Filtering

Sorting

-

Order resources on one or multiple attributes using the sort query string parameter

+

Order resources on multiple attributes using the sort query string parameter

@@ -223,14 +225,14 @@

Request

Response

-{
+{
   "meta": {
     "totalResources": 1
   },
   "links": {
-    "self": "/articles?filter=contains(summary,'web')&sort=-lastModifiedAt&fields%5Barticles%5D=title,summary&include=author",
-    "first": "/articles?filter=contains(summary,'web')&sort=-lastModifiedAt&fields%5Barticles%5D=title,summary&include=author",
-    "last": "/articles?filter=contains(summary,'web')&sort=-lastModifiedAt&fields%5Barticles%5D=title,summary&include=author"
+    "self": "/articles?filter=contains(summary,'web')&sort=-lastModifiedAt&fields%5Barticles%5D=title,summary&include=author",
+    "first": "/articles?filter=contains(summary,'web')&sort=-lastModifiedAt&fields%5Barticles%5D=title,summary&include=author",
+    "last": "/articles?filter=contains(summary,'web')&sort=-lastModifiedAt&fields%5Barticles%5D=title,summary&include=author"
   },
   "data": [
     {
diff --git a/src/JsonApiDotNetCore.Annotations/JsonApiDotNetCore.Annotations.csproj b/src/JsonApiDotNetCore.Annotations/JsonApiDotNetCore.Annotations.csproj
index 765c4231d7..ed36e0797c 100644
--- a/src/JsonApiDotNetCore.Annotations/JsonApiDotNetCore.Annotations.csproj
+++ b/src/JsonApiDotNetCore.Annotations/JsonApiDotNetCore.Annotations.csproj
@@ -10,7 +10,7 @@
 
   
     jsonapidotnetcore;jsonapi;json:api;dotnet;asp.net;rest;web-api
-    Annotations for JsonApiDotNetCore, a framework for building JSON:API compliant REST APIs using ASP.NET and Entity Framework Core.
+    Annotations for JsonApiDotNetCore, which is a framework for building JSON:API compliant REST APIs using ASP.NET Core and Entity Framework Core.
     json-api-dotnet
     https://www.jsonapi.net/
     MIT
diff --git a/src/JsonApiDotNetCore.OpenApi.Swashbuckle/JsonApiDotNetCore.OpenApi.Swashbuckle.csproj b/src/JsonApiDotNetCore.OpenApi.Swashbuckle/JsonApiDotNetCore.OpenApi.Swashbuckle.csproj
index 89822ff4b3..4a57ca1c85 100644
--- a/src/JsonApiDotNetCore.OpenApi.Swashbuckle/JsonApiDotNetCore.OpenApi.Swashbuckle.csproj
+++ b/src/JsonApiDotNetCore.OpenApi.Swashbuckle/JsonApiDotNetCore.OpenApi.Swashbuckle.csproj
@@ -11,7 +11,7 @@
   
     $(VersionPrefix)-preview.$(OpenApiPreviewNumber)
     jsonapidotnetcore;jsonapi;json:api;dotnet;asp.net;rest;web-api;openapi;swagger;swaggerui;swashbuckle
-    A Swashbuckle integration that enables you to describe a JsonApiDotNetCore API with an OpenAPI specification.
+    Provides OpenAPI document generation for JsonApiDotNetCore APIs by using Swashbuckle.
     json-api-dotnet
     https://www.jsonapi.net/
     MIT
diff --git a/src/JsonApiDotNetCore.SourceGenerators/JsonApiDotNetCore.SourceGenerators.csproj b/src/JsonApiDotNetCore.SourceGenerators/JsonApiDotNetCore.SourceGenerators.csproj
index 8f9e397df3..db6f039bd1 100644
--- a/src/JsonApiDotNetCore.SourceGenerators/JsonApiDotNetCore.SourceGenerators.csproj
+++ b/src/JsonApiDotNetCore.SourceGenerators/JsonApiDotNetCore.SourceGenerators.csproj
@@ -12,7 +12,7 @@
 
   
     jsonapidotnetcore;jsonapi;json:api;dotnet;asp.net;rest;web-api
-    Source generators for JsonApiDotNetCore, a framework for building JSON:API compliant REST APIs using ASP.NET and Entity Framework Core.
+    Source generators for JsonApiDotNetCore, which is a framework for building JSON:API compliant REST APIs using ASP.NET Core and Entity Framework Core.
     json-api-dotnet
     https://www.jsonapi.net/
     MIT
diff --git a/src/JsonApiDotNetCore/JsonApiDotNetCore.csproj b/src/JsonApiDotNetCore/JsonApiDotNetCore.csproj
index 1c1ba7ab7f..d36600e878 100644
--- a/src/JsonApiDotNetCore/JsonApiDotNetCore.csproj
+++ b/src/JsonApiDotNetCore/JsonApiDotNetCore.csproj
@@ -9,7 +9,7 @@
 
   
     jsonapidotnetcore;jsonapi;json:api;dotnet;asp.net;rest;web-api
-    A framework for building JSON:API compliant REST APIs using ASP.NET and Entity Framework Core. Includes support for Atomic Operations. 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.
+    A framework for building JSON:API compliant REST APIs using ASP.NET Core and Entity Framework Core. Includes support for the Atomic Operations extension. 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, pagination, sparse fieldset selection, and side-loading related resources. 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.
     json-api-dotnet
     https://www.jsonapi.net/
     MIT