[DOCS] Support OpenAPI example[s] attribute
See original GitHub issueHello 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:
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:
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:
- Created 4 years ago
- Comments:6 (6 by maintainers)
Top 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 >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’s actually a more straightforward way to do it, though it’s not really mentioned in the docs. This should do the trick:
The same can be done for query parameters and path parameters, using FastAPI’s
Query
andPath
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.
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.