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.

[DOCS] Support OpenAPI example[s] attribute

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
jmrieboldcommented, Jan 29, 2020

There’s actually a more straightforward way to do it, though it’s not really mentioned in the docs. This should do the trick:

from fastapi import FastAPI, Body
from pydantic import BaseModel

class CustomModel(BaseModel):
    id: str = Body(None, example='n3xR4Ib79h')
    name: str = Body(None, example='John Doe')

The same can be done for query parameters and path parameters, using FastAPI’s Query and Path classes, respectively.

In any case, this should definitely be added to the docs. It took me a fair bit of time to find this solution.

0reactions
github-actions[bot]commented, Apr 9, 2020

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding Examples - Swagger
You can add examples to parameters, properties and objects to make OpenAPI specification of your web service clearer. Examples can be read by...
Read more >
OpenAPI Examples Need Help
The schema object is used in several places in both OAS2 and OAS3: request and response being the two most common. A schema...
Read more >
OpenAPI Specification v3.1.0 | Introduction, Definitions, & More
The OpenAPI Specification (OAS) defines a standard, programming language-agnostic interface description for HTTP APIs.
Read more >
OpenAPI 3.0 Tutorial | SwaggerHub Documentation
The path parameters can be used to isolate a specific component of the data that the client is working with, for example, https://example.io/v1/ ......
Read more >
ASP.NET Core web API documentation with Swagger / OpenAPI
Swagger is tooling that uses the OpenAPI specification. For example, OpenAPIGenerator and SwaggerUI. OpenAPI specification ( openapi.json ). The ...
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