-
Notifications
You must be signed in to change notification settings - Fork 834
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
Allow custom resource links #1678
Comments
Your interpretation of the specification is correct. If not stated explicitly implementations (and profiles) are not allowed to add additional members. This includes The names are reserved for potential further usage by the specification itself. If implementations would be allowed to define additional members, adding new members to the specification would be a breaking change. However this does not prevent usage of custom links. Extensions are allowed to define additional members. Including additional members of the |
It's also perfectly valid to have attributes whose values are URLs; URL values are not restricted to being placed in the {
"data": {
"id": "4",
"type": "movies",
"attributes": {
"imdb": "https://www.imdb.com/title/tt0133093/",
"title": "The Matrix"
}
}
} |
Thx for the replies. @jelhan Could an extension allow any custom links, without specifying them explicitely? @freddrake While your suggestion would be syntactically compliant, I think this breaks the idea of REST (HATEOAS). IMHO links should be discoverable by knowing the media type structure, not by the implicite knowledge of the semantics of the data. |
An extension can define any member name within their namespace. The namespace prevents collisions between members defined by an extension and potential additions to the base specification later. Please have a look at rules for extensions section for details.
I don't see how your attempt to define any custom link fits with the purpose of HATEOAS. It is not enough that a link is discoverable. A consumer also needs to understand the meaning of that link. This is why the JSON:API specification carefully defines the meaning of the links. I don't see much difference between Please note that presenting data in |
@jelhan thx for the pointers. |
Within the spec, the {
"data": {
"id": "4",
"type": "movies",
"attributes": {
"title": "The Matrix"
},
"relationships": {
"directors": {
"links": {
"related": "/directors/254"
}
}
}
}
} @jelhan wrote:
In the above example any client can figure out from the JSON:API structure that a movie has a relationship to another resource, and that the name of this relationship is BTW, the background of this issue is the following. Many hypermedia media types have the approach that the name of a link (relation) can be chosen by the providing service, e.g. HAL, HAL-FORMS, Siren ... Many (or even most) of the links in above formats could probably be transformed into valid JSON:API relationships, but some probably cannot, because in a JSON:API relationship the related link ALWAYS has to link to a related REST resource. So @freddrake 's suggestion would be a valid way to express those links in JSON:API. For me, the JSON:API spec standalone is fine, because most service developers could follow the following practice:
But if you have a generic programming model that provides a link abstraction, and you want to provide different media types as output, it is a bit harder to make the result JSON:API compliant. |
Both have very different purpose in the specification. The relationships object represents links between different resource. In combination with the attributes object it is responsible for representing the data. The data model of an API is highly domain driven. It can not be standardized in a generic way. Therefore the spec only puts few constraints on it, e.g. that a relationship must not have the same name as an attribute. The links object provides a client information how to construct additional requests. E.g. a Further standardizing allowed members of a relationships object would very likely conflict with JSON:API's goal to be agnostic about the data model of an API. Less standardizing allowed members of a links object would prevent a client from use these links in a meaningful way.
I would recommend to use an extension for these cases. An extension could not only define additional members but also define processing rules for them. Using an extension enables content negotiation. |
I'm not very familiar with all of them. But the HAL does not seem to specify that a provided link must also return a HAL document. That seems to be the main difference compared to JSON:API specification. |
I do see the value in custom resource links (I've actually used them for a long time, unaware of this limitation), for example for the html variant of the resource. Using an extension to allow them feels like a big burden. Can't we use rules similar to query parameters? By only reserving a-z link names and allowing custom links if they use camelCame for example. |
Isn't this what content negotiation is for? |
This is an interesting proposal. I feel we should discuss it with a broader perspective. Opened #1680 to do so. |
Do you mean this?
Than it would only work for the same resource in another content type. E.g. only a single link per resource. That only allows Plus it would give a bit ugly urls for end users I'd say. Somewhere in the url (host/path) is probably an |
Maybe I understand something wrong here, but the spec says:
Furthermore the spec says:
I interpret this language so that the following JSON would NOT be compliant with the spec:
If my interpretation is right, please allow custom links. Otherwise many REST hypermedia use cases could not be implemented in a spec-compliant way.
If my interpretation is wrong, please clarify the language of the spec by explicitly adding language to allow custom resource links.
Just a side note: If you validate the above JSON with https://www.jsonschemavalidator.net/ (and use the provided JSON:API schema), it says it is valid JSON:API.
The text was updated successfully, but these errors were encountered: