Skip to content

Commit ac5b100

Browse files
author
Bart Koelman
authored
Documentation updates for v4 (#741)
* Documentation updates for v4
1 parent b8887c4 commit ac5b100

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+598
-538
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ See the [examples](https://github.com/json-api-dotnet/JsonApiDotNetCore/tree/mas
3636

3737
## Installation And Usage
3838

39-
See [the documentation](https://json-api-dotnet.github.io/#/) for detailed usage.
39+
See [the documentation](https://json-api-dotnet.github.io/#/) for detailed usage.
4040

4141
### Models
4242

@@ -56,7 +56,7 @@ public class ArticlesController : JsonApiController<Article>
5656
public ArticlesController(
5757
IJsonApiOptions jsonApiOptions,
5858
IResourceService<Article> resourceService,
59-
ILoggerFactory loggerFactory)
59+
ILoggerFactory loggerFactory)
6060
: base(jsonApiOptions, resourceService, loggerFactory)
6161
{ }
6262
}
@@ -65,7 +65,7 @@ public class ArticlesController : JsonApiController<Article>
6565
### Middleware
6666

6767
```csharp
68-
public class Startup
68+
public class Startup
6969
{
7070
public IServiceProvider ConfigureServices(IServiceCollection services) {
7171
services.AddJsonApi<AppDbContext>();
@@ -89,7 +89,7 @@ dotnet restore
8989

9090
#### Testing
9191

92-
Running tests locally requires access to a PostgreSQL database. If you have docker installed, this can be propped up via:
92+
Running tests locally requires access to a PostgreSQL database. If you have docker installed, this can be propped up via:
9393

9494
```bash
9595
docker run --rm --name jsonapi-dotnet-core-testing -e POSTGRES_DB=JsonApiDotNetCoreExample -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 postgres:12.0

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
```
44
./generate.sh
55
docfx ./docfx.json --serve
6-
```
6+
```

docs/api/index.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ This section documents the package API and is generated from the XML source comm
55
## Common APIs
66

77
- [`JsonApiOptions`](JsonApiDotNetCore.Configuration.JsonApiOptions.html)
8-
- [`IResourceGraph`](JsonApiDotNetCore.Internal.IResourceGraph.html)
8+
- [`IResourceGraph`](JsonApiDotNetCore.Internal.Contracts.IResourceGraph.html)
99
- [`ResourceDefinition<T>`](JsonApiDotNetCore.Models.ResourceDefinition-1.html)
10-
- [`IQueryAccessor`](JsonApiDotNetCore.Services.IQueryAccessor.html)

docs/generators/index.md

Lines changed: 0 additions & 9 deletions
This file was deleted.

docs/getting-started/install.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,3 @@ Install-Package JsonApiDotnetCore
1616
<PackageReference Include="JsonApiDotNetCore" Version="3.0.0" />
1717
</ItemGroup>
1818
```
19-
20-
## Pre-Release Packages
21-
22-
Occasionally we will release experimental features as pre-release packages on our
23-
MyGet feed. You can download these by adding [the pacakge feed](https://www.myget.org/feed/Details/research-institute) to your nuget configuration.
24-
25-
These releases are deployed from the `develop` branch directly.
26-
27-
```xml
28-
<?xml version="1.0" encoding="utf-8"?>
29-
<configuration>
30-
<packageSources>
31-
<add key="JADNC Pre-Release" value="https://www.myget.org/F/research-institute/api/v3/index.json" />
32-
</packageSources>
33-
</configuration>
34-
```

docs/getting-started/step-by-step.md

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Step-By-Step Guide to a Running API
22

3-
The most basic use case leverages Entity Framework.
3+
The most basic use case leverages Entity Framework Core.
44
The shortest path to a running API looks like:
55

66
- Create a new web app
@@ -33,45 +33,45 @@ Install-Package JsonApiDotnetCore
3333
```
3434

3535
### Define Models
36-
36+
3737
Define your domain models such that they implement `IIdentifiable<TId>`.
38-
The easiest way to do this is to inherit `Identifiable`
38+
The easiest way to do this is to inherit from `Identifiable`
3939

4040
```c#
4141
public class Person : Identifiable
42-
{
42+
{
4343
[Attr("name")]
4444
public string Name { get; set; }
4545
}
4646
```
4747

4848
### Define DbContext
49-
49+
5050
Nothing special here, just an ordinary `DbContext`
5151

5252
```
5353
public class AppDbContext : DbContext
5454
{
5555
public AppDbContext(DbContextOptions<AppDbContext> options)
5656
: base(options) { }
57-
57+
5858
public DbSet<Person> People { get; set; }
5959
}
6060
```
6161

6262
### Define Controllers
63-
64-
You need to create controllers that inherit from `JsonApiController<TEntity>` or `JsonApiController<TEntity, TId>`
65-
where `TEntity` is the model that inherits from `Identifiable<TId>`
63+
64+
You need to create controllers that inherit from `JsonApiController<T>` or `JsonApiController<T, TId>`
65+
where `T` is the model that inherits from `Identifiable<TId>`
6666

6767
```c#
6868
public class PeopleController : JsonApiController<Person>
6969
{
7070
public PeopleController(
71-
IJsonApiContext jsonApiContext,
72-
IResourceService<Person> resourceService,
73-
ILoggerFactory loggerFactory)
74-
: base(jsonApiContext, resourceService, loggerFactory)
71+
IJsonApiOptions jsonApiOptions,
72+
ILoggerFactory loggerFactory,
73+
IResourceService<Person> resourceService)
74+
: base(jsonApiOptions, loggerFactory, resourceService)
7575
{ }
7676
}
7777
```
@@ -81,24 +81,26 @@ public class PeopleController : JsonApiController<Person>
8181
Finally, add the services by adding the following to your Startup.ConfigureServices:
8282

8383
```c#
84-
public IServiceProvider ConfigureServices(IServiceCollection services)
84+
// This method gets called by the runtime. Use this method to add services to the container.
85+
public void ConfigureServices(IServiceCollection services)
8586
{
86-
// add the db context like you normally would
87+
// Add the Entity Framework Core DbContext like you normally would
8788
services.AddDbContext<AppDbContext>(options =>
88-
{ // use whatever provider you want, this is just an example
89+
{
90+
// Use whatever provider you want, this is just an example
8991
options.UseNpgsql(GetDbConnectionString());
90-
}, ServiceLifetime.Transient);
92+
});
9193

92-
// add jsonapi dotnet core
94+
// Add JsonApiDotNetCore
9395
services.AddJsonApi<AppDbContext>();
94-
// ...
9596
}
9697
```
9798

98-
Add the middleware to the Startup.Configure method. Note that under the hood,
99-
this will call `app.UseMvc()` so there is no need to add that as well.
99+
Add the middleware to the Startup.Configure method. Note that under the hood,
100+
this will call `app.UseRouting()` and `app.UseEndpoints(...)` so there is no need to add that as well.
100101

101102
```c#
103+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
102104
public void Configure(IApplicationBuilder app)
103105
{
104106
app.UseJsonApi();
@@ -110,19 +112,19 @@ public void Configure(IApplicationBuilder app)
110112
One way to seed the database is in your Configure method:
111113

112114
```c#
113-
public void Configure(
114-
IApplicationBuilder app,
115-
AppDbContext context)
115+
public void Configure(IApplicationBuilder app, AppDbContext context)
116116
{
117117
context.Database.EnsureCreated();
118-
if(context.People.Any() == false)
118+
119+
if (!context.People.Any())
119120
{
120-
context.People.Add(new Person {
121+
context.People.Add(new Person
122+
{
121123
Name = "John Doe"
122124
});
123125
context.SaveChanges();
124126
}
125-
// ...
127+
126128
app.UseJsonApi();
127129
}
128130
```

docs/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ A [{ json:api }](https://jsonapi.org) web application framework for .Net Core.
66

77
### 1. Eliminate Boilerplate
88

9-
The goal of this package is to facility the development of json:api applications that leverage the full range
9+
The goal of this package is to facility the development of json:api applications that leverage the full range
1010
of features provided by the specification.
1111

12-
Eliminate CRUD boilerplate and provide the following features across all your resource endpoints:
12+
Eliminate CRUD boilerplate and provide the following features across all your resource endpoints:
1313

1414
- Relationship inclusion and navigation
1515
- Filtering

docs/request-examples/000-CREATE_Person.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
curl -vs http://localhost:5001/api/people \
2-
-H "Accept: application/vnd.api+json" \
32
-H "Content-Type: application/vnd.api+json" \
43
-d '{
54
"data": {

docs/request-examples/001-CREATE_Article.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
curl -vs http://localhost:5001/api/articles \
2-
-H "Accept: application/vnd.api+json" \
32
-H "Content-Type: application/vnd.api+json" \
43
-d '{
54
"data": {
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
curl -vs http://localhost:5001/api/articles \
2-
-H "Accept: application/vnd.api+json"
1+
curl -vs http://localhost:5001/api/articles
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
curl -vs http://localhost:5001/api/articles/1 \
2-
-H "Accept: application/vnd.api+json"
1+
curl -vs http://localhost:5001/api/articles/1
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
curl -vs http://localhost:5001/api/articles?include=author \
2-
-H "Accept: application/vnd.api+json"
1+
curl -vs http://localhost:5001/api/articles?include=author

docs/request-examples/005-PATCH_Article.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
curl -vs http://localhost:5001/api/people/1 \
2-
-H "Accept: application/vnd.api+json" \
1+
curl -vs http://localhost:5001/api/people/1 \
32
-H "Content-Type: application/vnd.api+json" \
43
-X PATCH \
54
-d '{
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
curl -vs http://localhost:5001/api/articles/1 \
2-
-H "Accept: application/vnd.api+json" \
32
-X DELETE

docs/request-examples/007-__SEED__.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
curl -vs http://localhost:5001/api/people \
2-
-H "Accept: application/vnd.api+json" \
32
-H "Content-Type: application/vnd.api+json" \
43
-d '{
54
"data": {
@@ -11,7 +10,6 @@ curl -vs http://localhost:5001/api/people \
1110
}'
1211

1312
curl -vs http://localhost:5001/api/articles \
14-
-H "Accept: application/vnd.api+json" \
1513
-H "Content-Type: application/vnd.api+json" \
1614
-d '{
1715
"data": {
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
curl -vs http://localhost:5001/api/articles?filter%5Btitle%5D=Moby \
2-
-H "Accept: application/vnd.api+json"
1+
curl -vs http://localhost:5001/api/articles?filter%5Btitle%5D=Moby
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
curl -vs http://localhost:5001/api/people?filter%5Bname%5D=like:Al \
2-
-H "Accept: application/vnd.api+json"
1+
curl -vs http://localhost:5001/api/people?filter%5Bname%5D=like:Al

docs/request-examples/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ To update these requests:
44

55
1. Add a bash (.sh) file prefixed by a number that is used to determine the order the scripts are executed. The bash script should execute a request and output the response. Example:
66
```
7-
curl -vs http://localhost:5001/api/articles \
8-
-H "Accept: application/vnd.api+json"
7+
curl -vs http://localhost:5001/api/articles
98
```
109

1110
2. Add the example to `index.md`. Example:

docs/usage/errors.md

Lines changed: 14 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,25 @@
11
# Errors
22

3-
By default, errors will only contain the properties defined by the `Error` class.
4-
However, you can create your own by inheriting from Error and either throwing it in a `JsonApiException` or returning the error from your controller.
3+
Errors returned will contain only the properties that are set on the `Error` class. Custom fields can be added through `Error.Meta`.
4+
You can create a custom error by throwing a `JsonApiException` (which accepts an `Error` instance), or returning an `Error` instance from an `ActionResult` in a controller.
5+
Please keep in mind that json:api requires Title to be a generic message, while Detail should contain information about the specific problem occurence.
56

7+
From a controller method:
68
```c#
7-
public class CustomError : Error
9+
return Conflict(new Error(HttpStatusCode.Conflict)
810
{
9-
public CustomError(int status, string title, string detail, string myProp)
10-
: base(status, title, detail)
11-
{
12-
MyCustomProperty = myProp;
13-
}
14-
15-
public string MyCustomProperty { get; set; }
16-
}
11+
Title = "Target resource was modified by another user.",
12+
Detail = $"User {userName} changed the {resourceField} field on the {resourceName} resource."
13+
});
1714
```
1815

19-
If you throw a `JsonApiException` that is unhandled, the middleware will properly serialize it and return it as a json:api error.
20-
16+
From other code:
2117
```c#
22-
public void MyMethod()
18+
throw new JsonApiException(new Error(HttpStatusCode.Conflict)
2319
{
24-
var error = new CustomError(507, "title", "detail", "custom");
25-
throw new JsonApiException(error);
26-
}
20+
Title = "Target resource was modified by another user.",
21+
Detail = $"User {userName} changed the {resourceField} field on the {resourceName} resource."
22+
});
2723
```
2824

29-
You can use the `IActionResult Error(Error error)` method to return a single error message, or you can use the `IActionResult Errors(ErrorCollection errors)` method to return a collection of errors from your controller.
30-
31-
```c#
32-
[HttpPost]
33-
public override async Task<IActionResult> PostAsync([FromBody] MyEntity entity)
34-
{
35-
if(_db.IsFull)
36-
return Error(new CustomError("507", "Database is full.", "Theres no more room.", "Sorry."));
37-
38-
if(model.Validations.IsValid == false)
39-
return Errors(model.Validations.GetErrors());
40-
}
41-
```
42-
43-
## Example: Including Links
44-
45-
This example demonstrates one way you can include links with your error payloads.
46-
47-
This example assumes that there is a support documentation site that provides additional information based on the HTTP Status Code.
48-
49-
```c#
50-
public class LinkableError : Error
51-
{
52-
public LinkableError(int status, string title)
53-
: base(status, title)
54-
{ }
55-
56-
public ErrorLink Links => "https://example.com/errors/" + Status;
57-
}
58-
59-
var error = new LinkableError(401, "You're not allowed to do that.");
60-
throw new JsonApiException(error);
61-
```
62-
63-
64-
65-
66-
67-
25+
In both cases, the middleware will properly serialize it and return it as a json:api error.

0 commit comments

Comments
 (0)