File: pydantic_custom_root_types.py

package info (click to toggle)
flask-openapi3 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,976 kB
  • sloc: python: 4,754; sh: 17; makefile: 15; javascript: 5
file content (71 lines) | stat: -rw-r--r-- 1,390 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# -*- coding: utf-8 -*-
# @Author  : llc
# @Time    : 2022/2/27 15:26
from typing import Any

from pydantic import BaseModel, RootModel

from flask_openapi3 import OpenAPI, Tag

app = OpenAPI(__name__)


class Sellout(BaseModel):
    a: str
    b: int


class SelloutList(RootModel):
    root: list[Sellout]


class SelloutDict(RootModel):
    root: dict[str, Sellout]


class SelloutDict2(RootModel):
    root: dict[Any, Any]


class SelloutDict3(BaseModel):
    model_config = {
        "extra": "allow"
    }


@app.post('/api/v1/sellouts',
          tags=[Tag(name='Sellout', description='Loren.')],
          responses={200: SelloutList}
          )
def post_sellout(body: SelloutList):
    print(body)
    return body.model_dump()


@app.post('/api/v2/sellouts',
          tags=[Tag(name='Sellout', description='Loren.')],
          responses={200: SelloutDict}
          )
def post_sellout2(body: SelloutDict):
    print(body)
    return body.model_dump()


@app.post('/api/v3/sellouts',
          tags=[Tag(name='Sellout', description='Loren.')]
          )
def post_sellout3(body: SelloutDict2):
    print(body)
    return body.model_dump()


@app.post('/api/v4/sellouts',
          tags=[Tag(name='Sellout', description='Loren.')]
          )
def post_sellout4(body: SelloutDict3):
    print(body)
    return body.model_dump()


if __name__ == '__main__':
    app.run(debug=True)