question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] OpenAPI does not recognise default values in data models

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
JohnPatoncommented, Mar 27, 2020

There is a difference between default and example.

class Item(pydantic.BaseModel):
    name: str = pydantic.Field(default='a', example='a')
    description: str = pydantic.Field(default='3', example='3')

will do what you want.

Might be a nice feature to use the default as an example if example isn’t specified

1reaction
artdgncommented, Jun 1, 2020

Thanks 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:

  • Prevent future confusion for users who will intuitively expect pythonic behaviour by specifying one value (the one specified in the typing annotation), instead of using the Field class and specifying both verbosly and repetitively for every field.
  • It doesn’t solve the semantic inconsistency between /docs and /redoc UIs demonstrated in the OP.
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found