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
|
from ninja import Field, NinjaAPI, Schema
class SchemaWithAlias(Schema):
foo: str = Field("", alias="bar")
api = NinjaAPI()
@api.get("/path", response=SchemaWithAlias)
def alias_operation(request):
return {"bar": "value"}
def test_alias():
schema = api.get_openapi_schema()["components"]
print(schema)
assert schema == {
"schemas": {
"SchemaWithAlias": {
"type": "object",
"properties": {
"foo": {"type": "string", "default": "", "title": "Foo"}
},
"title": "SchemaWithAlias",
}
}
}
# TODO: check the conflicting approach
# when alias is used both for response and request schema
# basically it need to generate 2 schemas - one with alias another without
# @api.post("/path", response=SchemaWithAlias)
# def alias_operation(request, payload: SchemaWithAlias):
# return {"bar": payload.foo}
|