[BUG] OpenAPI does not recognise default values in data models
See original GitHub issueDescribe the bug
When using a data model for a post endpoint the default values are not shown correctly in /docs
UI, but are shown correctly in /redoc
UI.
To Reproduce
import fastapi
import pydantic
app = fastapi.FastAPI()
class Item(pydantic.BaseModel):
name: str = pydantic.Field(default='a')
description: str = '3'
@app.put("/")
def func(item: Item):
return {}
- In
/docs
, the default values displayed incorrectly:
{
"name": "string",
"description": "string"
}
- in
/redoc
, the default values are displayed correctly:
{
"name": "a",
"description": "3"
}
Environment
-
OS: Linux, Ubuntu
-
FastAPI Version
0.52.0
-
Python version
3.6.9
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Can a swagger object passed as a parameter have default ...
default - The default value represents what would be assumed by the consumer of the input as the value of the schema if...
Read more >OpenAPI Specification - Version 3.0.3 - Swagger
Tools that do not recognize a specific format MAY default back to the type alone, as if the format is not specified. The...
Read more >OpenAPI Specification v3.1.0 | Introduction, Definitions, & More
If the servers property is not provided, or is an empty array, the default value would be a Server Object with a url...
Read more >Your first OpenAPI document (Part II: data model) - Medium
nullable boolean : if the field can be nullable — remember: null is not a value itself, you only have this field to...
Read more >Fix Swagger Validator errors in Power Platform connectors
For each error, there's a corresponding issue and solution. ... OperationNotAllowed, Body or form data parameters are not supported in a ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
There is a difference between
default
andexample
.will do what you want.
Might be a nice feature to use the
default
as anexample
ifexample
isn’t specifiedThanks for looking into it, both the explanation (on the difference between example and default) and the suggestion to take it up with OAS / pydantic is helpful for someone who will find this issue after encountering this problem.
However, it won’t:
/docs
and/redoc
UIs demonstrated in the OP.