Skip to content

Commit

Permalink
feat(api): exclude none
Browse files Browse the repository at this point in the history
  • Loading branch information
exherb committed Jul 26, 2024
1 parent 1b1e6b9 commit 6d8eeca
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
6 changes: 6 additions & 0 deletions runtime/anyforce/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,8 @@ def bind(self, router: APIRouter):
response_model=DetailPydanticModels,
response_class=ORJSONResponse,
status_code=status.HTTP_201_CREATED,
response_model_exclude_unset=True,
response_model_exclude_none=True,
)
async def create(
request: Request,
Expand Down Expand Up @@ -487,6 +489,7 @@ async def create(
response_model=Response,
response_class=ORJSONResponse,
response_model_exclude_unset=True,
response_model_exclude_none=True,
)
async def index(
request: Request,
Expand Down Expand Up @@ -576,6 +579,7 @@ async def index(
response_model=DetailPydanticModel,
response_class=ORJSONResponse,
response_model_exclude_unset=True,
response_model_exclude_none=True,
)
async def get(
request: Request,
Expand Down Expand Up @@ -607,6 +611,7 @@ async def get(
response_model=DetailPydanticModels,
response_class=ORJSONResponse,
response_model_exclude_unset=True,
response_model_exclude_none=True,
)
async def update(
request: Request,
Expand Down Expand Up @@ -682,6 +687,7 @@ async def update(
response_model=Union[List[DeleteResponse], DeleteResponse],
response_class=ORJSONResponse,
response_model_exclude_unset=True,
response_model_exclude_none=True,
)
async def delete(
request: Request,
Expand Down
2 changes: 1 addition & 1 deletion runtime/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "anyforce"
version = "0.33.0"
version = "0.40.0"
description = ""
authors = [
{name = "exherb", email = "i@4leaf.me"},
Expand Down
31 changes: 31 additions & 0 deletions runtime/x.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import asyncio
from datetime import datetime
from typing import Optional

from anyforce.model import BaseUpdateModel, fields


class Model(BaseUpdateModel):
name = fields.CharField(max_length=32)

class PydanticMeta(BaseUpdateModel.PydanticMeta):
computed = (
"int_field_plus_bigint_field",
"async_int_field_plus_bigint_field",
)

def int_field_plus_bigint_field(self) -> int:
return 1

async def async_int_field_plus_bigint_field(self) -> Optional[int]:
return 2


async def main():
model = Model(
id=1, name="xxxx", created_at=datetime.now(), updated_at=datetime.now()
)
print(model.detail().model_validate(model).model_dump(exclude_unset=True))


asyncio.run(main())

0 comments on commit 6d8eeca

Please sign in to comment.