'How to do multiple (simultaneous) versions of APIs with OpenAPI and express-openapi?

Re: https://www.npmjs.com/package/express-openapi#getting-started

On many projects I've been worked on (in other technologies) we would always support multiple versions of an API simultaneously, e.g.

GET http://example.com/api/v1/pets
GET http://example.com/api/v2/pets

I'm baffled how to do this with express-openapi (or OpenAPI in general, for that matter).

The way the example files are named (./api-v1/api-doc.js, ./api-v1/paths/worlds.js, ./api-v1/services/worldsService.js) imply that to add a v2 version of the API, you'd have similar files (./api-v2/api-doc.js, etc).

But the initialization code seems to provide no way to add a v2 version of the API to the same instance:

initialize({
  app,
  apiDoc: './api-v1/api-doc.yml',
  dependencies: {
    worldsService: v1WorldsService
  },
  paths: './api-v1/paths'
});

I'm trying to figure out how to do this (so far I can find no examples of this anywhere, even with other OpenAPI implementations). Am I missing something fundamental in how OpenAPI handles this? It seems very basic.

Or should we not be trying to have separate specification files for the separate versions, and put all the versions into one specification?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source