-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Exclude links without any data #1668
Conversation
With which level of links were you experiencing this? When last i tested this I recall it omitted links with a nil value by default. Are you saying that it does, in fact, serialize a response similar to: {
"data": {
"attributes": {
"name": "My Cool Post"
},
"links": {
"comments": "/blogs/4134587815891/comments",
"author": "/authors/32512",
"top-comment": null
},
"type": "blogs"
}
} |
It serializes to an empty hash like that: {
"data": {
"attributes": {
"name": "My Cool Post"
},
"links": {
"comments": "/blogs/4134587815891/comments",
"author": "/authors/32512",
"top-comment": {}
},
"type": "blogs"
}
} |
Related to #1514. |
@sigmike would you mind sharing how you'd see a conditional link working? I believe you could do this using the block form if it's an attribute or association link since it's eval'd at runtime, not at load. |
(I'd prefer not to give 'nil' a meaning) |
Maybe like on the attributes: But it would be nice to also be able to pass a block to
I already use the block form to return the link value. That's why just returning nil to disable to link is very convenient. |
sorry, what I meant was has_one :user do
link user_url if object.user.admin?
end I don't totally get the use case |
I'm not sure I get it. My use case is when I use link directly as documented here: https://github.com/rails-api/active_model_serializers/blob/master/docs/general/serializers.md#link I'm using it to provide links to images attached to the resource, but only if such images exist. |
Here's another example of when this happens: Given the following in a serializer: link(:foo) { object.bunnies } When {
"data": {
"id": "5",
"type": "companies",
"attributes": {
"created_at": "2016-03-29T19:09:32.754Z",
"name": "Northern Bank of the South",
"updated_at": "2016-03-29T19:09:32.754Z"
},
"links": {
"self": "http://localhost:9010/companies/5",
"foo": {}
}
}
} At minimum, |
@remear you've convinced me when you changed 'make a link conditional' to 'handle a nil link'. :)
is a bug |
but hmm http://jsonapi.org/format/#document-links
which says nothing about required object attributes and the linkage docs imply it should be |
this is an unresolved contradiction in the spec so omitting it might be best and also |
I was just looking for that. Because there's no clear answer, I'm not sure how we should make this behave. Here's the link schema for reference: "link": {
"description": "A link **MUST** be represented as either: a string containing the link's URL or a link object.",
"oneOf": [
{
"description": "A string containing the link's URL.",
"type": "string",
"format": "uri"
},
{
"type": "object",
"required": [
"href"
],
"properties": {
"href": {
"description": "A string containing the link's URL.",
"type": "string",
"format": "uri"
},
"meta": {
"$ref": "#/definitions/meta"
}
}
}
]
} Reading this leads me to believe link must either be a string in |
@remear #1668 (comment) the schema isn't authoritative :) |
My concern is that it might become authoritative depending on the implementation of #1162. |
has there been a conclusion on this PR? |
@sigmike Sorry for the delay. I'd like to get this merged in. Would you please rebase this against master, add an entry to the CHANGELOG for this under |
Merged on command line and added change log |
Purpose
I couldn't find a way to exclude a link conditionally. So I changed the serializer to exclude links without any data. A link without data is certainly wrong, so it's probably safe to exclude it.
Changes
A link without data is serialized as nil, and links serialized as nil are excluded from the output.
It's only implemented in the json-api adapter.
Caveats
It may not be the best solution to this problem nor the best implementation. Maybe conditionals would be better.
Related GitHub issues
None. Maybe the conditional attributes PR: #1403.