fastapi

Форк
0
/
test_repeated_parameter_alias.py 
100 строк · 3.7 Кб
1
from fastapi import FastAPI, Path, Query, status
2
from fastapi.testclient import TestClient
3

4
app = FastAPI()
5

6

7
@app.get("/{repeated_alias}")
8
def get_parameters_with_repeated_aliases(
9
    path: str = Path(..., alias="repeated_alias"),
10
    query: str = Query(..., alias="repeated_alias"),
11
):
12
    return {"path": path, "query": query}
13

14

15
client = TestClient(app)
16

17

18
def test_get_parameters():
19
    response = client.get("/test_path", params={"repeated_alias": "test_query"})
20
    assert response.status_code == 200, response.text
21
    assert response.json() == {"path": "test_path", "query": "test_query"}
22

23

24
def test_openapi_schema():
25
    response = client.get("/openapi.json")
26
    assert response.status_code == status.HTTP_200_OK
27
    actual_schema = response.json()
28
    assert actual_schema == {
29
        "components": {
30
            "schemas": {
31
                "HTTPValidationError": {
32
                    "properties": {
33
                        "detail": {
34
                            "items": {"$ref": "#/components/schemas/ValidationError"},
35
                            "title": "Detail",
36
                            "type": "array",
37
                        }
38
                    },
39
                    "title": "HTTPValidationError",
40
                    "type": "object",
41
                },
42
                "ValidationError": {
43
                    "properties": {
44
                        "loc": {
45
                            "items": {
46
                                "anyOf": [{"type": "string"}, {"type": "integer"}]
47
                            },
48
                            "title": "Location",
49
                            "type": "array",
50
                        },
51
                        "msg": {"title": "Message", "type": "string"},
52
                        "type": {"title": "Error Type", "type": "string"},
53
                    },
54
                    "required": ["loc", "msg", "type"],
55
                    "title": "ValidationError",
56
                    "type": "object",
57
                },
58
            }
59
        },
60
        "info": {"title": "FastAPI", "version": "0.1.0"},
61
        "openapi": "3.1.0",
62
        "paths": {
63
            "/{repeated_alias}": {
64
                "get": {
65
                    "operationId": "get_parameters_with_repeated_aliases__repeated_alias__get",
66
                    "parameters": [
67
                        {
68
                            "in": "path",
69
                            "name": "repeated_alias",
70
                            "required": True,
71
                            "schema": {"title": "Repeated Alias", "type": "string"},
72
                        },
73
                        {
74
                            "in": "query",
75
                            "name": "repeated_alias",
76
                            "required": True,
77
                            "schema": {"title": "Repeated Alias", "type": "string"},
78
                        },
79
                    ],
80
                    "responses": {
81
                        "200": {
82
                            "content": {"application/json": {"schema": {}}},
83
                            "description": "Successful Response",
84
                        },
85
                        "422": {
86
                            "content": {
87
                                "application/json": {
88
                                    "schema": {
89
                                        "$ref": "#/components/schemas/HTTPValidationError"
90
                                    }
91
                                }
92
                            },
93
                            "description": "Validation Error",
94
                        },
95
                    },
96
                    "summary": "Get Parameters With Repeated Aliases",
97
                }
98
            }
99
        },
100
    }
101

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

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

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

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