API versioning with OpenAPI generator

77 Views Asked by At

As I found no practical guide online, I guess that a summarized and practical answer would assist many developers out there trying to version their OpenAPI specification.

How I approach API versioning

Ideally, I would like to break my API without forcing clients that are fine with the current version to upgrade their API client package (with pip install --upgrade for instance).

The backend and the API client are both generated by OpenAPI generator, according to a api_v1.yaml specification file. What I would like to do next is the following:

  1. Copy api_v1.yaml file to a new api_v2.yaml file.
  2. Open the api_v2.yaml file with a text editor, and add some changes:
openapi: 3.0.0
info:
  title: Sample API
  version: 2.0.0  # I change this (was 1.0.0)
servers:
  - url: http://api.example.com/v2  # I change this (was v1)
paths:
    # Here I break the v1 routes with some upgrades

Now I have a new api_v2.yaml file with backward incompatible changes.

My Question

I wonder how to use the OpenAPI generator in order to instance a single backend, that supports both of the versions.

The new backend instance should replace the current one, while still offering the v1 business logic at http://api.example.com/v1, in addition to the new changes that are available at http://api.example.com/v2.

I will be satisfied with an automation script that does that, but I refuse to manually edit the OpenAPI generator result as ideally this should be done by a CI/CD job, once I pushed a new major version.

I would appreciate any practical suggestions, that would also help others achieve API versioning as quick as possible.

0

There are 0 best solutions below