Skip to content

Navigation Menu

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

Appearance settings

Bound Typevar does not work as body to accept multiple children types of the same parent #9903

Answered by YuriiMotov
orfisko asked this question in Questions
Discussion options

First Check

  • I added a very descriptive title here.
  • I used the GitHub search to find a similar question and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from typing import TypeVar

from fastapi import FastAPI
from fastapi.testclient import TestClient
from pydantic import BaseModel
from starlette import status

app = FastAPI()
testclient = TestClient(app)

class Parent(BaseModel):
    parentAttribute: str

class Child(Parent):
    childAttribute: str

PARENT = TypeVar("PARENT", bound=Parent)

@app.post(
    "/",
    response_model=PARENT,
    status_code=status.HTTP_201_CREATED,
)
def add_parentlike_intance(parent: PARENT):
    return parent

child = Child(parentAttribute="parent", childAttribute="child")

postresponse = testclient.post(
    url=f"/",
    data=child.model_dump_json(),
)

assert postresponse.status_code == 201, postresponse.content
# Traceback (most recent call last):
#   File "/home/hendrik/.config/JetBrains/PyCharmCE2023.1/scratches/bug_bound_typevar.py", line 35, in <module>
#     assert postresponse.status_code == 200, postresponse.content
# AssertionError: b'{"detail":[{"type":"missing","loc":["query","parent"],"msg":"Field required","input":null,"url":"https://errors.pydantic.dev/2.1.2/v/missing"}]}'

Description

When I want to send in a body that is defined as a typevar this does not work.

  • If it is not supported, this needs to be added to the documentation.
  • I would expect fastapi to apply 'union' type behaviour here.
  • Using the typevar to validate the response does work.
  • the error message is very obscure. It seems that due to using the typevar the input variable to the post function cannot be filled. For someone without access to the router code it would be impossible to understand what is going on. Thing is, when developing and without fine knowledge of the ins and outs of fastapi there is not much else to go on for me as the developer either.

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.100.0

Python Version

3.10.6

Additional Context

No response

You must be logged in to vote

You can't create generic endpoints in FastAPI. FastAPI uses type annotations to generate schema and in case using TypeVar it would be impossible to generate schema.

Use Union[Parent, Child] instead.

And, in it doesn't work well for response_model. In the openapi schema you will only see the schema for Parent as the response model.

Replies: 1 comment

Comment options

You can't create generic endpoints in FastAPI. FastAPI uses type annotations to generate schema and in case using TypeVar it would be impossible to generate schema.

Use Union[Parent, Child] instead.

And, in it doesn't work well for response_model. In the openapi schema you will only see the schema for Parent as the response model.

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.