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

Suggestion to employ HATEOAS for relationships if REST is the chosen API style #5

Open
germanandy opened this issue Nov 8, 2024 · 5 comments
Assignees
Labels
help wanted Extra attention is needed

Comments

@germanandy
Copy link

The RFC visits the topic of Objects and their Relationships to each other in a number of places.

In the payload Metadata, the field 'ParentId' appears.

The inclusion of 'ParentId' in the metadata for the object payload indicates that the API design assumes it can arrive at a finite set of ideal relationships - parent/child in hierarchies and then ad-hoc relationships, etc.

This is a more involved problem than it first appears.

In a non-trivial object model, the relationships can become quite complex and hard to represent.

Hierarchies come in several different flavours:

  • Composition/Aggregation Hierarchies (has-a):
    • 'Site A' has-a 'Area X' which has-a 'Line 5' which has-a 'Mixing vessel A'
  • Inheritance (is-a):
    • 'Mixing vessel A' is-a instance of 'MixingVessel' which is-a subclass of 'Vessel'
  • Association:
    • 'Mixing vessel A' Feeds-into 'Fermentation Vessel B'

There are more types but these are typical.

As the system grows in complexity the schema could be repeatedly modified to accommodate new relationships, resulting in overloading or misusing existing fields.

SUGGESTION IF USING REST AS THE API ARCHITECTURAL STYLE - Embed the relationships in the HATEOAS links.

HATEOAS Principle
This principle first appeared in Roy Fielding's original REST dissertation and it is underutilised but quite a neat fit here
https://en.wikipedia.org/wiki/REST
https://en.wikipedia.org/wiki/HATEOAS

Put the relationships in the links section via HATEOAS
Put the relationships in the links section and leave it dynamic, establish a few conventions about what kind of relationships could appear there. Note the MQTT subscribe link in the example below.

{
  "elementId": "mixing_vessel_a",
  "DataType": "Object",
  "properties": {
    // Properties specific to Mixing Vessel A
  },
  "links": [
    {
      "rel": "belongs-to",
      "href": "/org/site_a/areas/area_x/lines/line_5",
      "elementId": "line_5"
    },
    {
      "rel": "is-a",
      "href": "/classes/mixing_vessel",
      "elementId": "mixing_vessel_class"
    },
    {
      "rel": "feeds-into",
      "href": "/org/site_a/areas/area_x/lines/line_5/fermentation_vessel_b",
      "elementId": "fermentation_vessel_b"
    },
    {
      "rel": "mqtt-subscribe",
      "href": "mqtt://broker.example.com/mixing_vessel_a/data",
      "title": "MQTT Data Subscription for Mixing Vessel A"
    },
    {
      "rel": "has-a",
      "children": [
        {
          "href": "/org/site_a/areas/area_x/lines/line_5/mixing_vessel_a/agitation_system",
          "elementId": "agitation_system",
        },
        {
          "href": "/org/site_a/areas/area_x/lines/line_5/mixing_vessel_a/temperature_sensor",
          "elementId": "temperature_sensor",
        },
        {
          "href": "/org/site_a/areas/area_x/lines/line_5/mixing_vessel_a/pressure_valve",
          "elementId": "pressure_valve",

        }
      ]
    }
  ]
}

In this way you shift the responsibility of conveying relationships from the payload to the hypermedia controls within the RESTful response. This means that instead of embedding relationship identifiers directly in the data, you provide links that define how the current resource is related to others. This decouples the core data schema from its relationships, allowing each to evolve independently.

There are some downsides to this, mainly in the flexibility of the way the schema is expressed, but there may be more danger in trying to define relationships of objects inside the data model.

@germanandy germanandy changed the title Novel suggestion to employ HATEOAS for relationships if REST is the chosen API style Suggestion to employ HATEOAS for relationships if REST is the chosen API style Nov 8, 2024
@jwise-mfg jwise-mfg added the help wanted Extra attention is needed label Nov 21, 2024
@randy-armstrong
Copy link

The OPC UA address space is like this. The relationships are metadata that is independent of the values. There is a separate Browse service that allows a client to request the relationships on a Element/Node. OPC UA also provides all of the mechanisms needed to model complex systems.

Many of the implementers of this API will also have knowledge of OPC UA so I think it makes sense to borrow from OPC UA when it comes down to a choice between using something else that provides the same features but is different enough to create headaches for developers.

@jwise-mfg jwise-mfg self-assigned this Feb 11, 2025
@jwise-mfg
Copy link
Member

@germanandy the Working Group would like to hear from a SME on this topic. Would you care to join us for a meeting, or can you recommend someone we could learn more from?

@germanandy
Copy link
Author

germanandy commented Feb 13, 2025 via email

@jwise-mfg
Copy link
Member

@germanandy sent you a message on LinkedIn, thanks!

@jwise-mfg
Copy link
Member

jwise-mfg commented Feb 21, 2025

@germanandy and Niels Andersen from LNS invited for March 11 WG meeting as SMEs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants