Debian Codegraph @

tests/test_alias.py

raw symbols SCIP
1 from ninja import Field, NinjaAPI, Schema
2
3
4 class SchemaWithAlias(Schema):
5 foo: str = Field("", alias="bar")
6
7
8 api = NinjaAPI()
9
10
11 @api.get("/path", response=SchemaWithAlias)
12 def alias_operation(request):
13 return {"bar": "value"}
14
15
16 def test_alias():
17 schema = api.get_openapi_schema()["components"]
18 print(schema)
19 assert schema == {
20 "schemas": {
21 "SchemaWithAlias": {
22 "type": "object",
23 "properties": {
24 "foo": {"type": "string", "default": "", "title": "Foo"}
25 },
26 "title": "SchemaWithAlias",
27 }
28 }
29 }
30
31
32 # TODO: check the conflicting approach
33 # when alias is used both for response and request schema
34 # basically it need to generate 2 schemas - one with alias another without
35 # @api.post("/path", response=SchemaWithAlias)
36 # def alias_operation(request, payload: SchemaWithAlias):
37 # return {"bar": payload.foo}