vervet icon indicating copy to clipboard operation
vervet copied to clipboard

Need a way to express contextual examples with $refs

Open cmars opened this issue 4 years ago • 2 comments

OpenAPI has a fundamental limitation with examples and $refs. You can put examples in referenced types, but if that type is something really generic like a JSON API link or our stripped-down relationship type, the example isn't going to be very relevant to the resource that you're reading about.

OpenAPI 3.1.0 took a step in the right direction, allowing summary: and description: to be used with $ref:.

Proposal: x-snyk-ref-example

Until OpenAPI catches up, proposing an extension x-snyk-ref-example that provides this capability. An extension is used so that it is clear and explicit that this is not yet standard OpenAPI behavior. It also gives us a clear way to ignore or adapt OpenAPI lint rules that might otherwise fail on it.

Example:

type: object
properties:
  links:
    $ref: "#/components/schemas/Links"
    x-snyk-ref-example:
      self:
        href: /what/an/actual/path/to/one/of/these/looks/like

cmars avatar Nov 18 '21 22:11 cmars

Related:

  • https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#referenceObject
  • Discussion about it in https://github.com/OAI/OpenAPI-Specification/issues/1514

cmars avatar Nov 19 '21 19:11 cmars

A better alternative to consider is requiring sub-typed links to be used in resources, which would then have actually-relevant examples. Then we don't need a custom extension.

cmars avatar Dec 06 '21 14:12 cmars