Skip to content

openapi link vs hypermedia link to represent relationships in APIs #2122

@esoni88

Description

@esoni88

Hi All,
Suppose i have a resourse Dog and a Resource Owner. i can design Dog in openapi in the following way:

Dog:
      type: object
      required:
        - id
        - name
        - owner
      properties:
        id:
          type: integer
          format: int32
        name:
          type: string    
        ownerId:
          type: string  

in this way the client can use ownerId to get the owner data calling the api /owners/x

    Dog:
      type: object
      required:
        - id
        - name
        - owner
      properties:
        id:
          type: integer
          format: int32
        name:
          type: string    
        owner:
          ref: $ref: "#/components/schemas/Owner"            

in this way when the client get a Dog get also all the info of the owner. The disvantages is the verbosity of response.. for each Dog the client obtain also all the info of the owner.

  1. following the design best practices i can represent owner field as hypermedialink for ie /owners/1
    Dog:
      type: object
      required:
        - id
        - name
        - owner
      properties:
        id:
          type: integer
          format: int32
        name:
          type: string    
        owner:
          type: string
          format: uri

What is the best way to design this typical situation in openapi ? in general i think the third solution is better as reported in rest best practices.
But reading the new openapi feature link
Links are one of the new features of OpenAPI 3.0. Using links, you can describe how various values returned by one operation can be used as input for other operations. This way, links provide a known relationship and traversal mechanism between the operations. The concept of links is somewhat similar to hypermedia, but OpenAPI links do not require the link information present in the actual responses.
i have some doubts... reading above i understood that the better solution in openapi is to use the first solution extended using Link

    Dog:
      type: object
      required:
        - id
        - name
        - owner
      properties:
        id:
          type: integer
          format: int32
        name:
          type: string    
        ownerId:
          type: string   
          links:
            GetOwnerByIid:   # <---- arbitrary name for the link
              operationId: getOwner
              parameters:
                ownerId: '$response.body#/ownerId'
              description: >
                The `ownerId` value returned in the response can be used as
                the `ownerId` parameter in `GET /owners/{ownerId}`.

My idea is correct ? why openapi not suggest to use hypermedia link for relationship?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Moved to MoonwalkIssues that can be closed or migrated as being addressed in Moonwalklinks

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions