I am creating a simple REST API for a blog website. I'm writing an endpoint for creating posts, I'm using Forms to define the schema instead of Pydantic's models because one of the fields is a user uploaded image which is much easier to handle as a form. I'm trying to add a "Tags" system for posts where a post can have multiple tags, Each tag is an integer value that corresponds to that tag's ID in the DB:
Sending that request yields this response:
{
"detail": [
{
"type": "int_parsing",
"loc": [
"body",
"tags",
0
],
"msg": "Input should be a valid integer, unable to parse string as an integer",
"input": "3,5"
}
]
}
The request is processed normally if the tags array is empty or if it contains exactly 1 element, But 2 or more causes that parsing error.
Here's my endpoint's signature:
@router.post('/', response_model=PostRead, status_code=status.HTTP_201_CREATED)
def create_post(
db: Annotated[Session, Depends(get_db)],
author: Annotated[User, Depends(get_current_user)],
title: Annotated[Optional[str], Form()],
body: Annotated[Optional[str], Form()] = None,
image_file: Annotated[Optional[UploadFile], File()] = None,
tags: Annotated[List[int], Form()] = [],
):
Here's the schema from the openapi.json, it's using version 3.1.0:
Body_create_post_api_posts__post
properties
title
anyOf
0
type "string"
1
type "null"
title "Title"
body
anyOf
0
type "string"
1
type "null"
title "Body"
image_file
anyOf
0
type "string"
contentMediaType "application/octet-stream"
1
type "null"
title "Image File"
tags
items
type "integer"
type "array"
title "Tags"
default []
type "object"
required
0 "title"
title "Body_create_post_api_posts__post"
I tried changing the tags parameter annotation to these but nothing changed:
tags: List[int] = Form([])
tags: List[int] = Form(...)
tags: Annotated[List[int], Form(...)] = []
Also tried changing int to str.
Somebody experienced the same issue here.
