kevinswiber / siren

Structured Interface for Representing Entities, super-rad hypermedia
MIT License
1.3k stars 71 forks source link

Why are `rel` attributes an Array of strings and not a single string? #79

Closed pke closed 7 years ago

pke commented 7 years ago

Can there be more than one rel on a link/action?

thomaseizinger commented 7 years ago

I guess the same reasoning applies as for multiple classes for the HTML class attribute: In order to have multiple ways for referring to the same element from different contexts. For example, one might assign a generic relation item and a domain-specific relation order to each element of an order collection. In general, using an array instead of a single value is more open for extensions of the spec in the future.

I guess the rationale for using an array is a combination of the above two reasons. However, I am just guessing here :)

kevinswiber commented 7 years ago

@pke Yes, there can be more than one link relation on links and sub-entities.

Take this example:

{
  "class": [
    "root"
  ],
  "links": [
    {
      "rel": [
        "self"
      ],
      "href": "http://stage.zettaapi.org/"
    },
    {
      "title": "cloud",
      "rel": [
        "http://rels.zettajs.io/server"
      ],
      "href": "http://stage.zettaapi.org/servers/cloud"
    },
    {
      "rel": [
        "http://rels.zettajs.io/events"
      ],
      "href": "ws://stage.zettaapi.org/events"
    },
    {
      "title": "annarbor",
      "rel": [
        "http://rels.zettajs.io/peer",
        "http://rels.zettajs.io/server"
      ],
      "href": "http://stage.zettaapi.org/servers/annarbor"
    },
    {
      "title": "detroit",
      "rel": [
        "http://rels.zettajs.io/peer",
        "http://rels.zettajs.io/server"
      ],
      "href": "http://stage.zettaapi.org/servers/detroit"
    },
    {
      "rel": [
        "http://rels.zettajs.io/peer-management"
      ],
      "href": "http://stage.zettaapi.org/peer-management"
    }
  ],
  "actions": [
    {
      "name": "query-devices",
      "method": "GET",
      "href": "http://stage.zettaapi.org/",
      "type": "application/x-www-form-urlencoded",
      "fields": [
        {
          "name": "server",
          "type": "text"
        },
        {
          "name": "ql",
          "type": "text"
        }
      ]
    }
  ]
}

You can see that there are links to servers, some of which are classified as peers. It allows modeling of complex relationships.

We have client code that searches for an executes only on server links. And we have another branch of client code that operates only on peers.

pke commented 7 years ago

Thanks to all for the explanation