Global resource-specific metadata
See original GitHub issueDESCRIPTION
As of today, there are two ways to define metadata. Either global (once per response) or per resource (for each item in the result the metadata is added).
If we add the metadata per resource we can easily gather additional information about the entity by accessing the generic type information via reflection. Unfortunately this seems not possible if using the global metadata, because the interface IResponseMeta
is lacking the type information.
ACTUAL BEHAVIOR
Consider the following entity, which contains additional attributes which are used to generate meta data.
public sealed class SupportTicket : Identifiable
{
[Attr]
[Required]
[MaxLength(200)]
public string Description { get; set; }
}
The only way to include those metadata is by creating a ResourceDefinition inherit from JsonApiResourceDefinition<SupportTicket>
which then allows us to override IDictionary<string, object> GetMeta(SupportTicket resource)
. This allows us to create something like this:
{
"links": {
"self": "http://localhost/supportTickets",
"first": "http://localhost/supportTickets"
},
"data": [
{
"type": "supportTickets",
"id": "1",
"attributes": {
"description": "Critical: Quae magnam est modi quaerat voluptas repellendus quos dolorem. Ipsum rerum voluptatem facere. Veniam dolor soluta repudiandae doloribus. Qui ratione soluta velit deleniti atque id ad dicta sint."
},
"links": {
"self": "http://localhost/supportTickets/1"
},
"meta": {
"rules": [
"required": "true",
"maxLength": "200"
]
}
}, {
"type": "supportTickets",
"id": "2",
"attributes": {
"description": "Lorem ipsum dolor sit....."
},
"links": {
"self": "http://localhost/supportTickets/2"
},
"meta": {
"rules": [
"required": "true",
"maxLength": "200"
]
}
}
]
}
Even though this generally works, the data sent to the client is highly redundant.
EXPECTED BEHAVIOR
We need a way to create entity related global meta data which leads to the following result:
{
"links": {
"self": "http://localhost/supportTickets",
"first": "http://localhost/supportTickets",
"meta": {
"rules": [
"required": "true",
"maxLength": "200"
]
}
},
"data": [
{
"type": "supportTickets",
"id": "1",
"attributes": {
"description": "Critical: Quae magnam est modi quaerat voluptas repellendus quos dolorem. Ipsum rerum voluptatem facere. Veniam dolor soluta repudiandae doloribus. Qui ratione soluta velit deleniti atque id ad dicta sint."
},
"links": {
"self": "http://localhost/supportTickets/1"
}
}, {
"type": "supportTickets",
"id": "2",
"attributes": {
"description": "Lorem ipsum dolor sit....."
},
"links": {
"self": "http://localhost/supportTickets/2"
}
}
]
}
For some reason this feature seemed to be remove in PR: https://github.com/json-api-dotnet/JsonApiDotNetCore/pull/845/files
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
If I understand you correctly, you’d like to add top-level meta to GET resource requests that lists the attr validation rules for the resource type being requested. To accomplish that, inject
IJsonApiRequest
in yourIResponseMeta
implementation and make sure to register it request-scoped.Example:
which produces:
The general hint was added to the docs recently, but it hasn’t been merged yet. See https://github.com/json-api-dotnet/JsonApiDotNetCore/blob/write-callbacks/docs/usage/extensibility/middleware.md.