luolingchun / flask-openapi3

Generate REST API and OpenAPI documentation for your Flask project.
https://luolingchun.github.io/flask-openapi3/
MIT License
203 stars 33 forks source link

Field aliases no longer working with forms and query string #182

Closed jpveooys closed 1 month ago

jpveooys commented 1 month ago

Environment:

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.

luolingchun commented 1 month ago

@jpveooys Thank you for the report.

There are some bugs, and I'm going to fix it