Skip to content

Response x-api-version header is from request when using waterfalling #278

@slafs

Description

@slafs

Describe the bug

The comment from

cadwyn/cadwyn/middleware.py

Lines 127 to 128 in ca3f3a1

# We return it because we will be returning the **matched** version, not the requested one.
# In date-based versioning with waterfalling, it makes sense.
doesn't seem to be true. The x-api-version header in the response seems to have the same value as the one in the request, not the closest matched version determined by "waterfalling".

To Reproduce

This test (app.py):

from cadwyn import Cadwyn, Version, VersionBundle, VersionedAPIRouter

versions = VersionBundle(Version("2023-04-14"), Version("2022-11-16"))

app = Cadwyn(
    versions=versions,
)

router = VersionedAPIRouter()


@router.get("/foo")
def foo():
    return "foo"


app.generate_and_include_versioned_routers(router)


def test_param():
    from fastapi.testclient import TestClient

    client = TestClient(app, headers={"x-api-version": "2024-01-01"})

    res = client.get("/foo")

    assert res.json() == "foo"
    assert res.headers["x-api-version"] == "2023-04-14"

when run like this:

uvx --with 'cadwyn[standard]' pytest app.py

fails.

Expected behavior

As per linked comment, I'd expect the version header value to be 2023-04-14 when I passed 2024-01-01 in the request.

Additional context

Looks like this never worked (? 🤔). The earliest version I can test with this snippet is 4.3.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions