Skip to content
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

[Question] Is there an option to omit optional members of a resource (links) #444

Closed
pixelhandler opened this issue Sep 13, 2015 · 11 comments

Comments

@pixelhandler
Copy link
Contributor

In a resource object I'd like to omit the links object, see http://jsonapi.org/format/#document-resource-objects

I have a long list of objects with one attribute and only need the id, type and attributes members of the resource object.

Is there an option to omit optional members of a resource (links)?

@lgebhardt
Copy link
Member

There isn't an option for excluding links, though it's a good idea. We should probably add this as a global configuration option, and also allow it per resource.

@DavidVII
Copy link

👍

@clumsysnake
Copy link

in the meantime, what's the best way of getting this done in the application layer? subclass the serializer and override #serialize_to_links_hash?

@mecampbellsoup
Copy link
Contributor

@lgebhardt @clumsysnake any ideas on this? Having same issue, i.e. would like to omit links object from the serialized response object produced by an API endpoint.

@lgebhardt
Copy link
Member

I haven't worked on this feature yet. I don't have a good work around at this point.

@mecampbellsoup
Copy link
Contributor

@lgebhardt how about just making it an option that can be passed to serialize_to_hash? Or adding it as an include_directive? https://github.com/mecampbellsoup/jsonapi-resources/blob/c358cb7515441ee7a958b37bcc3be44acc05d0ef/lib/jsonapi/resource_serializer.rb#L46

@mecampbellsoup
Copy link
Contributor

@lgebhardt if you can point me in the right direction I'll take a shot at implementing this, we really need it 😅

@mecampbellsoup
Copy link
Contributor

#735

@utilityboy
Copy link
Contributor

utilityboy commented Sep 1, 2016

I get around this by specifying the fields I want. Not sure if it's supposed to work like this or not, but:

machines?fields[machines]=name

produces:

{
  "data": [
    {
      "id": "1",
      "type": "machines",
      "links": {
        "self": "http://my.api.com/machines/1"
      },
      "attributes": {
        "name": "M1"
      }
    },
    {
      "id": "2",
      "type": "machines",
      "links": {
        "self": "http://my.api.com/machines/2"
      },
      "attributes": {
        "name": "M2"
      }
    },
    {
      "id": "3",
      "type": "machines",
      "links": {
        "self": "http://my.api.com/machines/3"
      },
      "attributes": {
        "name": "M3"
      }
    }
...

If you need any of the related resources, include them as well. It's ugly but does cut down on payload sizes when you don't need related resources.

@senid231
Copy link
Contributor

senid231 commented Oct 29, 2016

after PR #893 it will be possible to allow resource to decide if his relationships has links.

For example: add options to relationships like include_self_route and include_related_route, and make jsonapi_relationships, jsonapi_related_resource(s) and jsonapi_link(s) respect these options

default values for include_self_route and include_related_route can be stored in JSONAPI.configuration

@tommy-russoniello
Copy link
Collaborator

Closed in favor of #594.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants