fastapi

Форк
0
/
test_custom_schema_fields.py 
60 строк · 1.2 Кб
1
from fastapi import FastAPI
2
from fastapi._compat import PYDANTIC_V2
3
from fastapi.testclient import TestClient
4
from pydantic import BaseModel
5

6
app = FastAPI()
7

8

9
class Item(BaseModel):
10
    name: str
11

12
    if PYDANTIC_V2:
13
        model_config = {
14
            "json_schema_extra": {
15
                "x-something-internal": {"level": 4},
16
            }
17
        }
18
    else:
19

20
        class Config:
21
            schema_extra = {
22
                "x-something-internal": {"level": 4},
23
            }
24

25

26
@app.get("/foo", response_model=Item)
27
def foo():
28
    return {"name": "Foo item"}
29

30

31
client = TestClient(app)
32

33

34
item_schema = {
35
    "title": "Item",
36
    "required": ["name"],
37
    "type": "object",
38
    "x-something-internal": {
39
        "level": 4,
40
    },
41
    "properties": {
42
        "name": {
43
            "title": "Name",
44
            "type": "string",
45
        }
46
    },
47
}
48

49

50
def test_custom_response_schema():
51
    response = client.get("/openapi.json")
52
    assert response.status_code == 200, response.text
53
    assert response.json()["components"]["schemas"]["Item"] == item_schema
54

55

56
def test_response():
57
    # For coverage
58
    response = client.get("/foo")
59
    assert response.status_code == 200, response.text
60
    assert response.json() == {"name": "Foo item"}
61

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.