Skip to content

Instantly share code, notes, and snippets.

@mikekistler
Last active October 17, 2024 15:10
Show Gist options
  • Save mikekistler/1983a8249c0df56a46f94b4df1b7181d to your computer and use it in GitHub Desktop.
Save mikekistler/1983a8249c0df56a46f94b4df1b7181d to your computer and use it in GitHub Desktop.
Proposal for links in OpenAPI v4
@ralfhandl
Copy link

We should try to also address the "for any entry in the list" case, for example with a wildcard array index [*]:

          links:
            keyId:
              operationId: RegenerateKey
              # The id field of the first key can be set as id property of the request body of RegenerateKey
              requestBody:
                '/id': '$.response/body#value[*].id'

Extracting paired items could then be achieved by using both [*].id and [*].etag in a link object.

@chatelao
Copy link

Typo: Compatability -> Compatibility

Thanks for the proposal, I will have a deeper look later.

@Bert-R
Copy link

Bert-R commented Jun 4, 2024

This proposal also makes it much easier to document HATEOAS APIs. On OAI/OpenAPI-Specification#577, I said the following:

OAS with JSON Schema could describe:

  • The starting path(s) with belonging requests/responses
  • It should be possible to define a link truly as a link with:
    • Allowed HTTP methods
    • For each HTTP method the schema of the request/response

The paths that are used in the links should not be defined as paths in the OAS, as that would stimulate wrong behavior: just use these endpoints instead of following the links.

Your proposal here allows describing starting paths (a set of operations). The extended links refer to operations, which specify the allowed HTTP methods and schema of request and response. My bullet points are covered with that.

The issue that is not addressed is in the last sentence of the quote. A simple solution to that would be to annotate operations with some sort of flag indicating these operations should only be invoked through links returned from the server, never without that server guidance. Is that something that you can add to your proposal?

This way, we have a pragmatic way to define a mediatype, while retaining the dynamic hypermedia as the engine of application state.

@mikekistler
Copy link
Author

I've never understood HATEOAS. The notion of "just follow this link" with no definition of its parameters, headers, authorization scheme, response schema, response headers, etc does not make sense to me. I'm probably misrepresenting the concept of HATEOAS there but that's just the way it seems to me.

So you are correct that my proposal does not address excluding the paths used in links from the API definition. I think they should be in the API definition so that all the details of the request are well defined.

@Bert-R
Copy link

Bert-R commented Jun 4, 2024

The purist approach of HATEOAS is that you figure it all out at runtime. IMHO, that is not realistic.

Your approach allows defining links and the operations they relate to. In a HATEOAS API design, I would use that to define a single public GET operation that returns nothing but a bunch of links. That resource is what the client (e.g. a mobile app) would fetch. The app designers would read what possible links are returned. Then they would read the operations and know the possible subsequent requests. The links would generally be optional and the server will populate those links that are applicable in that actual state of the application.

Such an API design has two major benefits:

  • The app does not need to keep track of the state to know whether a certain operation will be possible. If the operation is possible, its link is available, otherwise not. So, the defined operations are documented in the OAS spec, the subset of actually possible operations is indicated through link availability. E.g. a pageable resource will on the first page not have a previous link, but it will have a next link if multiple pages exist. The next page will have a previous link.
  • The server can change URLs over time without the client being aware (e.g. add a query parameter and populate it from the server, in the the returned link). As long as the operations are the compatible, the app will continue to work.

So, my request to be able to mark operations as "use only through a link" is to guide app designers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment