-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from luolingchun/openapi_extra
Openapi extra
- Loading branch information
Showing
26 changed files
with
860 additions
and
373 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
*New in v2.4.0* | ||
|
||
The [BaseModel](https://docs.pydantic.dev/latest/usage/models/) in [Pydantic](https://github.com/pydantic/pydantic) | ||
supports some custom configurations([Model Config](https://docs.pydantic.dev/latest/usage/model_config/)), | ||
so we can use the `openapi_extra` to extend OpenAPI Specification. | ||
|
||
The `openapi_extra` will be merged with the automatically generated OpenAPI schema. | ||
|
||
## form | ||
|
||
```python | ||
class UploadFilesForm(BaseModel): | ||
file: FileStorage | ||
str_list: List[str] | ||
|
||
class Config: | ||
openapi_extra = { | ||
# "example": {"a": 123}, | ||
"examples": { | ||
"Example 01": { | ||
"summary": "An example", | ||
"value": { | ||
"file": "Example-01.jpg", | ||
"str_list": ["a", "b", "c"] | ||
} | ||
}, | ||
"Example 02": { | ||
"summary": "Another example", | ||
"value": { | ||
"str_list": ["1", "2", "3"] | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Effect in Redoc: | ||
|
||
![](../assets/Snipaste_2023-06-02_11-05-11.png) | ||
|
||
## body | ||
|
||
```python | ||
class BookBody(BaseModel): | ||
age: int | ||
author: str | ||
|
||
class Config: | ||
openapi_extra = { | ||
"description": "This is post RequestBody", | ||
"example": {"age": 12, "author": "author1"}, | ||
"examples": { | ||
"example1": { | ||
"summary": "example summary1", | ||
"description": "example description1", | ||
"value": { | ||
"age": 24, | ||
"author": "author2" | ||
} | ||
}, | ||
"example2": { | ||
"summary": "example summary2", | ||
"description": "example description2", | ||
"value": { | ||
"age": 48, | ||
"author": "author3" | ||
} | ||
} | ||
|
||
}} | ||
``` | ||
|
||
Effect in swagger: | ||
|
||
![](../assets/Snipaste_2023-06-02_11-06-59.png) | ||
|
||
## responses | ||
|
||
```python | ||
class Message(BaseModel): | ||
message: str = Field(..., description="The message") | ||
|
||
class Config: | ||
openapi_extra = { | ||
# "example": {"message": "aaa"}, | ||
"examples": { | ||
"example1": { | ||
"summary": "example1 summary", | ||
"value": { | ||
"message": "bbb" | ||
} | ||
}, | ||
"example2": { | ||
"summary": "example2 summary", | ||
"value": { | ||
"message": "ccc" | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Effect in swagger: | ||
|
||
![](../assets/Snipaste_2023-06-02_11-08-40.png) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Author : llc | ||
# @Time : 2023/6/1 15:04 | ||
from typing import List | ||
|
||
from pydantic import BaseModel | ||
|
||
from flask_openapi3 import OpenAPI, FileStorage | ||
|
||
app = OpenAPI(__name__) | ||
|
||
|
||
class UploadFilesForm(BaseModel): | ||
file: FileStorage | ||
str_list: List[str] | ||
|
||
class Config: | ||
openapi_extra = { | ||
# "example": {"a": 123}, | ||
"examples": { | ||
"Example 01": { | ||
"summary": "An example", | ||
"value": { | ||
"file": "Example-01.jpg", | ||
"str_list": ["a", "b", "c"] | ||
} | ||
}, | ||
"Example 02": { | ||
"summary": "Another example", | ||
"value": { | ||
"str_list": ["1", "2", "3"] | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
class BookBody(BaseModel): | ||
age: int | ||
author: str | ||
|
||
class Config: | ||
openapi_extra = { | ||
"description": "This is post RequestBody", | ||
"example": {"age": 12, "author": "author1"}, | ||
"examples": { | ||
"example1": { | ||
"summary": "example summary1", | ||
"description": "example description1", | ||
"value": { | ||
"age": 24, | ||
"author": "author2" | ||
} | ||
}, | ||
"example2": { | ||
"summary": "example summary2", | ||
"description": "example description2", | ||
"value": { | ||
"age": 48, | ||
"author": "author3" | ||
} | ||
} | ||
|
||
}} | ||
|
||
|
||
@app.post('/upload/files') | ||
def upload_files(form: UploadFilesForm): | ||
print(form.file) | ||
print(form.str_list) | ||
return {"code": 0, "message": "ok"} | ||
|
||
|
||
@app.post('/book', ) | ||
def create_book(body: BookBody): | ||
print(body) | ||
return {"code": 0, "message": "ok"} | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.