Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix ModelField alias property#13833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
pressogh wants to merge3 commits intofastapi:master
base:master
Choose a base branch
Loading
frompressogh:fix-model-field-alias

Conversation

pressogh
Copy link

Hello!

I've encountered an issue in FastAPI'sopenapi.json generation for parameter inputs (Query,Header,Cookie). When usingalias for field names in Pydantic models, the alias appears correctly in the generated schema. However, when usingvalidation_alias, it is ignored and the original field name is shown instead.

To demonstrate this behavior, I’ve included minimal reproducible examples along with corresponding Swagger UI screenshots.

With alias

frompydanticimportBaseModel,FieldfromtypingimportAnnotatedfromfastapiimportFastAPI,Header,Query,CookieclassDataModel(BaseModel):foo:str=Field(...,alias="bar")app=FastAPI()@app.get("/test")deftest(query:Annotated[DataModel,Query(...)],header:Annotated[DataModel,Header(...)],cookie:Annotated[DataModel,Cookie(...)]):return"Received: "+str(query)+", "+str(header)+", "+str(cookie)
image

With validation_alias

frompydanticimportBaseModel,FieldfromtypingimportAnnotatedfromfastapiimportFastAPI,Header,Query,CookieclassDataModel(BaseModel):foo:str=Field(...,validation_alias="bar")app=FastAPI()@app.get("/test")deftest(query:Annotated[DataModel,Query(...)],header:Annotated[DataModel,Header(...)],cookie:Annotated[DataModel,Cookie(...)]):return"Received: "+str(query)+", "+str(header)+", "+str(cookie)
image

As shown above, even though validation_alias="bar" is set, the OpenAPI schema still uses "foo".

Upon inspecting the parameter generation logic, I found that the name used in the schema is determined via the field alias:

name=param.alias

This uses Pydantic’s ModelField logic, where alias is returned if present; otherwise, the field name is used:

@dataclass
classModelField:
field_info:FieldInfo
name:str
mode:Literal["validation","serialization"]="validation"
@property
defalias(self)->str:
a=self.field_info.alias
returnaifaisnotNoneelseself.name

Proposed Change

This PR modifies the alias resolution logic in ModelField to prioritize validation_alias or serialization_alias, depending on mode, instead of defaulting to alias alone.

Thanks for taking the time to review this!

@pressoghpressogh reopened thisJun 29, 2025
@pressoghpressogh marked this pull request as draftJune 29, 2025 13:40
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

1 participant
@pressogh

[8]ページ先頭

©2009-2025 Movatter.jp