Skip to content

[FEATURE REQUEST] Add Support for Resource Level Meta #13

@andrew-waters

Description

@andrew-waters

Is your feature request related to a problem? Please describe.

The spec defines the ability for an API to implement meta on the resource level:

In addition, a resource object MAY contain any of these top-level members:

 - attributes: an attributes object representing some of the resource’s data.
 - relationships: a relationships object describing relationships between the resource and other JSON:API resources.
 - links: a links object containing links related to the resource.
 - meta: a meta object containing non-standard meta-information about a resource that can not be represented as an attribute or relationship.

It appears that the library currently only has support for top level meta using the CompoundDocument pattern.

This request is to add meta support to the resources, for example this abbreviated resource:

{
  "data": [{
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "JSON:API paints my bikeshed!"
    },
    "meta": {
      "views": 2
    }
  }]
}

Describe the solution you'd like

There may be a more elegant solution, but it feels like the interface should be something similar to either:

@ResourceWrapper(type: "people")
struct Person: Equatable {
  var id: String

  @ResourceAttribute var firstName: String
  @ResourceAttribute var lastName: String
  @ResourceAttribute var twitter: String?

  @ResourceMeta var meta: PersonMeta
}

Or by extending the ResourceWrapper:

@ResourceWrapper(type: "people", meta: PersonMeta)
struct Person: Equatable {
  var id: String

  @ResourceAttribute var firstName: String
  @ResourceAttribute var lastName: String
  @ResourceAttribute var twitter: String?
}

It feels like the second option is the cleanest, but that may come with the caveat that an opinionated decision needs to be made by the library about the visibility of the property - when using this library in an SDK, properties need to be public for them to be useful. Therefore, the first solution may be the desired one, albeit a bit more verbose. It is also more in line with the @ResourceRelationship.

In either case, the user would simply define their meta struct:

struct PersonMeta: Equatable {
  var views: Int
}

Describe alternatives you've considered

n/a

Additional context

n/a

Acceptance Criteria

Describe the criteria for considering this feature request complete and satisfactory.

  • Resource level meta is decoded alongside other data for one and many requests
  • The visibility of the new meta property can be controlled by the package user

Resources

Stakeholders

n/a

Related Issues

n/a

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions