Skip to content

Generic resource definitions #832

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 8 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
50 changes: 32 additions & 18 deletions docs/usage/meta.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,60 @@
# Metadata

Non-standard metadata can be added to your API responses in two ways: Resource and Request Meta. In the event of a key collision, the Request Meta will take precendence.
Top-level custom metadata can be added to your API responses in two ways: globally and per resource type. In the event of a key collision, the resource meta will take precendence.

## Resource Meta
## Global Meta

Resource Meta is metadata defined on the resource itself by implementing the `IHasMeta` interface.
Global metadata can be added by injecting a service that implements `IResponseMeta`.
This is useful if you need access to other injected services to build the meta object.

```c#
public class Person : Identifiable, IHasMeta
public class ResponseMetaService : IResponseMeta
{
public ResponseMetaService(/*...other dependencies here */) {
// ...
}

public Dictionary<string, object> GetMeta()
{
return new Dictionary<string, object>
{
{"copyright", "Copyright 2018 Example Corp."},
{"authors", new[] {"John Doe"}}
{"authors", new string[] {"John Doe"}}
};
}
}
```

## Request Meta
```json
{
"meta": {
"copyright": "Copyright 2018 Example Corp.",
"authors": [
"John Doe"
]
},
"data": {
// ...
}
}
```

Request Meta can be added by injecting a service that implements `IRequestMeta`.
This is useful if you need access to other injected services to build the meta object.
## Resource Meta

Resource-specific metadata can be added by implementing `IResourceDefinition<TResource, TId>.GetMeta` (or overriding it on `JsonApiResourceDefinition`):

```c#
public class RequestMetaService : IRequestMeta
public class PersonDefinition : JsonApiResourceDefinition<Person>
{
public RequestMetaService(/*...other dependencies here */) {
// ...
public PersonDefinition(IResourceGraph resourceGraph) : base(resourceGraph)
{
}

public Dictionary<string, object> GetMeta()
public override IReadOnlyDictionary<string, object> GetMeta()
{
return new Dictionary<string, object>
{
{"copyright", "Copyright 2018 Example Corp."},
{"authors", new string[] {"John Doe"}}
["notice"] = "Check our intranet at http://www.example.com for personal details."
};
}
}
Expand All @@ -46,10 +63,7 @@ public class RequestMetaService : IRequestMeta
```json
{
"meta": {
"copyright": "Copyright 2018 Example Corp.",
"authors": [
"John Doe"
]
"notice": "Check our intranet at http://www.example.com for personal details."
},
"data": {
// ...
Expand Down
12 changes: 5 additions & 7 deletions src/JsonApiDotNetCore/Serialization/Building/MetaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ public class MetaBuilder<TResource> : IMetaBuilder<TResource> where TResource :
private readonly IPaginationContext _paginationContext;
private readonly IJsonApiOptions _options;
private readonly IResourceDefinitionAccessor _resourceDefinitionAccessor;
private readonly IRequestMeta _requestMeta;
private readonly IResponseMeta _responseMeta;

private Dictionary<string, object> _meta = new Dictionary<string, object>();

public MetaBuilder(IPaginationContext paginationContext, IJsonApiOptions options, IResourceDefinitionAccessor resourceDefinitionAccessor, IRequestMeta requestMeta = null)
public MetaBuilder(IPaginationContext paginationContext, IJsonApiOptions options, IResourceDefinitionAccessor resourceDefinitionAccessor, IResponseMeta responseMeta = null)
{
_paginationContext = paginationContext ?? throw new ArgumentNullException(nameof(paginationContext));
_options = options ?? throw new ArgumentNullException(nameof(options));
_resourceDefinitionAccessor = resourceDefinitionAccessor ?? throw new ArgumentNullException(nameof(resourceDefinitionAccessor));
_requestMeta = requestMeta;
_responseMeta = responseMeta;
}

/// <inheritdoc />
Expand Down Expand Up @@ -54,13 +54,11 @@ public IDictionary<string, object> GetMeta()
_meta.Add(key, _paginationContext.TotalResourceCount);
}

if (_requestMeta != null)
if (_responseMeta != null)
{
Add(_requestMeta.GetMeta());
Add(_responseMeta.GetMeta());
}

// TODO: This looks wrong. We should be adding resource-level meta to each individual resource, instead of once at the top.

var resourceMeta = _resourceDefinitionAccessor.GetMeta(typeof(TResource));
if (resourceMeta != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace JsonApiDotNetCore.Serialization
{
/// <summary>
/// Service to add global top-level metadata to a <see cref="Document"/>.
/// Service to add global top-level json:api meta to a response <see cref="Document"/>.
/// Use <see cref="IResourceDefinition{TResource,TId}.GetMeta"/> to specify top-level metadata per resource type.
/// </summary>
public interface IRequestMeta
public interface IResponseMeta
{
IReadOnlyDictionary<string, object> GetMeta();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public RequestMetaTests(IntegrationTestContext<Startup, AppDbContext> testContex

testContext.ConfigureServicesBeforeStartup(services =>
{
services.AddScoped<IRequestMeta, TestRequestMeta>();
services.AddScoped<IResponseMeta, TestResponseMeta>();
});
}

[Fact]
public async Task Injecting_IRequestMeta_Adds_Meta_Data()
public async Task Injecting_IResponseMeta_Adds_Meta_Data()
{
// Arrange
var route = "/api/v1/people";
Expand All @@ -43,7 +43,7 @@ public async Task Injecting_IRequestMeta_Adds_Meta_Data()
}
}

public sealed class TestRequestMeta : IRequestMeta
public sealed class TestResponseMeta : IResponseMeta
{
public IReadOnlyDictionary<string, object> GetMeta()
{
Expand Down