flask-openapi3 icon indicating copy to clipboard operation
flask-openapi3 copied to clipboard

Field aliases no longer working with forms and query string

Open jpveooys opened this issue 4 months ago • 1 comments

Environment:

  • Python version: 3.12.3
  • Operating system: Ubuntu 24.04
  • Flask version: 3.0.3
  • flask-openapi3 version: 4.0.0

Since version 4.0.0, aliases no longer work for forms. This is a simple test that works in tests/test_validation_error.py:

class AliasModel(BaseModel):
    aliased_field: str = Field(alias="aliasedField")  # or Field(validation_alias="aliasedField")

@app.post("/alias-test")
def alias_test(form: AliasModel):
    assert form.aliased_field == "test"
    return b"", 200


def test_form_alias(client):
    resp = client.post(
        "/alias-test",
        data={"aliasedField": "test"},
    )
    assert resp.status_code == 200, resp.json

    resp = client.post(
        "/alias-test",
        data={"aliased_field": "test"},
    )
    assert resp.status_code == 400, resp.json

Similarly, there are problems when using aliases for query:

class QueryModel(BaseModel):
    aliased_field: str = Field(alias="aliasedField")  # or Field(validation_alias="aliasedField") 


@app.get("/query-alias-test")
def query_alias_test(query: QueryModel):
    assert query.aliased_field == "test"
    return b"", 200


def test_query_alias(client):
    resp = client.get(
        "/query-alias-test",
        query_string={"aliasedField": "test"},
    )
    assert resp.status_code == 200, resp.json

This seems to have regressed in https://github.com/luolingchun/flask-openapi3/commit/2f33e859b9fa8a6b0114b0ae850cb4b1fda1e4c6.

There is no problem when using JSON and the body view argument.

jpveooys avatar Oct 01 '24 14:10 jpveooys