Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support OpenAPI example[s] attribute #924

@simonTurintech

Description

@simonTurintech

Hello everyone.
Today I tried to use fastAPI to add an exemple of values for a model, as decribed here.

After a tedious research in the documentation of Pydantic, I managed to go from this implementation:

from fastapi import FastAPI
from pydantic import BaseModel

class CustomModel(BaseModel):
    id: str
    name: str

app = FastAPI()

@app.get("/", response_model=CustomModel)
def root() -> CustomModel:
    return {"message": "Hello World"}

Giving the following result:
Screenshot 2020-01-27 at 21 51 26

To this implementation:

from fastapi import FastAPI
from pydantic import BaseModel

class CustomModel(BaseModel):
    id: str
    name: str

    class Config(object):
        schema_extra = {
            'example': {
                'id': 'n3xR4Ib79h',
                'name': 'John Doe'
            }
        }

app = FastAPI()

@app.get("/", response_model=CustomModel)
def root() -> CustomModel:
    return {"message": "Hello World"}

Giving the following result:
Screenshot 2020-01-27 at 21 51 47

The code used above is introduced in this page of pydantic's documentation.

As this is a behaviour documented in OpenAPI, and as fastAPI can actually provide that behaviour, should it be a good idea to add this to the doc ? It is quite hidden inside pydantic docs, and it could be usefull to other users.

Thank you for your opinion,
Simon

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions