DeanWay / fastapi-versioning

api versioning for fastapi web applications
MIT License
642 stars 63 forks source link

Support for a single method to handle multiple versions #37

Closed c-tanner closed 3 years ago

c-tanner commented 3 years ago

Currently, if we are to support multiple versions of an API with many endpoints, we need to duplicate each endpoint (even if logic does not change for every endpoint) in order to be backward compatible.

An array of values could be passed to @version() to allow a single method to support multiple versions. Example:

Current:

def handle_home() -> str:
    return "You are home."

@app.get("/home")
@version(1, 0)
def home() -> str:
    return handle_home()

@app.get("/home")
@version(1, 1)
def home() -> str:
    return handle_home()

@app.get("/home")
@version(1, 2)
def home() -> str:
    return handle_home()

Suggested:

@app.get("/home")
@version([1, 0], [1, 1], [1, 2])
def home() -> str:
    return "You are home"
beenje commented 3 years ago

Actually, it will automatically add newer versions for each endpoint not redefined. I got this from https://medium.com/geoblinktech/fastapi-with-api-versioning-for-data-applications-2b178b0f843f

That's nice but I wonder how you remove an endpoint in a new version.

DeanWay commented 3 years ago

That's nice but I wonder how you remove an endpoint in a new version.

@beenje this is demonstrated in this example: https://github.com/DeanWay/fastapi-versioning/blob/master/example/annotation/store.py#L48-L51

🙂

DeanWay commented 3 years ago

@c-tanner for your example you can just specify the first version:

@app.get("/home")
@version(1, 0)
def home() -> str:
    return "You are home."

if there are any later versions specified anywhere else then /home will exist for those versions too. The general rule is that you specify the version when an endpoint was introduced or changed, and later versions will include the most recent change. You would only need to do something like the example you've given if you for some reason wanted to create some arbitrary new versions without changing or introducing any endpoint at all.