This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to have multiple str in body with an example for each of these str (while typing those value as string) #5249
Comments
I thought this was a straight up answer but I ran into several weird behaviours when trying to come up with examples. I need a little more time to really dive into this, because it is not at all consistent. For starters, try replacing your
This is not picked up at all, while using just 'example' is picked up. That is even the case when you have multiple properties (sentence_1 and foo in this case). Weird and unexpected. |
Yea I encountered this behaviour (in part leading me to split the params into multiple schemas) Thanks for your time, curious to know where the final point of that will be |
Alright so this is partly a SwaggerUI problem I think, and partly a FastAPI problem. Let's talk about the SwaggerUI problems first. The example that I gave above runs properly in Code: class Sentence3(BaseModel):
sentence_1: str
sentence_2: str
class Config:
schema_extra = {
"examples": [
{
"sentence_1": "Hello sentence 1 from model3",
"sentence_2": "Hello sentence 2 from model3",
},
]
}
@app.post("/with_model_3")
def with_model_3(sentences: Sentence3):
return "hello there" This will render in SwaggerUI: but will render properly in Redoc: I came on to this because I couldn't find anything wrong with the generated So at least, you don't have to separate the two sentences in two Now, the weird stuff is still that when you put two |
Thanks for you answer @JarroVGIT. It seems that Example code from fastapi import Body, FastAPI, Form
app = FastAPI()
@app.post("/body/empty")
async def body_empty(importance: int = Body()):
return ""
# Does provide an example value
@app.post("/body/example")
async def body_example(importance: int = Body(example=2)):
return ""
@app.post("/form/empty")
async def form_empty(importance: int = Form()):
return ""
# Does NOT provide an example value
@app.post("/form/example")
async def form_example(importance: int = Form(example=2)):
return "" |
|
@JarroVGIT I will take a look at starting from today. Keeping you up to date during this week. |
@JarroVGIT This is my current stat of understanding regarding the code base :
If On the contrary, if I have also few questions for you:
I hope my explanations and understanding are not too messy. |
Update : As you do (cf): for f in flat_dependant.body_params:
BodyModel.__fields__[f.name] = f followed by (cf): final_field = create_response_field(
name="body",
type_=BodyModel, # used here
required=required,
alias="body",
field_info=BodyFieldInfo(**BodyFieldInfo_kwargs),
) It seems that you do provides to The question now is, do you rely on If you rely on |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
First Check
Commit to Help
Example Code
Description
I'm trying to find a way to :
str
str
But seems that if we provide two value in the body we must use
BaseModel
(cf) to describe it example value, however if we do that the param will no longer be typed as anstr
but as anobject
.How can I create a route requiring two str in the body while providing an example for those two str and still have those values typed as
str
?In the example code I create three routes :
/one_body
to show that we can provide an example when there is only one value in the body/only_body
to show that when provide two values in the body we can no longer provide an example/with_base_model
to show that when useBaseModel
to provide an example, the value is no longer typed as anstr
/openapi.json
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.79.0
Python Version
Python 3.8.10
Additional Context
No response
The text was updated successfully, but these errors were encountered: